How to use NeoPixels with Raspberry Pi Pico

How to use NeoPixels with Raspberry Pi Pico
NeoPixel beginners guide

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.

Code is licensed under the Open Source MIT License

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

Wiring

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.

Libraries

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.

These libraries are licensed under the Open Source MIT License

The code

Create a new file and name it code.py. Now import the required libraries, declare constants and initialise, see the inline comments for details.

import time
import board
import neopixel
import random

# Change this to match your NeoPixel setup
NUM_OF_PIXELS = 8

# Adjust brighness between 0 and 1
BRIGHTNESS = 0.3

# We will be using ADC1
# Change this to the output Pin you plan to use
DATA_OUT_PIN = board.A1

# Create pixels array of `NUM_OF_PIXELS` size
pixels = neopixel.NeoPixel(DATA_OUT_PIN, n=NUM_OF_PIXELS, brightness=BRIGHTNESS)
Import libs, declare constants and Initialise

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[0] = (255, 0, 0)
# Second to green
pixels[1] = (0, 255, 0)
# Sixth to Cyan
pixels[5] = (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.

0:00
/
NeoPixels demo

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