This installment of our micro:bit peripherals in Python series demonstrates the super-fun, ultra-popular NeoPixel light strips. NeoPixel is an Adafruit product, but there are multiple other types of similar “addressable LEDs”. One of the nice things about the NeoPixel is that there is already an easy-to-use Python library for it built-in to your micro:bit. All instructions and code below will be demonstrated with the Adafruit 12 RGB NeoPixel Ring but can be modified to work with any NeoPixel.
Check out the demonstration video here: https://youtu.be/OlSojuhayOY
WHAT IS A NEOPIXEL:
A NeoPixel is a peripheral that has multiple pixels placed on a circuit board. The board can be laid out in a ring, a line, a square, or just an individual pixel. Each pixel contains three tiny little LEDs: one red, one green, and one blue. These three LEDs let you use an RGB (Red Green Blue) color scheme to make the pixel look different colors and have different brightness’s. This same principle is used to make pixels on a television or computer screen light up different colors.
How do a red, green, and blue light allow me to see yellow?
The RGB model works by blending the wavelengths of the 3 light sources into one message that the cone cells in your eye receive and your brain processes. Those 3 light sources must be very close together and be sent from a black screen or reflected from a white screen to convince your brain that it is just one color rather than three distinct lights.
The three colors blend in specific ways to form other colors. Yellow can be created by shining 100% red, 100% green, and 0% blue color together. This blend of wavelengths is what makes up the yellow color to our eyes.
As a note, RGB cannot make up every color. It can only create a subset of colors based on the capabilities of the 3 specific LEDs. RGB pixels can show any color inside a color triangle like the one below:
SETTING UP THE DEVICE:
First, lets hook up the NeoPixel and then we can talk about how to use it with Python code.
NeoPixels are generally used with a 5 volt power supply, but can operate just fine from the 3V pin of the micro:bit - they just show up a little bit dimmer and less vibrant. There is a warning on the micro:bit documentation website that you should never power more than 8 pixels at a time directly from the micro:bit 3V line (it recommends using an external power source instead). Powering too many can cause damage to your micro:bit. Regardless of power supply, the micro:bit should not be used to drive the logic for more than 256 pixels at any one time. Driving more than that can cause weirdness due to the Python implementation of the NeoPixel module. For this example, we will use an external 3V power supply to power the NeoPixel ring. Make sure you connect to the DIN or IN pin on the NeoPixel and not the DOUT or OUT pin. The OUT pin is only used for chaining together multiple NeoPixel strips.
Powering NeoPixels from an External 5V Supply or Battery Pack
Powering NeoPixels Directly from the micro:bit
WARNING: You could do damage to your micro:bit if you use this with a NeoPixel that has more than 8 pixels.
LIGHTING IT UP:
The NeoPixel is an awesome little piece of hardware which is made simple to use by the micro:bit’s built-in neopixel library. This library allows you to select individual LEDs, give them an RGB value, and light them up. There is no need to learn the complicated data protocol between the micro:bit and the NeoPixel. Let’s start with a simple example. Hook up your NeoPixel as shown above, type the following code into CodeSpace, and run it on your micro:bit:
You should see a single red pixel on the NeoPixel strip.
Let’s break this code down.
First, we import the neopixel library:
from neopixel import NeoPixel
Next, we set how many pixels are in the NeoPixel strip we are using. In this case, my NeoPixel strip is 12 pixels so I set:
num_pixels = 12
The next step is to make a NeoPixel object. The NeoPixel needs two things: it needs a micro:bit pin number, and it needs to know how many pixels it has. My NeoPixel strip is connected to pin0 for this example.
np = NeoPixel(pin0, num_pixels)
Now we can set an RGB value on one of the pixels on the NeoPixel by accessing the pixel’s position. In the next line of code we are selecting the first pixel - np[0]. If we wanted to select the second pixel we would type np[1]. Then, we are setting that pixel to a specific color. The color format is three sets of intensities from 0 - 255. The numbers set the intensities of the three LEDs inside each pixel. The format for setting intensities is: (RED, GREEN, BLUE). In this line of code we are setting the LED’s color to red because the intensity of the RED LED is the maximum (255) and the intensity of the other two LEDs are the minimum (0). This sets what color the pixel is going to be.
np[0] = (255, 0, 0)
The final line of code just tells the micro:bit to show the new values for the pixels. The show function must be called before you will see any change on your NeoPixel.
np.show()
CHANGE THE COLORS:
We have learned how to set the first pixel to red. What if we want to use a different color? All we need to do is change the (R, G, B) value for the pixel and call the show function. Here are some common colors that you may want to use in your next project. Running the following code will cycle between different values.
There are lots of great resources online to calculate RGB color combinations! https://www.google.com/search?q=color+picker
SET MULTIPLE PIXELS AT ONCE:
You know how to set a single pixel. What if you want to set multiple pixels to different colors all at once? Easy! Just set the different values for the pixels and call the show function just one time. In this example, we are setting the first pixel to red, the second pixel to green, and the third pixel to blue. Give it a go!
MAKE IT SPIN:
Now we know how to set the first pixel to any color. What if we want to set other pixels? What if we want to clear all the old values? Run the following code on your micro:bit. Note that we are going back to just a red color for now, but you should experiment with changing it up!
If you ran the above code, you probably watched a red pixel spinning around your micro:bit. Let’s look at how it works.
We setup the NeoPixel like we did in the first example. Then we put our code into a while loop with a for loop inside of it. The first while loop just keeps the program running forever.
while True:
The second loop only runs 12 times. The first time it runs, it sets the value of “pixel” to 0. The second time it runs, it sets the value of “pixel” to 1. It keeps running until the value of “pixel” is equal to 11 and then it stops. This allows us to perform some action on each pixel in the NeoPixel.
for pixel in range(0, num_pixels):
The next function is a new one. The clear function tells the NeoPixel to clear all pixel values and turn completely dark. In this example, we are using it to turn off the red pixel.
np.clear()
Now we set the pixel selected in the for loop to red. This will be the next one in the circle because the for loop always increases it by one.
np[pixel] = (255, 0, 0)
Then, we show the new value.
np.show()
Finally, we wait a tenth of a second so that your eyes have time to follow the changing pixel.
sleep(100)
RANDOM COLORS:
For this last example we are going to change the colors randomly. This example uses the randint function which gives you a random value in the range you select. We are using randint(0, 255) which gives you a number between 0 and 255. Try it out yourself!
NeoPixels are fun by themselves, but even better when you add them to your own projects. String multiple NeoPixels together, sew them into clothes, or hang them on your wall as part of an artistic creation. I’m sure you can find a fun, new way to use NeoPixels in your next project. Thanks for reading - now get out there, write some Python code and have fun!