In our last article we discussed how to wire WS2812b addressable LEDs to a Raspberry Pi.  Of course, just wiring them up doesn’t really do much.  Not even a single LED on a WS2812b strip will light until you send it a command to do so.  So in this article we will continue on with learning about controlling WS2812b LEDs with a Raspberry Pi using Python code.

Controlling WS2812b LEDs with a Raspberry Pi

If you’re new to Raspberry Pi‘s or addressable LEDs, here’s a convenient parts list for this project:

Before we begin writing Python code to control your addressable LED strips, we need to make sure your Raspberry Pi has the prerequisite libraries installed.

Installation of the NeoPixel Library for Python

There are several libraries out there for controlling WS2812b LEDs with a Raspberry Pi, but my favorite is the Neopixel library. It’s super easy to use and just makes everything a snap.  If you’ve got a favorite drop it in the comments below.

To install the Neopixel library run the following command:

sudo pip3 install rpi_ws281x adafruit-circuitpython-neopixel

Note: If you’re running anything other than the default install you might want to pop a freshly loaded SD card into your Raspberry Pi to make sure this works.  Previous installs that have changed your version of Python may keep this from working.  Circuit Python is only compatible with Python 3.x.

Controlling WS2812b LEDs with a Raspberry Pi using Python

The first few lines of code in your Python program are there simply to import the needed libraries and to assign the WS2812b LED strip to a GPIO pin. The following code does that.  We assign GPIO pin 18 as the connection for our addressable LEDs and we define that there are 30 LEDs in our strip. If your WS2812b LED strip is longer or short, just change 30 to the appropriate number of LEDs.

import board
import neopixel
pixels = neopixel.NeoPixel(board.D18, 30)

Surprisingly, that’s all there is to it!  The next few lines of code can be anything you’d like. Let’s just do a single line of code to light the first LED and make it red.  Enter the following code and then run your Python program:

pixels[0] = (255, 0, 0)

Upon execution your addressable LED strip should look this the following picture:

Writing a for-loop or do-while and modifying any of the numbers will of course make the LEDs change according to your loop statement.  The following code for example would make the second through tenth LEDs light up 1 second apart in order (We start at zero and end at 9 since addressable LEDs start at LED 0).

for x in range(0, 9):
    pixels[x] = (255, 0, 0)
    sleep(1)

Of course, changing the color is as simple as changing the RGB values after pixels (or GRB depending on your strips).  Pixels[0] = (0,0,255) would be bright blue for example. If you’re not familiar with RGB, a simple google search should set you on the right path, but simply put 0-255 defines the brightness or intensity of the LED color in Red, Green, or Blue.

If your wanted to turn the entire LED strip on and set all LEDs to green we’d use the fill command to do that:

pixels.fill((0, 255, 0))

Again, as surprising as it may seem, that’s all there is to getting started with controlling ws2812b LEDs with a Raspberry Pi!  From here everything else is just standard Python language wrapped around these two commands!