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

Arduino UNO-Based Motion and Distance Detection System with Relay-Controlled Alarm and LED Indicator

View and Edit Project In Cirkit DesignerImage of Arduino UNO-Based Motion and Distance Detection System with Relay-Controlled Alarm and LED Indicator

Circuit Documentation

Summary

This circuit utilizes an Arduino UNO microcontroller to interface with two HC-SR05 ultrasonic sensors, two PIR motion sensors, an LDR, a 2-channel relay module, and a red light indicator. The system is designed to detect movement using the PIR and ultrasonic sensors and trigger an alarm and LED indicator through the relay module when movement is detected.

Component List

  1. Arduino UNO

    • Description: Microcontroller board based on the ATmega328P.
    • Pins: UNUSED, IOREF, Reset, 3.3V, 5V, GND, Vin, A0, A1, A2, A3, A4, A5, SCL, SDA, AREF, D13, D12, D11, D10, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0
  2. HC-SR05 (Ultrasonic Sensor)

    • Description: Ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND, OUT
  3. HC-SR05 (Ultrasonic Sensor)

    • Description: Ultrasonic distance sensor.
    • Pins: VCC, TRIG, ECHO, GND, OUT
  4. Red Light 220VAC

    • Description: 220V AC red light indicator.
    • Pins: -, +
  5. LDR (Light Dependent Resistor)

    • Description: Light sensor.
    • Pins: A0, D0, GND, VCC
  6. 2-Channel Relay

    • Description: Relay module with two channels.
    • Pins: NC, COM, NO, VCC, IN2, IN1, GND
  7. 12V Power Supply

    • Description: 12V DC power supply.
    • Pins: +, -
  8. Power 220V

    • Description: 220V AC power source.
    • Pins: hot wire, neutral wire
  9. PIR/Motion Sensor

    • Description: Passive Infrared (PIR) motion sensor.
    • Pins: GND, OUTPUT, VCC
  10. PIR/Motion Sensor

    • Description: Passive Infrared (PIR) motion sensor.
    • Pins: GND, OUTPUT, VCC

Wiring Details

Arduino UNO

HC-SR05 (Ultrasonic Sensor)

HC-SR05 (Ultrasonic Sensor)

Red Light 220VAC

LDR

2-Channel Relay

12V Power Supply

Power 220V

PIR/Motion Sensor

PIR/Motion Sensor

Code Documentation

/*
 * This Arduino sketch uses two motion sensors (PIR) and two ultrasonic sensors
 * (HC-SR05) to detect movement. If movement is detected, it triggers an alarm
 * and lights up an LED indicator through a 2-channel relay module.
 */

// Pin definitions
const int pir1Pin = 12; // PIR sensor 1 output pin
const int pir2Pin = 13; // PIR sensor 2 output pin
const int trigPin1 = 9; // HC-SR05 sensor 1 trigger pin
const int echoPin1 = 10; // HC-SR05 sensor 1 echo pin
const int trigPin2 = 5; // HC-SR05 sensor 2 trigger pin
const int echoPin2 = 4; // HC-SR05 sensor 2 echo pin
const int relayPin1 = 7; // Relay channel 1 pin
const int relayPin2 = 8; // Relay channel 2 pin

void setup() {
  // Initialize serial communication
  Serial.begin(9600);

  // Initialize PIR sensor pins
  pinMode(pir1Pin, INPUT);
  pinMode(pir2Pin, INPUT);

  // Initialize ultrasonic sensor pins
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);

  // Initialize relay pins
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);

  // Set initial state of relays to LOW (off)
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, LOW);
}

void loop() {
  // Read PIR sensor values
  int pir1State = digitalRead(pir1Pin);
  int pir2State = digitalRead(pir2Pin);

  // Read ultrasonic sensor distances
  long distance1 = readUltrasonicDistance(trigPin1, echoPin1);
  long distance2 = readUltrasonicDistance(trigPin2, echoPin2);

  // Check if any sensor detects movement
  if (pir1State == HIGH || pir2State == HIGH || distance1 < 100 || distance2 < 100) {
    // Trigger alarm and LED indicators
    digitalWrite(relayPin1, HIGH);
    digitalWrite(relayPin2, HIGH);
  } else {
    // Turn off alarm and LED indicators
    digitalWrite(relayPin1, LOW);
    digitalWrite(relayPin2, LOW);
  }

  // Small delay to avoid bouncing
  delay(100);
}

// Function to read distance from ultrasonic sensor
long readUltrasonicDistance(int trigPin, int echoPin) {
  // Send a 10us pulse to trigger pin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the echo pin, and return the distance in cm
  long duration = pulseIn(echoPin, HIGH);
  long distance = (duration / 2) / 29.1;
  return distance;
}

This code initializes the pins for the PIR sensors, ultrasonic sensors, and relay module. It continuously reads the state of the sensors and triggers the relay module to activate the alarm and