Arduino UNO-Based Motion and Distance Detection System with Relay-Controlled Alarm and LED Indicator
View and Edit Project In Cirkit Designer
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
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
HC-SR05 (Ultrasonic Sensor)
- Description: Ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND, OUT
HC-SR05 (Ultrasonic Sensor)
- Description: Ultrasonic distance sensor.
- Pins: VCC, TRIG, ECHO, GND, OUT
Red Light 220VAC
- Description: 220V AC red light indicator.
- Pins: -, +
LDR (Light Dependent Resistor)
- Description: Light sensor.
- Pins: A0, D0, GND, VCC
2-Channel Relay
- Description: Relay module with two channels.
- Pins: NC, COM, NO, VCC, IN2, IN1, GND
12V Power Supply
- Description: 12V DC power supply.
- Pins: +, -
Power 220V
- Description: 220V AC power source.
- Pins: hot wire, neutral wire
PIR/Motion Sensor
- Description: Passive Infrared (PIR) motion sensor.
- Pins: GND, OUTPUT, VCC
PIR/Motion Sensor
- Description: Passive Infrared (PIR) motion sensor.
- Pins: GND, OUTPUT, VCC
Wiring Details
Arduino UNO
- GND connected to:
- HC-SR05 (GND)
- HC-SR05 (GND)
- 5V connected to:
- HC-SR05 (VCC)
- HC-SR05 (VCC)
- D9 connected to:
- HC-SR05 (TRIG)
- D4 connected to:
- HC-SR05 (ECHO)
- D10 connected to:
- HC-SR05 (ECHO)
- D5 connected to:
- HC-SR05 (TRIG)
- A0 connected to:
- LDR (A0)
- D13 connected to:
- PIR/Motion Sensor (OUTPUT)
- D12 connected to:
- PIR/Motion Sensor (OUTPUT)
HC-SR05 (Ultrasonic Sensor)
- GND connected to:
- Arduino UNO (GND)
- VCC connected to:
- Arduino UNO (5V)
- TRIG connected to:
- Arduino UNO (D9)
- ECHO connected to:
- Arduino UNO (D10)
HC-SR05 (Ultrasonic Sensor)
- GND connected to:
- Arduino UNO (GND)
- VCC connected to:
- Arduino UNO (5V)
- TRIG connected to:
- Arduino UNO (D5)
- ECHO connected to:
- Arduino UNO (D4)
Red Light 220VAC
- - connected to:
- 2-Channel Relay (COM)
- + connected to:
- Power 220V (neutral wire)
LDR
- GND connected to:
- 12V Power Supply (-)
- VCC connected to:
- 12V Power Supply (+)
- A0 connected to:
- Arduino UNO (A0)
2-Channel Relay
- GND connected to:
- 12V Power Supply (-)
- VCC connected to:
- 12V Power Supply (+)
- COM connected to:
- Red Light 220VAC (-)
- NC connected to:
- Power 220V (hot wire)
12V Power Supply
- - connected to:
- PIR/Motion Sensor (GND)
- LDR (GND)
- PIR/Motion Sensor (GND)
- 2-Channel Relay (GND)
- + connected to:
- PIR/Motion Sensor (VCC)
- LDR (VCC)
- PIR/Motion Sensor (VCC)
- 2-Channel Relay (VCC)
Power 220V
- neutral wire connected to:
- Red Light 220VAC (+)
- hot wire connected to:
- 2-Channel Relay (NC)
PIR/Motion Sensor
- GND connected to:
- 12V Power Supply (-)
- VCC connected to:
- 12V Power Supply (+)
- OUTPUT connected to:
- Arduino UNO (D13)
PIR/Motion Sensor
- GND connected to:
- 12V Power Supply (-)
- VCC connected to:
- 12V Power Supply (+)
- OUTPUT connected to:
- Arduino UNO (D12)
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