Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Project Documentation

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

Image 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

  • Microcontroller board based on the ATmega328P
  • It has 14 digital input/output pins, 6 analog inputs, a 16 MHz quartz crystal, a USB connection, a power jack, an ICSP header, and a reset button.

HC-SR04 Ultrasonic Sensor (x3)

  • Ultrasonic distance measuring module
  • Uses sonar to determine the distance to an object
  • Features VCC, TRIG, ECHO, and GND pins

12V Single Channel Relay (x2)

  • Electromechanical switch used to control high power circuits with a low power signal
  • Has Normally Closed (NC), Common (COM), Normally Open (NO), Input (IN), Ground (GND), and VCC pins

Rain Sensor

  • Detects rainwater and provides both analog and digital outputs
  • Comes with AO (Analog Output), DO (Digital Output), GRD (Ground), and VCC (Power) pins

LEDs: Two Pin (Red, Green, Blue)

  • Standard light-emitting diodes for indication
  • Each LED has an anode and a cathode pin

DC Motor

  • Converts electrical energy into mechanical rotation
  • Has two pins for power connection

12V Battery

  • Provides power to the circuit
  • Has positive (+) and negative (-) terminals

Solenoid

  • An electromechanical device used for creating a magnetic field from electric current
  • Has two pins for power connection

Wiring Details

Arduino UNO

  • GND connected to common ground net
  • 5V connected to common VCC net
  • Digital pins D2 to D13 connected to various sensors and actuators as per the code

HC-SR04 Ultrasonic Sensors

  • VCC pins connected to common VCC net
  • GND pins connected to common ground net
  • TRIG and ECHO pins connected to specific Arduino digital pins as per the code

12V Single Channel Relays

  • VCC pins connected to common VCC net
  • GND pins connected to common ground net
  • IN pins connected to specific Arduino digital pins as per the code
  • COM pins connected to the positive terminal of the 12V battery
  • NC pins connected to the DC Motor and Solenoid

Rain Sensor

  • VCC pin connected to common VCC net
  • GRD pin connected to common ground net
  • DO pin connected to an Arduino digital pin as per the code

LEDs (Red, Green, Blue)

  • Cathode pins connected to common ground net
  • Anode pins connected to specific Arduino digital pins as per the code

DC Motor

  • One pin connected to the NC pin of a relay
  • Other pin connected to the negative terminal of the 12V battery

12V Battery

  • Positive terminal connected to COM pins of relays
  • Negative terminal connected to one pin of the DC Motor and one pin of the Solenoid

Solenoid

  • One pin connected to the NC pin of a relay
  • Other pin connected to the negative terminal of the 12V battery

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 ==