How To Use Raspberry Pi To Code Your Holiday Decorations

How To Use Raspberry Pi To Code Your Holiday Decorations

12/21/2022
9 minutes

Whether you take a Clark Griswold approach to your home’s holiday light display, or are more of a Charlie Brown minimalist, ‘tis the season for twinkly lights everywhere you look. With some savvy programming skills and a Raspberry Pi computer, you can code a custom holiday light display — and we’ll teach you how.

If you’re not familiar with Raspberry Pi, “it’s basically a full computer in the size of a credit card,” explains Jace Van Auken, Codecademy Curriculum Developer who worked on the Learn Raspberry Pi course. Raspberry Pi was invented in 2012 by a programmer named Eben Upton, who was inspired to make a low-cost hobby computer similar to the one he grew up using in the ‘80s, called the BBC Micro.

The single-board computer might look bare bones or unassuming, but it’s capable of much more than meets the eye. “Raspberry Pi is super powerful and it continues to get more powerful,” Jace says. What’s so neat about Raspberry Pi is that it’s not only a practical tool for learning programming and computing, but it can also be used for creative pursuits and fun stuff that’s purely for entertainment purposes. In fact, Raspberry Pi is a go-to tool for STEAM (short for Science, Technology, Engineering, Art, and Math) projects.

Learn something new for free

Given that, we’re going to show you how to use Raspberry Pi to make holiday decor. Jace will explain how you can use your Raspberry Pi to create a light display that animates and changes colors. With a LED string light, your Raspberry Pi kit, coding knowledge, and some imagination, you’ll be decking your halls with code in no time.

The coding skills you need for this project

This project is a fun, hands-on opportunity to learn how to use Raspberry Pi, but you need some background knowledge before you get your hands dirty. Working with hardware and wires might be a little bit intimidating at first, and it’s not as straightforward as other projects where you’re just writing code. You can learn the skills you need for this project in the free Codecademy course Learn Raspberry Pi.

Raspberry Pi runs on the open-source operating system Linux. You can use lots of different programming languages with Raspberry Pi, but Python tends to be the most popular. There’s even an integrated development environment (or IDE) for Raspberry Pi called Thonny that comes with Python built-in. In Learn Raspberry Pi, you’ll get introduced to the command line and Linux operating system, plus you’ll learn the ins and outs of the hardware and software that’s used for Raspberry Pi projects.

If you’re new to coding, we have lots of other beginner-friendly courses that will set you up to complete this Raspberry Pi project — and start dreaming up your own projects. Start by checking out the free Codecademy course Learn Raspberry Pi. For a more in-depth look at Linux, you might want to try Introduction to Linux or Learn the Command Line. We also have lots of Python courses for all levels, like the popular beginner course Learn Python 3 or the free intermediate course Python for Programmers.

Gather these supplies

Here’s what you need in order to make a holiday light display with Raspberry Pi.

  • Raspberry Pi: You’ll need a Raspberry Pi computer with power for this project. Any RPi variation will work, and there are lots of options available at different price points that you can browse on the Raspberry Pi website. We recommend the Raspberry Pi 400 kit, which has a Raspberry Pi 4 built into a keyboard and comes with all the accessories you need to get started (a mouse, power supply, and monitor adapter).
  • Addressable LED strand: Find flexible strand lights that are “addressable,” which means that each LED light can be individually programmed to create animations or custom displays. We used this inexpensive option on Amazon.
  • Protoboard: Also known as a “breadboard,” this is a surface with rows and columns of holes that you can use to prototype circuits.
  • 5V power supply, 2 Amps: In order to properly power your Raspberry Pi, you need a 5 Volt power supply around 2 Amps.
  • Level-shifting chip: A level-shifter is necessary because the Raspberry Pi data ranges from 0-3.3V, but the LEDs want data from 0-5V. In many cases, the 3.3V data will work for the LEDs but isn’t a guarantee. You can find the integrated circuit we used by searching the product number: 74LS245N.
  • Hookup wire: This is the type of wire that you use when you build circuits with a protoboard. You’ll need Female/Male jumper wires for this project.

Start coding your holiday lights with Raspberry Pi

Before you jump in and start this project, spend some time tinkering around with Raspberry Pi. In the free Codecademy course Learn Raspberry Pi, we’ll walk you through how to properly set up a circuit in Raspberry Pi to run an external device like an LED light. It’s a good idea to take the course to get a detailed explanation of how to use Raspberry Pi — this project will make way more sense if you have a clear understanding of the technology first.

Set up your Raspberry Pi and circuit

Got all your supplies handy? ​​Using a breadboard we’re able to supply the LEDs 5V of power, and we can use the level-shifter to pass data from the Raspberry Pi to the LEDs. You’ll want to reference this image below for the wiring:

How to set up your circuit with your Raspberry Pi and breadboard.

We can zoom in and see where we placed the wires on our Raspberry Pi. These general-purpose input/ouput (GPIO) pins allow the Raspberry Pi to control external components like lights. The black wire (on the right) is connected to pin 6 on the 40-pin header. The green wire (on the left) is connected to GPIO18, which is pin 12 on the 40-pin header. You can get a quick diagram of the Raspberry Pi’s 40-pin header by opening up the terminal and typing pinout.

A closeup of the Raspberry Pi pins.

Take a closer look at the circuit wiring in the image below. The Raspberry Pi has 2 wires connecting to the breadboard: the green wire supplies data, the black wire is your ground wire. Our breadboard has a 5V power supply, and a level-shifting IC with 3 wires going to the addressable LEDs.

(Expert tip from Jace: It’s important that every component in this project shares ground, because ground is the reference point for the different voltages in this system. In many cases, if something is not working it is because the grounds of each component were disconnected somewhere.)

A closeup of the Raspberry Pi pins.

A note about safety: Be careful when you’re setting up your circuit. While the voltages and currents that come from your device’s general-purpose input/output (or GPIO) are relatively low, it’s possible to accidentally damage your Raspberry Pi and breadboard if you don’t take certain precautions. (We cover how to do this in the course Learn Raspberry Pi.)

Test your circuit

Now it’s time to write some code and test that your circuit works. Open up your terminal and run the following command to install the necessary Python modules. Be sure to use sudo (short for “superuser do”) when you run this command and your file.

sudo pip install rpi_ws281x adafruit-circuitpython-neopixel

With these modules installed, now we can turn on the lights. Run this code as sudo. If everything is set up correctly, you’ll see the first LED on your strand light up!

import board
import neopixel

NUM_PIXELS = 42

pixels = neopixel.NeoPixel(board.D18, NUM_PIXELS)
pixels[0] = (255, 255, 255)

Design your lights

Be creative and decide what shape or design you’d like to use for your lights — we chose a snowflake. Jace printed a snowflake pattern on a piece of paper, drew a dot where the lights will lay, and labeled each light with a number (1-42). He arranged the 42 LED lights so that each branch of the snowflake would contain 7 LED lights. Jace used hot glue to attach the LED strand light to the paper so the wired lights can maintain the shape of the snowflake.

We set up our LED lights like a snowflake.
It’s lit!

The following code goes further and creates functions to light up all the LEDs or just a single branch. In the main function there is an infinite loop that randomly colors each branch blue over a white snowflake. Try it out!

import time
import random
import board
import neopixel

# adjust these based on your project
NUM_PIXELS = 42
PIXELS_PER_BRANCH = 7

# color variables
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)

# global neopixel instance
pixels = neopixel.NeoPixel(board.D18, NUM_PIXELS)

# fill a branch (0-6) a specific color
def fill_branch(branch, color):
  start = branch * PIXELS_PER_BRANCH
  finish = start + PIXELS_PER_BRANCH
  pixels[start:finish] = [color] * PIXELS_PER_BRANCH

# fill all the pixels
def fill_all(color):
  pixels[:] = [color] * NUM_PIXELS

if __name__ == "__main__":
  while True:
    # create random branch indexes
    branches = [0, 1, 2, 3, 4, 5]
    random.shuffle(branches)
        
    # restart all LEDs to WHITE
    # go through random indexes
    # and light up branches to blue
    fill_all(WHITE)
    time.sleep(0.5)
    for i in branches:
      fill_branch(branches[i], BLUE)
      time.sleep(0.5)

A bit of diffusion paper over the whole thing hides all the wires and showcases the LEDs so we can enjoy the programmed animation!

Show us your creations

We hope this Raspberry Pi project keeps you entertained this holiday season, and inspires you to think of more ways to combine creativity and coding. If you tackle this DIY project, we want to see your finished product! Be sure to share a photo of your own Raspberry Pi holiday lights creation and tag Codecademy on Instagram, Facebook, and Twitter.

Related courses

3 courses

Related articles

7 articles