Contents
Introduction
The PIR (Passive Infrared) motion sensor detects movement by sensing changes in infrared radiation emitted by warm objects like humans and animals. It is the same technology used in commercial burglar alarms and automatic hallway lights. In this tutorial, we build a complete motion-activated alarm system.
Components Needed
- Arduino Uno
- HC-SR501 PIR Sensor
- Buzzer
- LED
- Resistor (330-ohm)
Wiring
| PIR Pin | Arduino |
|---|---|
| VCC | 5V |
| GND | GND |
| OUT | Digital Pin 2 |
Project 1: Basic Motion Detector
#define PIR_PIN 2
#define LED_PIN 13
void setup() {
Serial.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println("Warming up PIR sensor... please wait 30 seconds.");
delay(30000);
Serial.println("Ready!");
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
digitalWrite(LED_PIN, HIGH);
Serial.println("Motion Detected!");
delay(1000);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Project 2: Motion Alarm with Buzzer
#define PIR_PIN 2
#define BUZZER_PIN 8
#define LED_PIN 13
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
delay(30000); // Warm-up
}
void loop() {
if (digitalRead(PIR_PIN) == HIGH) {
for (int i = 0; i < 5; i++) {
digitalWrite(LED_PIN, HIGH); tone(BUZZER_PIN, 1000); delay(200);
digitalWrite(LED_PIN, LOW); noTone(BUZZER_PIN); delay(200);
}
}
delay(100);
}
Important Notes
- The HC-SR501 needs a 30-second warm-up period after power-on before it reliably detects motion.
- Adjust the sensitivity and delay potentiometers on the sensor to tune its range and response time.
Conclusion
With just a PIR sensor and Arduino, you can build a professional-grade security alarm system for a fraction of the cost of commercial solutions!