Illustration: at night in a hallway the motion sensor faces the door; a cat walks past and the red LED is lit, with a buzzer beside it.Home and IoT
Illustration (made with AI): roughly what the finished project looks like.

A Motion Alarm

A PIR sensor notices a warm body moving. As soon as someone walks into the room, a siren sounds and a red light flashes.

Difficulty
Beginner
Time
30 min
Age
10+
Parts
6, 6 on the site

Written by the RoboHub team. A question? Write to us.

Variations

Choose a variation and the parts list updates.

Instructions

  1. Connect the sensor

    The HC-SR501 has three pins: connect VCC to 5V, GND to ground and OUT to pin 2. The sensor gives a 3.3 V signal: if the Uno R4 doesn't notice movement, put a logic level shifter between OUT and pin 2.

    The sensor gives a 3.3 V signal. If the Uno R4 doesn't notice movement, put a level shifter between OUT and D2. D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • HC-SR501 PIR sensor VCC → Arduino 5V
    • HC-SR501 PIR sensor OUT → Arduino D2
    • HC-SR501 PIR sensor GND → Arduino GND
  2. Siren and light

    Connect a red LED through a 330 Ω resistor to pin 7, and the buzzer's “+” to pin 8 and “−” to ground.

    The sensor gives a 3.3 V signal. If the Uno R4 doesn't notice movement, put a level shifter between OUT and D2. D2 is the board's digital pin 2, GND is ground. Red wires are power, black ground, the other colours signals (on a real servo the ground wire is often brown). In the steps, what a step adds is in colour and what is already connected in grey.

    • Red LED + → 330 ΩArduino D7
    • Red LED − → Arduino GND
    • Buzzer + → Arduino D8
    • Buzzer − → Arduino GND
  3. Upload the code

    After power-on the sensor needs about 30 seconds to settle to the room. Don't move in front of it during that time.

    Arduino C++
    const int PIR = 2, LED = 7, BUZZER = 8;
    
    void setup() {
      pinMode(PIR, INPUT);
      pinMode(LED, OUTPUT);
      Serial.begin(9600);
      Serial.println("Warming up the sensor...");
      delay(30000);                        // the PIR needs about 30 s after power-on
      Serial.println("Armed");
    }
    
    void loop() {
      if (digitalRead(PIR) == HIGH) {      // movement
        Serial.println("Motion!");
        for (int i = 0; i < 10; i++) {     // 2 seconds of siren
          digitalWrite(LED, HIGH);
          tone(BUZZER, 1800);  delay(100);
          digitalWrite(LED, LOW);
          tone(BUZZER, 1200);  delay(100);
        }
        noTone(BUZZER);
      }
      delay(50);
    }
  4. Tune the sensor

    The sensor has two small knobs: one sets the sensitivity (up to 7 metres), the other how long the signal stays on. For quick testing, turn the time knob fully anticlockwise.

    Illustration: a PIR motion sensor, a lit red LED and a buzzer.

    Illustration, made with AI

  • Home and IoT
    Intermediate1 h12+

    A Code Lock with a Keypad

    Type the secret code and press #: with the right code the green light comes on and a servo slides the bolt open, with the wrong one the buzzer beeps three times.

    7/7 parts on the site
  • Games
    Beginner45 min10+

    A Two-Player Reaction Game

    An LED lights at a random moment. Whoever presses their button first wins. Built from Student Kit parts in 45 minutes.

    6/6 parts on the site
  • Lights and displays
    Beginner45 min10+

    Pedestrian Crossing with a Button

    Cars have green until a pedestrian presses the button. Then the cars get red, the pedestrians get green and a buzzer ticks, just like at a real crossing.

    6/6 parts on the site