How to Automate Christmas Lights With a Raspberry Pi Pico W
Raspberry Pi enthusiasts will argue that the true meaning of Christmas is plugging in your favorite light display, creating something new with your Raspberry Pi Pico, and building something that causes you to crack a smile.
There’s a priceless feeling of pride when you assemble hardware, together with software, creating a masterpiece. No matter how long you’re tinkering with tech, that feeling never seems to go away.

Let’s dive into the sheer joy that you’ll gain from learning the basics of Christmas light automation using your Raspberry Pi Pico, MicroPython code, and a few relays.
What You’ll Need
Make sure you have a few components before getting started:
Assembling the Hardware
First, let’s focus on the connections between the Relay Module Board and the Raspberry Pi Pico:
If you don’t have some electrical knowledge, it’s recommended to get help from a friend. You’ll want to wire up the 5V relays similar to those shown in the image above. The first (left) terminal does not get used on the 5V Relays. Also, since there is no fourth Christmas light string in this tutorial, the last relay and outlet plug are not connected together.

Each relay will be able to prevent the electricity from flowing to each outlet plug where each light strand is connected to. The Raspberry Pi Pico, through the MicroPython code, will instruct the relays when to turn on and off.
To dive a bit deeper into electrical automation,check out our relay guidefor details.
Creating the Code
First, slide over to our GitHub repository todownload the code. You can either download thelights.pyfile or copy and paste the code from the GitHub window into your Thonny IDE directly. If you just picked up a freshly made Raspberry Pi Pico, review the step-by-step guide to help youset up your new microcontrollerwith Thonny.
Let’s review a few important code snippets to understand what’s going on. Having the ability to importrandom,machine, andtime/sleepfunctionality makes this coding project really simple. Then, all you need to do is map each relay to which pin is being used:

Repeating this mapping will need to continue for each relay you’re assigning to thePin.Outfunction (corresponding to the GPIO pin on your Raspberry Pi Pico) to.
Creating a list of relays will give you the ability to randomly choose which light strand to turn on and off:

A loop can serve as the Christmas lights randomizer. You can choose arange of 50(as in the example code), or feel free to increase the number. This will instruct your Raspberry Pi Pico to run through this process for the amount of times defined in the set range.
In this loop, you’ll userandom.choice, along with the list functions with the purpose of have the system pick and choose a relay to switch on and off at will.
The example code prints the output of(lights); however, this is not required. On the other hand, you may want to keep it in the code until you’re finished troubleshooting.
lights.valuewill have a suffix of(1)or(0)depending on whether the relay should be set to open (on) or closed (off). The open and closed state may also depend on how you’ve chosen to wire up the outlets and relays.
GPI0.cleanup()will clear the Raspberry Pi GPIO so that your Christmas lights don’t stay in the “on” position when the Python code is finished. For testing purposes, this function was commented out in the example code. With that said, there is generally more than one way to perform a task. Review our comparison betweenRPi.GPIO versus GPIO Zerofor details.
Now, the final result. If your volume is up a bit, you’ll notice the audible click of the relays turning the outlets on and off as the loop runs through the randomize function.
What’s Your Next Automation Adventure?
In the Christmas classic movie, Christmas Vacation, Clark Griswold uses a scary number of extension chords to electrify his house full of Christmas lights. Although, this seems like something only fit for Hollywood, there are many automation projects that range from simple to unbelievable. With the right open source equipment and software, you can even use a Raspberry Pi to sync your lights to Christmas music too!
Are you ready to automate your house filled with Christmas lights to your favorite rock and roll classics? Now that you understand the basics of automation using the Raspberry Pi Pico and relays, you’re now set to send “send us cookies” signals all the way to the North Pole.
These dazzling DIY lighting projects will brighten up your festivities.
you could’t call this offline, Notion.
Your iPhone forgets what you copy, but this shortcut makes it remember everything.
My iPhone does it all, but I still need my dumb phone.
You’ve been quoting these famous films wrong all along!
Free AI tools are legitimately powerful; you just need to know how to stack them.