Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA PIR sensor outputs high when it sees movement. The program turns a relay on at the first movement and keeps it on until 20 seconds pass with no movement, restarting the timer on each new one. It uses time.ticks_ms so the Pico can keep watching the sensor all the time. A low-voltage LED strip on its own supply is the safest first load.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
USB power adapter. Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
main.pyimport time
from machine import Pin
pir = Pin(16, Pin.IN)
relay = Pin(15, Pin.OUT, value=1) # 1 = off for an active-low relay module
OFF_DELAY_MS = 20000
last_motion = 0
light_on = False
while True:
now = time.ticks_ms()
if pir.value() == 1:
last_motion = now
if not light_on:
light_on = True
relay.value(0)
if light_on and time.ticks_diff(now, last_motion) > OFF_DELAY_MS:
light_on = False
relay.value(1)
time.sleep_ms(50)
Move the PIR away from heaters, fans and windows.
Raise OFF_DELAY_MS or the PIR's hold-time knob.
Swap 0 and 1 for the relay.
Yes, add a BH1750 or an LDR and require darkness.
About 3 to 7 metres.
Because the Pico would miss movement while sleeping.
5 parts: Raspberry Pi Pico, HC-SR501 PIR Motion Detector Sensor Module, 5V Relay Module with Optocoupler, Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 45 minutes. The build on this page has 6 steps.
No. This page has the complete, tested code (main.py).
About ₹602 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
A restartable off-timer; Reading a PIR in MicroPython; Switching a relay safely.
Anything connected to 230 V mains must be installed by a qualified electrician in a closed, earthed enclosure. Test with a low-voltage lamp first.
Ideas that reuse this build: ESP32 PIR light timer. They are listed under Take it further on this page.
Lights switch on by themselves at dusk and off again in the morning.
A precise thermostat that keeps tropical fish at a steady 25–26 °C.
Starts the pump when the tank runs low and stops it when it is full.
Tap a hidden pad to switch a lamp on or off, even through a wooden desk.
Learning project. Check every circuit and the datasheet of each part before you power it up.