DIY Countdown Timer for Christmas Lights with a Raspberry Pi
It’s that time of year again—when the house looks like a runway for twinkling elves and the power bill starts to whisper warnings. If you’ve ever wished you could program the exact moment your lights go on, stay on, and then fade out without juggling a stack of cheap plug‑in timers, you’re in the right place. I’m Mason Greene, and I’m about to walk you through a simple, cheap, and surprisingly satisfying way to put a smart brain behind your holiday sparkle using a Raspberry Pi.
Why a Countdown Timer Matters
The magic of the first light
There’s something almost ceremonial about the first glow of Christmas lights on a cold November evening. It signals the start of the season, lifts the mood, and—let’s be honest—gives you an excuse to break out the hot cocoa. A timer lets you automate that moment, so you never have to scramble to plug in a strand at 6 p.m. while the kids are already asking for Santa.
Avoiding the “all‑night‑on” nightmare
Most of us have learned the hard way that leaving lights on all night drains the circuit, heats up the sockets, and can even cause a fire hazard if the wiring is old. A countdown timer gives you precise control: lights on at sunset, off at bedtime, maybe a brief “twilight” mode for those late‑night walks. It’s the perfect blend of festive flair and safety.
What You’ll Need
| Item | Why It’s Needed |
|---|---|
| Raspberry Pi (any model, but Pi 3 or later is ideal) | The tiny computer that will run the schedule |
| Micro‑USB power supply (5 V, 2.5 A) | Keeps the Pi happy and stable |
| Relay board (2‑channel 5 V) | Safely switches the high‑voltage AC lights |
| Jumper wires | Connect Pi GPIO pins to the relay |
| Christmas light set with plug | The actual décor you’ll control |
| Optional: enclosure or project box | Keeps everything tidy and protected |
All of these can be found for under $30 if you hunt on a sale or second‑hand market. The relay board is the only part that deals directly with mains voltage, so treat it with respect: never touch the contacts while the circuit is live.
Wiring the Pi to the Relay
- Power down the Pi and unplug it. Safety first.
- Connect the relay’s VCC pin to the Pi’s 5 V pin, and the GND pin to any ground pin on the Pi.
- Hook the IN1 pin (or IN2 if you want a second outlet) to a free GPIO pin—GPIO 17 works nicely.
- Wire the light plug: cut the plug’s live (black) wire, strip the ends, and attach them to the relay’s COM and NO terminals. COM is the common side, NO is “normally open,” meaning power only flows when the relay is activated.
- Secure everything in a small box, leaving the Pi’s ports accessible for power and network.
If you’re uneasy about working with mains, ask a friend who’s an electrician to give the final connection a quick look. The low‑voltage side (Pi to relay) is completely safe.
Setting Up the Software
Installing the OS
Grab the latest Raspberry Pi OS Lite (the headless version) from the official site, flash it to a micro‑SD card with balenaEtcher, and boot the Pi. I prefer the Lite version because we don’t need a desktop for a simple timer.
Enabling GPIO Access
Open a terminal (or SSH in) and run:
sudo apt update
sudo apt install python3-gpiozero
gpiozero is a friendly Python library that abstracts the low‑level pin handling. It lets you write “relay.on()” instead of fiddling with raw registers.
Writing the Timer Script
Create a file called lights_timer.py in your home directory:
#!/usr/bin/env python3
from gpiozero import OutputDevice
from datetime import datetime, time
import time as t
# GPIO pin 17 controls the relay
relay = OutputDevice(17, active_high=False, initial_value=False)
# Define the schedule
ON_TIME = time(17, 30) # 5:30 pm
OFF_TIME = time(22, 30) # 10:30 pm
def is_time_between(start, end, now):
"""Return True if now is between start and end (inclusive)."""
if start <= end:
return start <= now <= end
else: # over midnight
return now >= start or now <= end
while True:
now = datetime.now().time()
if is_time_between(ON_TIME, OFF_TIME, now):
relay.on()
else:
relay.off()
t.sleep(30) # check twice a minute
Make it executable:
chmod +x lights_timer.py
Running at Boot
Edit the crontab for the pi user:
crontab -e
Add the line:
@reboot /home/pi/lights_timer.py &
Now the script launches automatically whenever the Pi powers up. The & runs it in the background, leaving the terminal free.
Tweaking the Schedule
The script above uses fixed times, but you can get fancy:
- Sunset based: Install the
astralPython package and compute local sunset each day, then turn lights on a few minutes after. - Random flicker: Add a short random delay before turning off to simulate a “twinkling” effect.
- Multiple zones: Use a second relay channel for a separate strand, each with its own schedule.
All of these are just a few lines of Python away, and the Raspberry Pi handles them without breaking a sweat.
Testing Before the Big Night
- Dry run: Disconnect the mains and plug a cheap lamp into the relay. Run the script and watch the lamp turn on/off at the expected times.
- Log output: Add
printstatements or write to a log file to verify the script’s logic. - Power check: Once you’re confident, reconnect the Christmas lights, plug the Pi into a UPS or a surge protector, and let it run for a few hours.
I once forgot to set the active_high=False flag, which meant the relay was energized when the script started—my lights flickered on at 2 am, startling the cat. A quick edit fixed it, but the lesson is: test early, test often.
The Payoff
When the first orange glow spills onto the front porch at exactly 5:30 pm, you’ll feel a quiet pride that’s hard to describe. No more juggling timers, no more worrying about leaving lights on too long, and no need to buy a pricey commercial controller. Plus, you’ve got a little Raspberry Pi humming away, ready for the next holiday hack—maybe a voice‑controlled snowflake projector or a temperature‑aware indoor garden.
If you’re new to the Pi, this project is a perfect entry point. It teaches you basic wiring, Python scripting, and cron‑style scheduling—all skills that translate to countless other smart‑home ideas. And if you’re already a seasoned maker, consider adding a web dashboard so you can toggle the lights from your phone, or integrate with Home Assistant for a unified holiday scene.
Happy wiring, and may your lights always shine at the right moment.
- → Step-by-step Guide to Building a Low-cost Raspberry Pi Weather Station for Classroom Learning @stemcasters
- → Exploring Edge AI: Running Machine Learning Models on Raspberry Pi @techtrekker
- → DIY Home Automation: Using Raspberry Pi to Control Your Lights and Locks @smarthomeliving
- → Turning a Raspberry Pi into a Home Automation Hub: Full Walkthrough @techshredder
- → Automate Home Tasks with a Raspberry Pi and Python: A Beginner’s Project @techtinker