In this tutorial we will cover how to program the Raspberry Pi Pico to control neopixels or also known as individually addressable RGB LEDs. We will use the popular WS2812B LED type of strip and based on a MicroPython script on a Raspberry Pi Pico, we will learn how to program the color of each LED individually.
- Prepare the hardware
– First you need a computer to run Thonny. In this tutorial we’ll use a Raspberry Pi 4 as our computer. And Thonny is a user-friendly Python IDE to interact with the Raspberry Pi Pico board. If you never used Thonny to program the Raspberry Pi Pico, before continuing, you better have a look at our tutorial “How to start programming the Raspberry Pi Pico“.
– Next you need an USB cable with micro-USB plug.
– You also need a Raspberry Pi Pico of course. And as we’ll connect another device to the Pico, we need pin headers soldered to the GPIO-pins of our board.
And finally you’ll need some extra components :
– a breadboard (we are using a 400 points breadboard)
– a neopixel or individually addressable RGB LED light strip (we are using a strip of 1 meter WS2812B LEDs with 30 LEDs per meter)
– Dupont jumper wires
- Get to know the neopixel or individually addressable LED light strip
Individually addressable LEDs or NeoPixel are RGB LEDs with an integrated tiny microcontroller on each LED. Besides a common VCC (5v in our case) and GND wire, there is a single data line that goes from one LED to the next. This configuration allows us to send individual information regarding color and brightness to each LED of the strip.
When a microcontroller as our Raspberry Pi Pico sends information to the strip, it’s always in a specific direction. To start, the data is sent to the first LED. The chip of the LED reads the data linked to the address of the first LED. It executes the instructions and passes the data to the next LED. And this goes on until the last LED of the strip.
This means there is no problem to cut the LED strip after an amount of LEDs you decide. And besides LED strips, the WS2812B LEDs comes in different formfactors as LED matrix panels of different sizes for example. Taking into account the small size and the easyness to wire all the elements, it makes the Individually Addressable RGB LEDs very convenient to install.
The way we use to transmit our desired color to a chosen LED is based on the classic RGB color model. This means that we can define for the 3 color channels (Red, Green and Blue) that each LED contains, a different level of brightness. There are 255 levels of brightness for each color channel. And with a specific brightness for each channel, we can generate all possible colors.
There are many different types of neopixel LED-strips:
– first, you can buy the strips with different lengths of course
– the density (the amount of LEDs per meter) can vary
– the voltage can be 5V or 12V (use 12v for longer distances)
– the ingress protection is another specification and can be important for outdoor projects for example
– finally, there is the RGBW or RGB+W varient (with an additional white chip, besides the red, green and blue one)
In this tutorial we are using a WS2812B strip of 1 meter, 30 LEDs/meter, 5V, IP30, RGB.
You can find more information regarding NeoPixels on this adafruit webpage.
Be careful ! Before starting to connect components to the GPIO pins of your Raspberry Pi Pico, make sure it is not connected to your computer.
- Setup the hardware part
– connect the +5V of the LED strip to the VBUS pin (red wire), DON’T USE THE VSYS PIN HERE
– connect the data pin of the LED strip to GP28 (yellow wire)
– connect the GND of the LED strip to a GND (ground) pin (black cable)
Pay attention ! As explained in the previous step, the data-flow to the LEDs has a specific direction. So, connect the Raspberry Pi Pico to the “Din” side of the LED strip. The LED strips of our kits have a male and female plug. Use the female plug.
Be careful ! For a LED strip with a limited amount of LED’s it’s OK to power the LED strip through the Raspberry Pi Pico. In this tutorial we are using a LED strip with 30 LED’s. The maximum current of each LED of our strip is 33mA. Which gives a maximum current of about 1A going through the Pico. Which is OK for testing, but consider it as a maximum. If you want to setup a real life application with a LED strip, always use a separate 5V power supply which can supply enough current to all the LEDs of your LED strip.
- Write the code
The aim of this basic script is to explore some of the commands we can use to program the color of the RGB LEDs.
Install the Library first
To avoid extensive and complicated code writing, libraries are often used. For our WS2812B LED strip, we will also be using a library. We found the most appropriate library from Blaz Rolih. As the file from this library is quite specific, it doesn’t come automatically with MicroPython. It means we have to install it ourselves.
So, before writing the code, we’ll have to upload the files to our Raspberry Pi Pico. You can download a ZIP-folder containing the files to be installed here.
Once downloaded and unzipped on your computer, you will see 2 files. ‘colorwave.py’ is an example of how you can control a LED-strip. We will come back on it at the end of this tutorial. And the ‘neopixel.py’ file contains the actual library. Upload both files to your Raspberry Pi Pico. If you don’t know how to do that, have a look at our tutorial ‘Transfer files between computer and Raspberry Pi Pico‘. If you have multiple folders on your Raspberry Pi Pico, make sure you upload the files in the same folder as the new file we will create for our main code. And don’t change the filename of the library of course.
By the way, you can use the same library for RGBW LED strips (see explanations in step 2).
OK, now the library is uploaded to our Raspberry Pi Pico, write or paste following code in the IDE:
from neopixel import Neopixel
numpix = 30
pixels = Neopixel(numpix, 0, 28, "GRB")
yellow = (255, 100, 0)
orange = (255, 50, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
color0 = red
pixels.set_pixel_line_gradient(3, 13, green, blue)
pixels.set_pixel_line(14, 16, red)
pixels.set_pixel(20, (255, 255, 255))
if color0 == red:
color0 = yellow
color1 = red
color0 = red
color1 = yellow
Be careful, MicroPython is whitespace-sensitive. Don’t remove the “tabs”.
Some explanations about the code :
import time: to import the time module. This will allow us to use time-related tasks.
from neopixel import Neopixel: to import the file from the neopixel library.
numpix = 30: we are using a LED strip with 30 LED’s, change this number according to the amount of LEDs of your LED strip.
pixels = Neopixel(numpix, 0, 28, "GRB"): configuring the LED strip : number of pixels, state machine number, data pin (GP28), RGB mode (specific to the LED strip we use).
yellow = (255, 100, 0): In the next lines we define several variables for the colors we will use. Each color is based on the RGB color model.
pixels.brightness(50): to define the brightness of the LEDs.
pixels.fill(orange): to give all the LEDs of the strip the same color.
pixels.set_pixel_line_gradient(3, 13, green, blue): to change the color of the LEDs gradually, here the color of the third to the thirteenth LED will gradually change from green to blue.
pixels.set_pixel_line(14, 16, red): to give a range of LEDs (from 14 to 16) the same color.
pixels.set_pixel(20, (255, 255, 255)): to give 1 LED (20) a specific color, and instead of a variable, we can also use the RGB values directly.
while True:is an infinitive loop (until we stop the program).
In the following lines, we alternate the color of LED nr 0 and nr 1.
pixels.show(): to actually send the data to the LED strip.
time.sleep(1): before repeating the loop, we wait for 1 second
- Run your script
Now, it’s time to save your script. Just make sure you save this MicroPython file in the same folder as the file from the library ‘neopixel.py’ you uploaded earlier.
Then click on the Run button. And check the color off the LEDs according to the script.
- Experiment with additional methods, other colors and other sequences
Great, you got the basic skills to control an RGB LED strip with a Raspberry Pi Pico now!
Before continuing with other experiments, it’s good to know that the library contains 2 other useful methods :
– rotate_left(number of pixel) : to shift the color of the pixels to the left
– rotate_right (number of pixel) : same method, but opposite direction
The ‘colorwave.py’ file you uploaded earlier, is a nice example how you can implement this method. Try it and have fun !
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?