Fareed Mohmed← Project library

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.

▶ Open in the simulator

Raspberry Pi simulator drawing of the circuit for Automatic porch light: the board, its power supply and the parts wired to the 40-pin header
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

PartType
PIR sensorPIR sensor
LDRLight sensor
Relay and lampRelay 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.

#ColourFrom (part and leg)To (header pin)
1RedPIR sensor - VCCPin 2 (5V)
2ColouredPIR sensor - OUTPin 15 (GPIO22)
3BlackPIR sensor - GNDPin 6 (GND)
4OrangeLDR - 3.3 VPin 1 (3V3)
5ColouredLDR - signalPin 7 (GPIO4)
6BlackLDR - GNDPin 9 (GND)
7ColouredRelay and lamp - INPin 11 (GPIO17)
8BlackRelay and lamp - GNDPin 14 (GND)
9RedRelay and lamp - VCCPin 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

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.

#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
41-channel 5 V relay module with optocoupler
10 A 250 V AC contacts
1Robu.in Amazon.in ThinkRobotics
5230 V LED bulb (9 W) with holder and wire1Robu.in Amazon.in ThinkRobotics
6Closed ABS enclosure (IP54) for the mains side1Robu.in Amazon.in ThinkRobotics
73-way screw terminal block (rated 10 A)1Robu.in Amazon.in ThinkRobotics
8MCB 6 A single pole and RCCB 30 mA (by an electrician)
Mains protection
1Robu.in Amazon.in ThinkRobotics
9HC-SR501 PIR motion sensor1Robu.in Amazon.in ThinkRobotics
10LDR (GL5528) photoresistor1Robu.in Amazon.in ThinkRobotics
111 µF capacitor1Robu.in Amazon.in ThinkRobotics
12Dupont jumper wires, male-to-female, 20 cm (pack of 40)1Robu.in Amazon.in ThinkRobotics
13Breadboard 830 points
For resistors and shared GND / 5 V
1Robu.in Amazon.in ThinkRobotics
← 13. Wall switch with a relayAll projects15. Thermostat fan with hysteresis →

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.