Guard your room, desk or snack drawer. Press the button to arm the alarm, and the red LED stays on. If the PIR sensor sees someone moving, the buzzer beeps and the LED flashes for five seconds. Press the button again to disarm it.
What you need
- UNO R3 board and USB cable
- PIR motion sensor (HC-SR501)
- Active buzzer
- Red LED and 1 resistor, 220 Ω
- Push button
- Breadboard and jumper wires (all from the UNO R3 Starter Kit)
Wiring

| From | To |
|---|---|
| PIR VCC | 5V (breadboard + rail) |
| PIR OUT | UNO pin 2 |
| PIR GND | GND (breadboard − rail) |
| Buzzer + (longer leg) | UNO pin 8 |
| Buzzer − | GND |
| UNO pin 6 | 220 Ω resistor, then the LED's long leg |
| LED short leg | GND |
| Button, one side | UNO pin 3 |
| Button, other side | GND |
The PIR sensor's pins are on its underside. Connect them with the Dupont wires from the kit. The active buzzer has a "+" mark on top and a longer + leg.
Build it step by step
- Connect 5V and GND to the breadboard rails.
- Wire the PIR sensor to the rails and its OUT pin to pin 2.
- Add the buzzer (pin 8), the LED with its resistor (pin 6) and the button (pin 3).
- Upload the code and open the Serial Monitor at 9600 baud.
- Keep still during the 30-second warm-up, then press the button to arm the alarm.
- Walk in front of the sensor to set it off.
The code
const int PIR_PIN = 2;
const int BUTTON_PIN = 3;
const int LED_PIN = 6;
const int BUZZER_PIN = 8;
const unsigned long ALARM_TIME = 5000; // ms
bool armed = false;
bool lastButton = HIGH;
unsigned long alarmEnd = 0; // 0 = no alarm running
void setup() {
Serial.begin(9600);
pinMode(PIR_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // pressed = LOW
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
Serial.println("Warming up the PIR sensor (30 seconds)...");
delay(30000);
Serial.println("Ready. Press the button to arm.");
}
void loop() {
// Arm or disarm on each button press
bool button = digitalRead(BUTTON_PIN);
if (lastButton == HIGH && button == LOW) {
armed = !armed;
alarmEnd = 0;
digitalWrite(BUZZER_PIN, LOW);
Serial.println(armed ? "Armed" : "Disarmed");
if (armed) { // short confirmation beep
digitalWrite(BUZZER_PIN, HIGH);
delay(100);
digitalWrite(BUZZER_PIN, LOW);
}
delay(200); // ignore button bounce
}
lastButton = button;
// Start the alarm when motion is detected while armed
if (armed && alarmEnd == 0 && digitalRead(PIR_PIN) == HIGH) {
alarmEnd = millis() + ALARM_TIME;
Serial.println("Motion detected!");
}
if (alarmEnd != 0 && millis() < alarmEnd) {
bool on = (millis() / 200) % 2; // beep and flash 2.5 times a second
digitalWrite(BUZZER_PIN, on);
digitalWrite(LED_PIN, on);
} else {
alarmEnd = 0;
digitalWrite(BUZZER_PIN, LOW);
digitalWrite(LED_PIN, armed ? HIGH : LOW); // steady light = armed
}
}
How it works
The PIR sensor notices the infrared heat of a moving person and sets OUT to HIGH. The sketch only reacts while armed is true, and then sets an end time five seconds in the future. Until that time, the buzzer and LED switch on and off based on millis(), so the button still works during the alarm. Detecting the moment the button goes from HIGH to LOW means one press switches the state only once.
Make it your own
- Add a 10-second delay after arming so you can leave the room first.
- Count how many times the alarm went off and print it.
- Combine this with the RFID project to disarm the alarm with your card.
Troubleshooting
- The alarm goes off by itself: keep the sensor away from heaters and windows, and turn its sensitivity knob down.
- It never triggers: wait for the warm-up and check that OUT is on pin 2.
-
The buzzer only clicks: you're using the passive buzzer. Use the active one, or use
tone(BUZZER_PIN, 2000).
Learn the basics first
New to these parts? These lessons explain them step by step: Detect motion with a PIR sensor and Make a tilt alarm with a buzzer.
0 comments