Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsA thermostat needs a gap between switching on and off, or the relay chatters. This MicroPython program reads a DHT22, switches a relay on above 30 degrees and off below 28, and shows the temperature, humidity and fan state on an OLED. The two limits are editable at the top of the program. A small low-voltage fan 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, dht
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
i2c = I2C(0, sda=Pin(4), scl=Pin(5), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
sensor = dht.DHT22(Pin(15))
relay = Pin(14, Pin.OUT, value=1) # 1 = off for an active-low relay module
ON_C, OFF_C = 30.0, 28.0
fan = False
while True:
try:
sensor.measure()
t, h = sensor.temperature(), sensor.humidity()
if not fan and t > ON_C:
fan = True
elif fan and t < OFF_C:
fan = False
relay.value(0 if fan else 1)
oled.fill(0)
oled.text("%.1f C %.0f%%" % (t, h), 0, 0)
oled.text("Fan: " + ("ON" if fan else "off"), 0, 24)
oled.text("on>%d off<%d" % (ON_C, OFF_C), 0, 48)
oled.show()
except OSError:
pass
time.sleep(3)
Widen the gap between ON_C and OFF_C.
Swap 0 and 1 in the relay.value line.
The DHT22 may have failed a read; check wiring.
Yes. Swap the logic so the relay is on below the low limit.
Not directly; switch its thermostat, not the compressor.
Add two buttons to raise and lower them.
6 parts: Raspberry Pi Pico, DHT22 AM2302 Digital Temperature & Humidity Sensor, 5V Relay Module with Optocoupler, 0.96" OLED Display 128x64, 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 1 hour. The build on this page has 6 steps.
No. This page has the complete, tested code (main.py).
About ₹808 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Hysteresis control; Reading a DHT22 in MicroPython; Switching a relay from the Pico.
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 temperature fan curve. 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.