Engineering · Raspberry Pi · Project 5 of 47 · Sensors · Beginner
Motion alarm with a PIR sensor
A PIR sensor sees body heat moving. When it triggers, an LED lights and the buzzer beeps five times.

Parts in this circuit
| Part | Type |
|---|---|
| PIR sensor | PIR sensor |
| Alarm LED | LED |
| Buzzer | Buzzer |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Red | PIR sensor - VCC | Pin 2 (5V) |
| 2 | Coloured | PIR sensor - OUT | Pin 7 (GPIO4) |
| 3 | Black | PIR sensor - GND | Pin 6 (GND) |
| 4 | Coloured | Alarm LED - anode + | Pin 11 (GPIO17) |
| 5 | Black | Alarm LED - cathode - | Pin 9 (GND) |
| 6 | Coloured | Buzzer - + | Pin 12 (GPIO18) |
| 7 | Black | Buzzer - - | Pin 14 (GND) |
Python code (gpiozero)
from gpiozero import MotionSensor, LED, Buzzer
from time import sleep
pir = MotionSensor(4)
led = LED(17)
buzzer = Buzzer(18)
print("Sensor warming up...")
sleep(2)
print("Armed. Click the sensor dome to wave a hand.")
while True:
pir.wait_for_motion()
print("Motion detected!")
led.on()
buzzer.beep(0.1, 0.1, n=5) # five short beeps
pir.wait_for_no_motion()
led.off()
print("All clear")
How the code works
- A PIR module outputs 3.3 V when it sees movement and 0 V when it does not.
- MotionSensor(4) reads that signal. wait_for_motion() sleeps until it goes high.
- n=5 makes the buzzer stop by itself after five beeps.
You will learn
- Digital sensors with a 3-wire connection
- Why sensors need a warm-up time
- Combining several outputs
Try this
- Keep the LED on for 10 seconds after the last motion.
- Print the time of each detection using the time module.
- Add a button that arms and disarms the alarm.
On a real Pi
- A PIR module needs 5 V on its supply pin, but its output is 3.3 V, safe for the Pi.
- Most PIR modules have two small trimmers for sensitivity and hold time, and a jumper for retrigger mode.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 5 mm LED, red | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 330 ohm resistor, 1/4 W One per LED | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Active buzzer, 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | HC-SR501 PIR motion sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.