Use NeoPixels with Raspberry Pi and Python

By Rob Zwetsloot. Posted

Light up a display cabinet with some NeoPixels, a Raspberry Pi, and a little Python code

We’ve covered NeoPixels in The MagPi before, with some wonderful cosplay lights and Christmas tree lights, which we’ve used ourselves. It’s been a long time since we’ve controlled any NeoPixels with a Raspberry Pi, though – so long that there’s actually a newer and much easier method for doing it.

We thought it was high time to try this out, and get a display cabinet lit up fancily in the process.

You'll need

  • NeoPixel lights
  • Circuit wire
  • Push-button
  • 470 Ω resistor
  • 5V power source
  • Soldering iron

Choose your NeoPixels

There are many configurations and types of NeoPixels that you can buy. For our display cabinet, we chose two quarter-circle stripes of lights that hold 15 LEDs a piece. This allowed us to create a little semicircle in our cabinet for a bit more interesting coverage.

If you have a big cabinet and wish to line the entire thing, you can always get a long flexible strip of NeoPixels. You can even get single lights, or smaller circles of NeoPixels.

The only thing you need to make sure you do for all the NeoPixel types is correctly count the number of LEDs in your system. We’ll tell you why in a few steps’ time.

Choose a location

What do you want to light up? For our project, a shelf of figurines was all we wanted to illuminate, and so we decided to add the lights above the shelf – attached to the ‘ceiling’, as it were.

 Our display cabinet we set up to add lights to. The top contains a series of red ranger 'ranger keys' from the TV show Kaizoku Sentai Gokaiger

You’ll need to take into consideration light coverage and camouflage in your display cabinet. Think about sight lines if you want to keep them hidden from specific angles, and look to see if your display cabinet has anything to aid in adding lights – the IKEA Detolf has a little plug on the top for wires, for example.

There also needs to be access for the Pi to control the lights, so keep that in mind.

Assemble your circuit

We’ve put together a handy circuit diagram below for you to follow along to. There are a few important things to note, though, to make sure you understand it.

The NeoPixel strips have three pads on them: one for 5 V power, one for ground, and a ‘data’ port. The data needs to be connected to the GPIO pin we’re sending the signals from. You also need to make sure to connect it to the Din (data in) pad. If you’re chaining together strips like we’ve done, make sure you connect the Dout (data out) from the first strip to the Din of the next strip. It’s also good practice for ground on the NeoPixels to go to the Raspberry Pi’s ground, as well as the ground of the power source.

We’ve placed the button way down the GPIO pins to keep it clear. You need the resistor in the little button circuit to make sure it works properly and can be sensed by the Raspberry Pi.

 Our wiring reference for this project - yours may differ slightly depending on your LED configuration

Soldering the NeoPixels

This bit can be tricky, but you will have to solder some wires to your NeoPixel strips. Make sure your soldering iron is properly prepared if this is your first time using it. Also, if this is your first time soldering, the Raspberry Pi Foundation has a great video on it.

We recommend putting a little solder on your wire (this is called tinning it), as well as a dab on the pads. That way, you just need to heat up the solder already on the wire and pad so that they fuse and connect.

Powering the system

You need to power two parts of your light display: the Raspberry Pi and the NeoPixels. The NeoPixels require a 5 V input, and also draw a lot of power at the same time; so, to be safe, you really shouldn’t power more than two or three NeoPixels with the 5 V pin of the Raspberry Pi. In our circuit, we’ve used four 1.5 V AA rechargeable batteries, as they’re technically closer to about 1.2 V each – that means we get roughly 5 V out of them.

A more convenient way is to get a 5 V power supply and use a power terminal with two screw ports to attach the positive and negative ends. Please make sure the power supply is unplugged while you do this, and be very careful when plugging it in.

You can technically run a Pi using the GPIO pins; however, we have elected to run a micro USB cable up to our Pi.

Basic code

We’ve written the code, rollcall.py, to work with our specific system. Download it from our GitHub and we’ll run through it.

First, we import the generic stuff: time, gpiozero, as well as the neopixel library and its related board library. To install the NeoPixel library, you'll need to open the terminal on your Raspberry Pi and enter:

sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel

We’ve also used the numpy library so that we can create RGB values for the LEDs, which enables them to fade between colours.

Our system uses 30 LEDs, and we’ve connected it to GPIO 18 on the board. We’ve then defined six colours that represent the figurines in the display for our added colour cycle effect. After that, we tell the code about the strip of NeoPixels.

We then define how to calculate the values for colour fading, and the colours to transition between, before turning the strip of LEDs to pure white. Finally, we set the main rollcall_cycle function to loop, so we can press the button whenever we want.

Alter your code

The main parts of the code to pay attention to are the LEDCOUNT, LEDPIN, and button values. Your number of LEDs will likely differ from ours, and you may have connected the strip and button to different GPIO pins.

As for the colour cycle, play around with that to your heart’s content, or remove it completely! You can even change the main colour value for the standard lights, using RGB values from 0 to 255.

Testing your lights

Before attaching everything to your cabinet, we highly recommend testing your LEDs. Run the code from within your preferred Python IDE (you may have to run it from the terminal using sudo), and make sure that not only are you getting the correct colours (you may have a GRB set of NeoPixels instead of RGB, for example), but that also the button works.

Final Pi prep

The Raspberry Pi in our setup is going to be headless, which means we want the Python code to load up after the Pi turns on. Our preferred method of doing this is adding a line to /etc/profile – it makes it a lot easier. Open up a Terminal window and type:

sudo nano /etc/profile

Use the arrow key to go to the bottom, then add:

sudo python rollcall.py

If you’ve saved the Python script to a specific folder other than the home directory, make sure to include the path to it as well. Save and exit the file. You can also turn off ‘boot to desktop’ in the Raspberry Pi configuration settings, so that the entire system loads up faster. It also uses a little less electricity that way.

Mounting the lights

Depending on your NeoPixels, you can attach them in several different ways. We’ve used Blu Tack to stick ours to the ceiling of our display case; however, you could also use glue. For the long strips, you can always nail them in with staples so that you don’t actually have to go through the strip.

Just make sure any exposed PCB is not touching anything conductive.

 The lights have been attached to the top here with some Blu Tack, while the wires are threaded through the hole.

Permanent circuit tips

You don’t really want exposed wires everywhere just to light up your display case. Creating a 3D case to house the Pi is a good first step, and using heat-shrink tubing to encase all the cables makes the whole thing a lot neater. You can also cover any soldered joints with a bit of hot glue.

Have fun and experiment!

This basic setup can very easily be expanded upon. As the Pi is internet-connected, you’ll be able to use some IoT stuff like Twitter triggers or noise activation or temperature-dependent colouration. You can even add more strips to the Pi so that you can have several layers of lighting effects. We hope this really improves your display cabinets.

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.