PIR motion alarm

Arm the alarm with a button, and the buzzer and light go off whenever the PIR sensor detects someone moving.
  • Level:Beginner
  • Duration: 45 min
  • Ages 10+
  • Platform: Arduino
Community project EEMENTOR team
PIR motion alarm – EEMENTOR STEM™ project

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

Breadboard wiring diagram: PIR motion alarm
Breadboard view. Check each connection against the wiring table below.
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

  1. Connect 5V and GND to the breadboard rails.
  2. Wire the PIR sensor to the rails and its OUT pin to pin 2.
  3. Add the buzzer (pin 8), the LED with its resistor (pin 6) and the button (pin 3).
  4. Upload the code and open the Serial Monitor at 9600 baud.
  5. Keep still during the 30-second warm-up, then press the button to arm the alarm.
  6. 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

Leave a comment

Please note, comments need to be approved before they are published.

Build it yourself

Get the parts used in this project, tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the parts

More projects

  • Pulse visualiser

    • Level:Intermediate
    • Duration: 45 min
    • Platform: Arduino

    by EEMENTOR team

  • Flame alarm

    • Level:Beginner
    • Duration: 40 min
    • Platform: Arduino

    by EEMENTOR team

Community projects

Built something awesome? Share it.

Send us your project and we'll feature it in the hub, credited to you, so other makers can learn from it.

  1. Sign in and tell us about your build
  2. Our team reviews your submission
  3. We publish it in the hub, credited to you