How to use NeoPixels with Raspberry Pi Pico
In this guide we will explore how to use NeoPixels using CircuitPython. This is a beginners guide so please feel free to ask any questions you may have.
What you need
- Raspberry Pi Pico or compatible board with CircuitPython installed
- NeoPixel display, this can be a single pixel or a strip
- Breadboard and 3 jumper wires
Connect NeoPixels power in to existing 3v3 OUT and ground to any ground pin on Raspberry Pi Pico. Now connect DATA IN on NeoPixels to Pin ADC1 on the Raspberry Pi Pico.
Connect the Raspberry Pi Pico to USB and we are now ready to program.
Download the following Adafruit CircuitPython libraries for NeoPixel, unzip and place copy the files to your board. This Zip contains adafruit_framebuf.py and neopixel.py.
Create a new file and name it code.py. Now import the required libraries, declare constants and initialise, see the inline comments for details.
Save the code to the board and run code.py to check for error, if there are any errors check to make sure if you are running CircuitPython and have downloaded the mentioned libraries.
We can now address any element of the array 'pixels' and control the colours. Let's start with addressing individual pixels on the strip.
# Set first Neo pixel to red pixels = (255, 0, 0) # Second to green pixels = (0, 255, 0) # Sixth to Cyan pixels = (70, 240, 240) # And so on # .... time.sleep(2) # Give your eyes some time to observe
Save and run code.py to test. If everything is connected you should now see first, second and sixth pixels light up in red, green and cyan. Note that I am testing with 8 pixel (individual light) NeoPixels strip, you should adapt the code according to your setup.
Since pixels object is basically a Python array, we can fill it with colour all at once.
pixels.fill((255, 255, 0)) time.sleep(2) # Give your eyes some time to observe
Now we know how to address individual pixels and change colours, we can create anything we like. Here is an example of a random colour generator
while True: index = 0 while index < NUM_OF_PIXELS: print("Light up pixel #", index, " with a magical colour") pixels[index] = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) time.sleep(.2) index += 1 time.sleep(1)
Save your code and run! You can also download the example below containing all code snippets from this page.
NeoPixels has been around for some time now, you will find lots of helpful examples and guides around. We used Adafruit libraries since its one of the best and the easiest of all.
Check out the Adafruit NeoPixel Github repo for more info, updates and examples.
Hope you have learned something new today. As always, feel free to post your questions, suggestions or feedback before