Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino UNO-Based Automated Control System with Ultrasonic Sensors and Rain Detection

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Automated Control System with Ultrasonic Sensors and Rain Detection

Circuit Documentation

Summary

The circuit in question is designed to interface an Arduino UNO with multiple sensors and actuators for various control applications. It includes ultrasonic sensors for distance measurement, a rain sensor for detecting precipitation, relays for controlling power to a motor and a solenoid, and LEDs for indication purposes. The Arduino UNO serves as the central processing unit, running embedded code to manage inputs from the sensors and control outputs to the actuators based on predefined conditions.

Component List

Arduino UNO

HC-SR04 Ultrasonic Sensor (x3)

12V Single Channel Relay (x2)

Rain Sensor

LEDs: Two Pin (Red, Green, Blue)

DC Motor

12V Battery

Solenoid

Wiring Details

Arduino UNO

HC-SR04 Ultrasonic Sensors

12V Single Channel Relays

Rain Sensor

LEDs (Red, Green, Blue)

DC Motor

12V Battery

Solenoid

Documented Code

// Define pins for Ultrasonic Sensors and Electromagnet
#define trigPin1 9
#define echoPin1 10
#define magnetPin1 8

#define trigPin2 11
#define echoPin2 12
#define mirrorControlPin 13

#define trigPin3 5
#define echoPin3 4
#define magnetPin2 2

// Define pins for Rain Sensor and Relay Control
#define rainSensorPin 7
#define relayPin 6

// Constants for distance thresholds
const int thresholdDistance1 = 30;  // Threshold distance in cm for door (Sensor 1)
const int thresholdDistance2 = 20;  // Threshold distance in cm for mirrors (Sensor 2)
const int thresholdDistance3 = 30;  // Threshold distance in cm for additional door (Sensor 3)

long duration;
int distance;

// Setup function to initialize all pins
void setup() {
  // Setup for Ultrasonic Sensor 1 and Electromagnet
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(magnetPin1, OUTPUT);

  // Setup for Ultrasonic Sensor 2 and Mirror Control
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(mirrorControlPin, OUTPUT);
  digitalWrite(mirrorControlPin, LOW);  // Ensure mirrors start unfolded

  // Setup for Ultrasonic Sensor 3 and Electromagnet
  pinMode(trigPin3, OUTPUT);
  pinMode(echoPin3, INPUT);
  pinMode(magnetPin2, OUTPUT);

  // Setup for Rain Sensor and Relay
  pinMode(rainSensorPin, INPUT);
  pinMode(relayPin, OUTPUT);

  // Initialize Serial Communication
  Serial.begin(9600);
}

// Main loop function
void loop() {
  // Ultrasonic Sensor 1 for Door Control
  controlElectromagnet1();

  // Ultrasonic Sensor 2 for Mirror Control
  controlMirrors();

  // Ultrasonic Sensor 3 for Additional Door Control
  controlElectromagnet2();

  // Rain Sensor for Window Control
  controlWindow();

  delay(1000);  // General delay to stabilize all readings and avoid excessive polling
}

// Function to control electromagnet based on ultrasonic sensor 1
void controlElectromagnet1() {
  // Clear the trigPin1
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);

  // Sets the trigPin1 HIGH for 10 microseconds
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);

  // Reads the echoPin1, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin1, HIGH);

  // Calculate the distance
  distance = duration * 0.034 / 2;  // Speed of sound wave divided by 2

  Serial.print("Distance for door (sensor 1): ");
  Serial.println(distance);

  // Check the distance and control the electromagnet
  if (distance < thresholdDistance1 && distance > 0) {  // If distance is below threshold and valid
    digitalWrite(magnetPin1, HIGH);  // Activate the electromagnet
    Serial.println("Obstacle detected, door held (sensor 1)!");
  } else {
    digitalWrite(magnetPin1, LOW);  // Deactivate the electromagnet
    Serial.println("Path clear, door released (sensor 1)!");
  }
}

// Function to control mirrors based on ultrasonic sensor 2
void controlMirrors() {
  // Read distance from ultrasonic sensor 2
  long duration2, distance2;
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);

  // Calculate the distance in centimeters
  distance2 = duration2 * 0.034 / 2;

  Serial.print("Distance for mirrors: ");
  Serial.println(distance2);

  // If the distance is below the threshold, fold the mirrors
  if (distance2 < thresholdDistance2 && distance2 > 0) {
    Serial.println("Obstacle detected! Folding mirrors.");
    foldMirrors();
  } else {
    stopFoldingMirrors();
  }
}

// Function to control electromagnet based on ultrasonic sensor 3
void controlElectromagnet2() {
  // Clear the trigPin3
  digitalWrite(trigPin3, LOW);
  delayMicroseconds(2);

  // Sets the trigPin3 HIGH for 10 microseconds
  digitalWrite(trigPin3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin3, LOW);

  // Reads the echoPin3, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin3, HIGH);

  // Calculate the distance
  distance = duration * 0.034 / 2;  // Speed of sound wave divided by 2

  Serial.print("Distance for door (sensor 3): ");
  Serial.println(distance);

  // Check the distance and control the electromagnet
  if (distance < thresholdDistance3 && distance > 0) {  // If distance is below threshold and valid
    digitalWrite(magnetPin2, HIGH);  // Activate the electromagnet
    Serial.println("Obstacle detected, door held (sensor 3)!");
  } else {
    digitalWrite(magnetPin2, LOW);  // Deactivate the electromagnet
    Serial.println("Path clear, door released (sensor 3)!");
  }
}

// Function to control the window based on rain sensor
void controlWindow() {
  int rainSensorValue = digitalRead(rainSensorPin);

  // Check if it's raining
  if (rainSensorValue ==