Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Motion alarm with a PIR sensor: the board, its power supply and the parts wired to the 40-pin header

Parts in this circuit

PartType
PIR sensorPIR sensor
Alarm LEDLED
BuzzerBuzzer

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.

#ColourFrom (part and leg)To (header pin)
1RedPIR sensor - VCCPin 2 (5V)
2ColouredPIR sensor - OUTPin 7 (GPIO4)
3BlackPIR sensor - GNDPin 6 (GND)
4ColouredAlarm LED - anode +Pin 11 (GPIO17)
5BlackAlarm LED - cathode -Pin 9 (GND)
6ColouredBuzzer - +Pin 12 (GPIO18)
7BlackBuzzer - -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

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.

#ItemQtyBuy / compare
1Raspberry Pi 4 Model B
about 4000 to 9000 INR
1Robu.in Amazon.in ThinkRobotics
2Official 15 W USB-C power supply (5.1 V, 3 A)
Do not use an old phone charger
1Robu.in Amazon.in ThinkRobotics
3microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS)
And a card reader to write it
1Robu.in Amazon.in ThinkRobotics
45 mm LED, red1Robu.in Amazon.in ThinkRobotics
5330 ohm resistor, 1/4 W
One per LED
1Robu.in Amazon.in ThinkRobotics
6Active buzzer, 5 V1Robu.in Amazon.in ThinkRobotics
7HC-SR501 PIR motion sensor1Robu.in Amazon.in ThinkRobotics
8Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
9Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 4. Door alarm with a buzzerAll projects6. Distance meter with an LED bar →

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.