NeoPixels
NeoPixels are Red-Green-Blue LEDs that are designed to makes them easy to control with three wires: GND, +5V and a single serial data line. They are very popular with our students because they are powerful, easy to program and full of bling.
Note
As of March of 2022 there is now built-in support for NeoPixels in the MicroPython 1.18 runtime for the Raspberry Pi RP2040 microcontroller. Although you can still use custom libraries, this tutorial assumes you are using version 1.18 or later.
Controlling NeoPixels is challenging since the timing of data being sent must be very precise. Python alone is not fast enough to send bits out of a serial port. So a small function that uses assembly code is used. This code can be called directly from a neopixel driver file so that the user's don't need to see this code.
MicroPython Example Code on ESP8266
Different Types of NeoPixels
There are many different types of NeoPixels. They come in many forms such as strips, rings and matrices.
The most common type of NeoPixels are strips. The strips come in a variety of densities and waterproofing. The most common and easiest to use are the 60 pixels-per-meter type.
Circuit connections
LED Strip | Pico Name | Pico Pin | Description |
---|---|---|---|
GND | GND | 3 | Ground |
5v | VBUS | 40 | Voltage from the USB bus. Top right with USB on top |
Data | GP22 | 22 | Row 12 on the right side |
Note that you can also power most of the LED strips using the 3.3 volts available on Grove connectors. The only difference is the brightness might not be quite as high, but for most applications this will not be a problem.
Setup Parameters
Our Python code will have four parts:
- Declaration of the import of the NeoPixel library from the RP2 runtime. We also import the sleep function from the utime module.
- Initialization of the fixed static parameters. This is done once and the parameters are usually at the top of the file to make them easy to find and change for each application.
- Initialization of the NeoPixel object using these static parameters. This is also done just once.
- Sending the drawing commands to the device through the data port. This is usually done within a main loop.
Import Statements
Here are the import statements we use:
1 2 3 |
|
Static Initialization Parameters
There are only two values. The number of pixels in the strip or ring and the pin number the data pin is connected to.
1 2 |
|
Initialize the Strip Object
To setup the NeoPixel object we just pass it the two parameters like this:
1 |
|
Here is the full initialization code:
1 2 3 4 5 6 7 |
|
Sample Programs
Now we are ready to write our first small test program!
Move Red Pixel Across Strip
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Move Red, Green and Blue
The following program will just take the block of code in the for loop above and duplicate it three times, one for red, one for blue and one for green
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Rainbow Cycle
The program cycles each pixel through all the colors in a rainbow. It uses two functions:
- wheel(pos) this function takes a position parameter from 0 to 255 and returns a triple of numbers for the red, green and blue values as the position moves around the color wheel. This is a handy program anytime you want to cycle through all the colors of the rainbow!
- rainbow_cycle(wait) will cycle each of the pixels in a strip through the color wheel. It gives the appearance that colors are moving across the strip. The wait is the delay time between updating the colors. A typical value for wait is .05 seconds or 50 milliseconds.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
References
- MicroPython RP2 Reference for NeoPixel Driver
- Core Electronics: How to use WS2812B RGB LEDs with Raspberry Pi Pico - HTML page, sample code and video
- MicroPython Library for NeoPixel (used before version 1.18 of the MicroPython RP2 Runtime) - note the lack of support for the RP2040 microcontroller.
- rp2 port no module named array