Ultimate Guide: Using a PIR Motion Sensor with Arduino for Motion Detection
- 11 Feb, 2025
Integrating a Passive Infrared (PIR) motion sensor with an Arduino enables motion detection for applications like security systems and automation.
Components Required:
Component | Quantity |
---|---|
Arduino UNO | 1 |
PIR Motion Sensor (e.g., HC-SR501) | 1 |
LED | 1 |
330Ω Resistor | 1 |
Breadboard | 1 |
Jumper Wires | Varies |
Circuit Setup:
PIR Sensor Connections:
- VCC to 5V on Arduino
- GND to GND on Arduino
- OUT to Digital Pin 2 on Arduino
LED Connections:
- Anode (longer leg) to Digital Pin 13 via 330Ω Resistor
- Cathode (shorter leg) to GND
Adjusting the PIR Sensor:
Use the potentiometers to adjust:
- Sensitivity: Controls the detection range.
- Time Delay: Determines how long the output remains high after detecting motion.
Arduino Code:
int ledPin = 13; int pirPin = 2; int pirState = LOW; int val = 0; void setup() { pinMode(ledPin, OUTPUT); pinMode(pirPin, INPUT); Serial.begin(9600); } void loop() { val = digitalRead(pirPin); if (val == HIGH) { digitalWrite(ledPin, HIGH); if (pirState == LOW) { Serial.println("Motion detected!"); pirState = HIGH; } } else { digitalWrite(ledPin, LOW); if (pirState == HIGH) { Serial.println("Motion ended!"); pirState = LOW; } } }
Operation:
Once uploaded, allow the PIR sensor to calibrate for 30 seconds. The LED will turn on when motion is detected and turn off after the delay time.
Adjust sensitivity and delay settings for different applications.