PIR Motion Sensor Alarm with Arduino (HC-SR501)
Build a motion alarm with an HC-SR501 PIR sensor, Arduino Uno, LED and buzzer. Pin table, commented code, sensor tuning and troubleshooting.
On this page
A motion alarm is one of the most satisfying beginner projects. Walk in front of the sensor and a buzzer sounds, an LED lights up, and the Serial Monitor prints a message. The HC-SR501 PIR module does the hard work of detecting movement, so the Arduino code stays short. In this tutorial you will wire the module, understand its two potentiometers and jumper, and add a proper arming delay so the alarm does not go off while you walk away.
Safety note
This project runs on 5 V from a USB cable and is safe to build. It is a learning project, not a certified burglar alarm. Do not rely on it to protect valuables.
What you need
Scroll sideways to see all columns.
| Item | Qty | Notes |
|---|---|---|
| Arduino Uno (or clone) + USB cable | 1 | CH340 clones need the CH340 driver on your PC |
| HC-SR501 PIR motion sensor module | 1 | White dome (Fresnel lens), 3 pins |
| Active buzzer, 5 V | 1 | Use an active piezo type rated at 20 mA or less at 5 V for direct D8 drive |
| LED 5 mm | 1 | Long leg = anode (+) |
| Resistor 220 Ω | 1 | LED resistor |
| Jumper wires | 6 | Male-to-female for the sensor pins |
| Breadboard | 1 | |
| Digital multimeter | 1 | For the checks below |
How the HC-SR501 works
PIR stands for passive infrared. Under the white dome sits a sensor that detects changes in infrared heat. Every warm body, including a person, gives off infrared. When someone moves across the sensor's field of view, the pattern of heat the sensor sees changes, and the module's output pin goes HIGH. A still person does not trigger it for long, which is why PIR sensors detect movement and not presence.
The module has three pins: VCC, OUT and GND. The order is not the same on every board, so read the tiny labels on the PCB or lift the dome to see them. Supply is 5 V (the module accepts about 5 V to 20 V because of an onboard regulator). The OUT pin gives about 3.3 V when triggered and about 0 V at rest. An Arduino Uno reads 3.3 V as HIGH, so you can connect OUT directly to a digital pin.
Controls on the back of the module:
- Sensitivity pot: sets detection range, roughly 3 m to 7 m. Turn it clockwise to increase the range on most boards.
- Time-delay pot: sets how long OUT stays HIGH after motion, from a few seconds to a few minutes. Start with it turned fully anticlockwise (shortest).
- Trigger jumper (H / L): in H (repeatable) mode OUT stays HIGH while you keep moving. In L (single) mode it goes HIGH once for the delay time, then LOW even if you are still moving. Use H for an alarm.
When you power the module, it needs about 30 to 60 seconds to settle. It may trigger randomly in this time. This is normal.
Connection table
Scroll sideways to see all columns.
| Component pin | Arduino Uno pin |
|---|---|
| HC-SR501 VCC | 5V |
| HC-SR501 GND | GND |
| HC-SR501 OUT | D2 |
| Buzzer + (long leg) | D8 |
| Buzzer − | GND |
| LED anode (long leg) via 220 Ω | D7 |
| LED cathode (short leg) | GND |
Signal path in one line:
Movement → PIR sensor → OUT (3.3 V) → D2 → code → D8 → buzzer, D7 → 220 Ω → LED
For direct drive from D8, use a 5 V active piezo buzzer drawing 20 mA or less. For higher current or voltage, use a suitably rated transistor and supply with a common ground.
Step-by-step build
- Place the buzzer and LED on the breadboard. Connect both negative legs to the GND rail and connect the rail to Arduino GND.
- Connect the LED anode through the 220 Ω resistor to D7, and the buzzer + to D8.
- Connect the PIR VCC to 5V, GND to GND and OUT to D2 using jumper wires.
- Set the PIR jumper to H, the time-delay pot fully anticlockwise, and the sensitivity pot to the middle.
- Upload the sketch below and open Serial Monitor at 9600 baud.
- Keep still and wait one minute for warm-up. Then wave your hand in front of the dome.
The code
// ElectroDai - PIR motion alarm (HC-SR501)
// PIR OUT -> D2, LED (via 220 ohm) -> D7, active buzzer -> D8
const int PIR_PIN = 2;
const int LED_PIN = 7;
const int BUZZER_PIN = 8;
const unsigned long WARMUP_MS = 60000; // PIR settling time at power-up
const unsigned long ALARM_MS = 5000; // how long the alarm stays on after PIR OUT goes LOW
unsigned long alarmStart = 0;
bool alarmOn = false;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.begin(9600);
Serial.println("Warming up the PIR sensor (60 s). Keep still...");
// Blink the LED during warm-up so you know the alarm is not ready
unsigned long start = millis();
while (millis() - start < WARMUP_MS) {
digitalWrite(LED_PIN, (millis() / 500) % 2);
delay(10);
}
digitalWrite(LED_PIN, LOW);
Serial.println("Alarm armed.");
}
void loop() {
unsigned long now = millis();
// Motion detected: start (or restart) the alarm
if (digitalRead(PIR_PIN) == HIGH) {
if (!alarmOn) Serial.println("Motion detected!");
alarmOn = true;
alarmStart = now;
}
if (alarmOn) {
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
// Stop after PIR OUT has been LOW for ALARM_MS
if (now - alarmStart > ALARM_MS) {
alarmOn = false;
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
Serial.println("Alarm stopped.");
}
}
}
The alarm restarts its 5 second timer every time the PIR still reports motion, so it keeps sounding while someone is moving in front of it.
Testing with a multimeter
- Supply: between PIR VCC and GND you should read about 5 V.
- OUT at rest: after warm-up, with no movement, OUT to GND should be close to 0 V.
- OUT on motion: hold the probes on OUT and GND and wave your hand. You should see about 3.3 V (a little higher or lower on some boards) for the delay time.
- Buzzer pin: when the alarm sounds, D8 to GND reads about 5 V.
- LED branch: the voltage across the 220 Ω resistor should be about 3 V when the LED is lit.
Troubleshooting
Scroll sideways to see all columns.
| Symptom | Likely cause | Fix |
|---|---|---|
| Alarm triggers at power-up | PIR warm-up | Wait one minute; the sketch already waits |
| Never triggers | Wrong pin order (VCC and GND swapped) or sensitivity too low | Read the PCB labels; turn the sensitivity pot up |
| Triggers randomly | Fan, heater, sunlight or a draft near the sensor | Move the sensor away from heat sources and windows |
| OUT stays HIGH a long time | Time-delay pot turned up | Turn it anticlockwise |
| Only fires once while moving | Jumper in L mode | Move the jumper to H |
| Buzzer is weak or silent | Passive buzzer (needs a tone signal) | Use an active buzzer or use tone() |
| Module gets warm | Reversed supply | Disconnect and check polarity |
FAQ
Can the HC-SR501 run from the 3.3 V pin?
Not reliably. The module expects about 5 V or more at VCC. Use the 5V pin on an Arduino Uno.
Why does it not detect someone standing still?
A PIR looks for changes in infrared. A completely still person does not change the pattern, so the output drops after the delay time.
Can I use a PIR sensor in a sunny room in Nepal?
Direct sun and warm drafts cause false triggers. Mount the sensor indoors, away from windows and heaters.
Can I send an SMS or notification?
Yes, with an ESP32 and a service such as Blynk. See our ESP32 Blynk tutorial.
Next step: add a safety sensor in MQ-2 Gas Leak Alarm with Arduino and Buzzer.