Engineering · Raspberry Pi · Project 14 of 47 · Home automation · Intermediate
Automatic porch light
The porch lamp comes on only when it is dark AND somebody is moving nearby, and stays on for a few seconds. It combines two sensors and a relay.

CAUTION: 230 V single-phase mains. The relay in this project switches a mains load (Live and Neutral, 230 V AC, 50 Hz in India). Mains can kill. Practise with the simulator or a low-voltage lamp. Build the mains side only with a qualified electrician, in a closed insulated box, behind a fuse or MCB and a 30 mA RCCB. Never touch the relay screw terminals while anything is connected.
Parts in this circuit
| Part | Type |
|---|---|
| PIR sensor | PIR sensor |
| LDR | Light sensor |
| Relay and lamp | Relay module |
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 15 (GPIO22) |
| 3 | Black | PIR sensor - GND | Pin 6 (GND) |
| 4 | Orange | LDR - 3.3 V | Pin 1 (3V3) |
| 5 | Coloured | LDR - signal | Pin 7 (GPIO4) |
| 6 | Black | LDR - GND | Pin 9 (GND) |
| 7 | Coloured | Relay and lamp - IN | Pin 11 (GPIO17) |
| 8 | Black | Relay and lamp - GND | Pin 14 (GND) |
| 9 | Red | Relay and lamp - VCC | Pin 4 (5V) |
Python code (gpiozero)
from gpiozero import MotionSensor, LightSensor, OutputDevice
from time import sleep, time
pir = MotionSensor(22)
ldr = LightSensor(4)
lamp = OutputDevice(17, active_high=False)
DARK_BELOW = 0.3 # darker than 30 % light
HOLD = 8 # keep the lamp on for 8 seconds
off_at = 0
print("Set Daylight below 30 % and click the PIR dome")
while True:
is_dark = ldr.value < DARK_BELOW
if is_dark and pir.motion_detected:
if not lamp.value:
print("Dark and movement: lamp ON")
lamp.on()
off_at = time() + HOLD # restart the timer
if lamp.value and time() > off_at:
lamp.off()
print("Timer finished: lamp OFF")
sleep(0.2)
How the code works
- The lamp needs both facts to be true: it is dark (ldr.value is low) and there is movement (the PIR output is high).
- Each time movement is seen, off_at is moved forward by HOLD seconds. The lamp only goes off when the time passes off_at.
- The loop checks five times a second. sleep(0.2) keeps the page and a real Pi relaxed.
You will learn
- Combining conditions with and
- A hold timer using time()
- Why automatic lights use two sensors
Try this
- In daylight, click the PIR dome: nothing happens. Why?
- Change HOLD to 20 seconds.
- Make the lamp also stay on for 2 seconds after the light level rises.
On a real Pi
- Use a PIR module with a time and sensitivity trimmer, and set it to the shortest time. Your code then controls the real hold time.
- Keep the LDR away from the lamp light, or it will switch the lamp off by itself.
- Mains wiring must be done by a qualified electrician.
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 | 1-channel 5 V relay module with optocoupler 10 A 250 V AC contacts | 1 | Robu.in Amazon.in ThinkRobotics |
| 5 | 230 V LED bulb (9 W) with holder and wire | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Closed ABS enclosure (IP54) for the mains side | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | 3-way screw terminal block (rated 10 A) | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | MCB 6 A single pole and RCCB 30 mA (by an electrician) Mains protection | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | HC-SR501 PIR motion sensor | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | LDR (GL5528) photoresistor | 1 | Robu.in Amazon.in ThinkRobotics |
| 11 | 1 µF capacitor | 1 | Robu.in Amazon.in ThinkRobotics |
| 12 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 13 | 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.