Lights that come on when you walk past use a PIR (passive infrared) sensor. This project wires the common HC-SR501 to a Raspberry Pi with an LED as the light. When the sensor sees movement the LED comes on, and it goes off 30 seconds after the last movement. It's a short Python program using GPIO Zero, and the same idea runs everything from a cupboard light to a camera that records only when something moves.
What you need
- A Raspberry Pi, its power supply and a microSD card with Raspberry Pi OS
- An HC-SR501 PIR motion sensor
- One LED and a 330 Ω resistor (orange, orange, brown)
- A breadboard and five male-to-female jumper wires
Shopping list (8 items)
Some shop links are affiliate links (Amazon UK, Tooled Up): if you buy through one we may earn a small commission, at no extra cost to you. As an Amazon Associate I earn from qualifying purchases. It never changes which products we recommend, and no shop has paid to be included.
Everything for this build, in one place. Each link goes to a page we checked had the part in stock.
- 1×Raspberry Pi 5 (any memory size)Both Pi projects run on any Raspberry Pi with a 40-pin header, including a Pi 4 or Zero 2 W. The 8 GB Pi 5 is linked, but 2 or 4 GB is plenty here.
- 1×Raspberry Pi 27 W USB-C power supplyThe official supply for the Pi 5 (5 V, 5 A). A Pi 4 needs the 15 W supply instead.
- 1×microSD card, 32 GB, A1 ratedHolds Raspberry Pi OS and your files. A1 or A2 cards are quicker at the small reads and writes an operating system does.
- 1×HC-SR501 PIR motion sensorThe common module with a white dome and two adjustment knobs. A pack of five.
- 1×5 mm LEDs, assorted coloursStandard LEDs, not the 'built-in resistor' kind.
- 1×Resistor kit, 1/4 W (0.25 W) assorted valuesCovers every resistor in these projects.
- 1×Half-size breadboard (400 points)
- 1×Jumper wire kit (male-male, male-female and female-female)40 of each kind. Male-female wires go from a Raspberry Pi's pins to a breadboard.
- FreeRaspberry Pi ImagerWrites Raspberry Pi OS to the card. Nothing to buy.
Each button opens that shop's page for the part, checked when we wrote the list; if one shop is out of stock, try the next. Any equivalent part with the same value works.
How a PIR sensor works
Everything warm gives off infrared light, and people are warmer than most rooms. A PIR sensor has two infrared-sensitive halves side by side. When a warm body moves across its view, one half sees it before the other, and that difference triggers the output. A person standing completely still can disappear from view, which is why motion lights sometimes go off while you're in the room. The white dome is a lens that splits the view into many zones, so even small movements cross from one zone to the next. The HC-SR501's output gives 3.3 V when it detects movement, which is safe for the Pi's GPIO pins.
Set up the sensor
- Trigger jumper: set it to H (repeat trigger). The output then stays on while movement continues.
- Time knob: turn it fully anticlockwise, to its shortest time. The program handles the 30-second delay, which is easier to change in code.
- Sensitivity knob: start in the middle. Clockwise usually increases the range.
Knob and jumper positions vary a little between makers, so check the seller's notes. The pin names are often printed under the dome, which pulls straight off.
The circuit
| Part | Raspberry Pi |
|---|---|
| PIR VCC | 5 V, physical pin 2 |
| PIR OUT | GPIO 17, physical pin 11 |
| PIR GND | GND, physical pin 6 |
| LED long leg (anode), through 330 Ω | GPIO 27, physical pin 13 |
| LED short leg (cathode) | GND, physical pin 14 |
The HC-SR501 needs 5 V or more to power it, but its output is 3.3 V, so it connects straight to a GPIO pin. With 3.3 V from the pin and about 2 V across a red LED, 330 Ω gives about 4 mA, gentle on the pin and plenty for an indicator. Our Raspberry Pi pinout shows where each pin is.
- Shut the Pi down and unplug it.
- Wire the sensor's three pins to pins 2, 11 and 6 with male-to-female jumper wires.
- Put the LED and resistor in the breadboard and wire them to pins 13 and 14.
Setting up the Pi
You need a Pi running Raspberry Pi OS that you can reach from your computer. Our Raspberry Pi first steps guide covers writing the card with Raspberry Pi Imager, turning on SSH and logging in. Raspberry Pi OS Lite, the version without a desktop, is enough for this project. Always unplug the Pi, or shut it down with sudo shutdown now, before changing the wiring.
The program
Save this as motion_light.py and run it with python3 motion_light.py. The sensor needs up to a minute to settle after power-up, during which it may trigger by itself, so the program waits for it to go quiet before it starts. Press Ctrl+C to stop.
# Motion-activated light: a PIR sensor on GPIO 17 turns an LED on GPIO 27 on,
# and it goes off 30 seconds after the last movement.
from gpiozero import MotionSensor, LED
from datetime import datetime
import time
ON_TIME = 30 # seconds the light stays on after the last movement
pir = MotionSensor(17)
light = LED(27)
last_motion = 0.0
print("Waiting for the sensor to settle...")
pir.wait_for_no_motion()
print("Ready")
while True:
if pir.motion_detected:
if not light.is_lit:
print(f"{datetime.now():%H:%M:%S} movement: light on")
light.on()
last_motion = time.monotonic()
elif light.is_lit and time.monotonic() - last_motion > ON_TIME:
light.off()
print(f"{datetime.now():%H:%M:%S} no movement for {ON_TIME} s: light off")
time.sleep(0.1)
How it works
- GPIO Zero.
MotionSensor(17)watches the sensor's output on GPIO 17, andLED(27)controls the LED on GPIO 27. Our GPIO Zero guide covers both. - Restarting the timer. Every time the sensor sees movement, the program notes the time. The light only goes off once 30 seconds have passed since the last movement, so walking about keeps it on.
- Why
time.monotonic(). It's a clock that only ever counts up, so the timing still works if the Pi's clock changes, for example when it sets itself from the internet after booting. - Tested. We ran this program with GPIO Zero's mock pins and a simulated sensor, checking that the light comes on with movement, stays on through repeated movement and goes off 30 seconds after the last.
Troubleshooting
- The light comes on with nobody there: PIR sensors react to warm air too. Keep it away from radiators, heaters, sunny windows and air vents, and turn the sensitivity down.
- It never triggers: wait a full minute after power-up, check VCC is on 5 V, and make sure the OUT wire is on physical pin 11.
- It goes off while you're still there: raise
ON_TIME, or point the sensor where people move across it rather than towards it. - It stays on far longer than 30 seconds: the time knob is turned up, so the sensor itself holds the output on. Turn it fully anticlockwise.
Take it further
- Start it at boot with a
@rebootline incrontab -e, as in our temperature logger project. - Only turn the light on after dark, by checking the time, or by adding a light sensor.
- Take a photo with a Raspberry Pi camera whenever movement is seen.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.