Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsThe Pico W sets its clock from an internet time server at start-up, then checks the time every few seconds and switches a relay according to a schedule you write in a list. It re-syncs the clock every few hours so it stays accurate, and it handles Indian time with a fixed offset of five hours thirty minutes. It suits a garden pump, an aquarium light or a water motor; for mains loads, the relay wiring must be done by an electrician.
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 network, time, ntptime
from machine import Pin
SSID = "YOUR_WIFI"
PASSWORD = "YOUR_PASSWORD"
OFFSET = 5 * 3600 + 30 * 60 # India: UTC+5:30
SCHEDULE = [(6, 0, 6, 30), (18, 0, 18, 30)] # (on hour, on minute, off hour, off minute)
relay = Pin(15, Pin.OUT, value=1) # 1 = off for an active-low relay module
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
while not wlan.isconnected():
time.sleep(0.5)
def sync():
try:
ntptime.settime()
except OSError:
pass
def should_be_on(h, m):
now = h * 60 + m
for oh, om, fh, fm in SCHEDULE:
if oh * 60 + om <= now < fh * 60 + fm:
return True
return False
sync()
last_sync = time.time()
while True:
if time.time() - last_sync > 6 * 3600:
sync()
last_sync = time.time()
t = time.localtime(time.time() + OFFSET)
relay.value(0 if should_be_on(t[3], t[4]) else 1)
time.sleep(5)
The NTP sync may have failed. Check Wi-Fi and the OFFSET line.
Check the relay's active level; flip 0 and 1 in the code if it is inverted.
It re-syncs at start-up, so it recovers as soon as Wi-Fi returns.
The Pico keeps time while powered, but loses it on power loss until it re-syncs.
Yes. Check t[6], where 5 and 6 are Saturday and Sunday.
Not in this version. The web relay projects show how to add a page.
4 parts: Raspberry Pi Pico W Microcontroller Board, 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 intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (main.py).
About ₹820 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Setting the clock with ntptime; Writing a simple schedule list; Switching a relay by time of day.
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: Pico W web switch. 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.