Automatic dusk-to-dawn light
Lights switch on by themselves at dusk and off again in the morning.
Shop by category
Shop all componentsAn infrared receiver turns a remote's flashing signal into a clean pulse train. MicroPython decodes the common NEC protocol with a short pure-Python routine that measures the gaps between pulses. This project prints the code of any button you press, then uses one chosen code to toggle a relay. Not every remote uses NEC, so if no code appears, try a different remote.
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.
IR receiver module
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
ir = Pin(16, Pin.IN)
relay = Pin(15, Pin.OUT, value=1) # 1 = off for an active-low relay module
TARGET = 0x00000000 # replace with the code printed for your button
def wait(level, limit_us):
start = time.ticks_us()
while ir.value() == level:
if time.ticks_diff(time.ticks_us(), start) > limit_us:
return None
return time.ticks_diff(time.ticks_us(), start)
def read_nec():
# Wait for the 9 ms leader burst (line low), then the 4.5 ms space
if ir.value() == 1:
return None
t = wait(0, 12000)
if t is None or t < 8000:
return None
t = wait(1, 6000)
if t is None or t < 3500:
return None
value = 0
for _ in range(32):
if wait(0, 900) is None:
return None
gap = wait(1, 2500)
if gap is None:
return None
value = (value << 1) | (1 if gap > 1000 else 0)
return value
while True:
code = read_nec()
if code is not None:
print(hex(code))
if code == TARGET:
relay.toggle()
time.sleep_ms(400)
Check the receiver's OUT pin and power. Some remotes use other protocols than NEC.
Sunlight and fluorescent light add noise. Shade the receiver.
Change the initial value and the logic for your module.
Most cheap ones use NEC. Branded TV remotes may use Sony or Samsung timings.
Yes, keep a dictionary mapping codes to actions.
About 5 to 8 metres in a dim room.
5 parts: Raspberry Pi Pico, Generic Infrared IR Receiver Module for Arduino – 38kHz TSOP1838 Sensor, 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 ₹563 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
How the NEC infrared protocol is timed; Decoding pulses with GPIO interrupts; Toggling a relay from a code.
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 IR remote relay 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.