DIY ELECTRONICSTR

← All projects

Motion Alarm

Beginner25 min

A small home alarm that goes off when someone walks in: the HC-SR501 PIR sensor senses the change in the heat a human body gives off, the buzzer plays a two-tone siren and the red LED blinks; every alarm is counted in the Serial Monitor. It teaches reading a sensor's digital output, catching only the moment motion starts, sounding for a set time with millis(), and gathering the job into its own function. No extra library needed.

Build it step by step in the app

What you need

Wiring steps

  1. 1

    Connect the power rails (5V and GND)

    Connect the power rails (5V and GND)

    Plug a black male-male jumper wire into one of the GND pins on the Arduino's power header and connect its other end to the breadboard's top blue (−) rail. Plug a red wire into the 5V pin on the same header and connect its other end to the top red (+) rail. The PIR sensor is powered from these two rails, and the LED's resistor and the buzzer take their ground from the (−) rail.

    • Arduino GND → the top blue (−) rail (black wire)
    • Arduino 5V → the top red (+) rail (red wire)
  2. 2

    Add the warning LED and its resistor

    Add the warning LED and its resistor

    Plug the red LED's long leg (+) into column 15 in the top half of the breadboard and its short leg (−) into column 16. Put one end of the 220 Ω resistor in column 16 and the other end in the top (−) rail. Then connect the Arduino's pin 12 to column 15 with a blue jumper wire (LED_PIN 12). The resistor limits the current through the LED.

    • red LED long leg (+) → column 15, top half
    • red LED short leg (−) → column 16, top half
    • 220 Ω resistor (leg 1) → column 16, top half
    • 220 Ω resistor (leg 2) → the top blue (−) rail
    • Arduino pin 12 → column 15, top half (blue wire)
  3. 3

    Add and wire the buzzer

    Add and wire the buzzer

    Plug the passive buzzer's + leg into column 8 in the top half and its other leg into column 9; the + leg is usually the longer one and the body has a + mark. Connect the Arduino's pin 8 to column 8 with an orange jumper wire (BUZZER_PIN 8). Connect column 9 to the top (−) rail with a short black wire.

    • buzzer + leg → column 8, top half
    • buzzer − leg → column 9, top half
    • Arduino pin 8 → column 8, top half (orange wire)
    • column 9, top half → the top blue (−) rail (black wire)
  4. 4

    Connect the PIR sensor

    Connect the PIR sensor

    Push the female ends of male-female jumper wires onto the HC-SR501's three pins (take off the white dome and the pin names are printed on the board). Connect VCC to the top (+) rail with a red wire, GND to the top (−) rail with a black wire, and OUT straight to the Arduino's pin 2 with a yellow wire (PIR_PIN 2). Of the two small screws on the board, one sets how far it senses and the other how long the output stays HIGH: to start, turn the time screw fully anticlockwise. With the corner jumper on H, the output stays HIGH for as long as the motion goes on.

    • PIR sensor VCC → the top red (+) rail (red wire)
    • PIR sensor GND → the top blue (−) rail (black wire)
    • Arduino pin 2 → PIR sensor OUT (yellow wire)

Arduino sketch

// PIR, buzzer and LED pins
const int PIR_PIN = 2;
const int BUZZER_PIN = 8;
const int LED_PIN = 12;

// PIR warm-up (ms): it learns the room
// meanwhile, so keep still
const unsigned long WARM_UP = 30000;
// How long one alarm sounds (ms)
const unsigned long ALARM_TIME = 3000;

int lastState = LOW;
int alarms = 0;

void setup() {
  pinMode(PIR_PIN, INPUT);
  pinMode(BUZZER_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);

  Serial.println("PIR warming up, wait 30 s...");
  delay(WARM_UP);
  Serial.println("Alarm armed.");
}

// Siren: two tones in turn, LED blinks
void soundAlarm() {
  unsigned long start = millis();
  bool high = true;
  while (millis() - start < ALARM_TIME) {
    tone(BUZZER_PIN, high ? 880 : 660);
    digitalWrite(LED_PIN, high ? HIGH : LOW);
    high = !high;
    delay(250);
  }
  noTone(BUZZER_PIN);
  digitalWrite(LED_PIN, LOW);
}

void loop() {
  int state = digitalRead(PIR_PIN);

  // Only the moment motion starts
  if (state == HIGH && lastState == LOW) {
    alarms++;
    Serial.print("Motion detected! Alarm: ");
    Serial.println(alarms);
    soundAlarm();
  }
  lastState = state;
}

What the Serial Monitor shows

SERIAL MONITOR · 9600 BAUD
PIR warming up, wait 30 s...
Alarm armed.
Motion detected! Alarm: 1
Motion detected! Alarm: 2

What you will see

At start-up the Serial Monitor says "PIR warming up"; wait 30 seconds without moving. Once it says "Alarm armed.", walk past the sensor: the buzzer sounds a two-tone siren for 3 seconds, the red LED blinks and the alarm count is printed. While the motion goes on the alarm does not sound again; it does once the sensor has calmed down and sees motion anew. If the alarm never sounds, check that the yellow wire is on pin 2 and VCC is in the (+) rail, and really move in front of the sensor (a PIR sees heat; it cannot see through glass). If the alarm keeps sounding or stops too late, turn the time screw on the board anticlockwise.

In the app, tick off every wire, match the parts to your inventory and pick up where you left off.

Build it step by step in the app