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:

Raspberry Pi Pico Coding Machine (Raspberry Pi, or PC) 5V Relays (Relay Module Board is great for simplicity’s sake) Wires (electrical wire, and breadboard jumper wire) Christmas lights Electrical outlets Scrap wood

Assembling the Hardware

First, let’s focus on the connections between the Relay Module Board and the Raspberry Pi Pico:

GND (relay) connects to GND (Raspberry Pi Pico) IN1 (relay) connects to GP5 (Raspberry Pi Pico) IN2 (relay) connects to GP6 (Raspberry Pi Pico) IN3 (relay) connects to GP14 (Raspberry Pi Pico) VCC (relay) connects to 3V3 OUT (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 guide for details.

Creating the Code

First, slide over to our GitHub repository to download the code. You can either download the lights.py file 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 you set up your new microcontroller with Thonny.

Let’s review a few important code snippets to understand what’s going on. Having the ability to import random, machine, and time/sleep functionality 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 the Pin.Out function (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 a range 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 use random.choice, along with the list functions in order to 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.value will 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 between RPi.GPIO versus GPIO Zero for 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.