To get you started with how to program your Pico in CircuitPython, especially for those who may have started out with the official MicroPython setup, we've 'ported' the Getting Started with MicroPython on Pico book examples to CircuitPython. The book is awesome, please download/purchase it to support Raspberry Pi Press!

It's easy to control addressable RGB NeoPixel LEDs with the Raspberry Pi Pico, CircuitPython and the Adafruit CircuitPython NeoPixel library. Simply connect the LEDs to your Pico board, copy the library to your CIRCUITPY drive, and update your code.py file. That's all there is to it.

This section will show you how to wire up a NeoPixel LED strip to your Pico board, install the NeoPixel library, and walk through a few examples of controlling the LEDs.

Adding glowy color to your projects has never been easier: no more soldering or stripping wires, clip 'em on and glow! This Adafruit NeoPixel LED Strip with Alligator...
$12.50
In Stock

Wiring the NeoPixel LED Strip

The first step is to connect the NeoPixel LEDs to your Pico board. Wire them as shown below.

Note that while your Pico board can control a considerable number of NeoPixel LEDs, the power you can draw from the 5V VBUS pin on your Pico board is limited. The examples below assume a strip of 30 NeoPixel LEDs, which the Pico board is capable of handling. However, if you want to control more than that, it is suggested that you use an external power supply to power your LEDs. Check out the NeoPixel guide for everything there is to know about NeoPixel LEDs, including power requirements and suggestions.

  • Board GND to NeoPixel GND
  • Board VBUS to NeoPixel 5V (power)
  • Board GP0 to NeoPixel signal

Installing the Adafruit CircuitPython NeoPixel Library

You'll need to install the Adafruit CircuitPython NeoPixel library on your CircuitPython board.

Next you'll need to install the necessary library to use the NeoPixels -- carefully follow the steps to find and install this library from Adafruit's CircuitPython library bundle.  Our CircuitPython starter guide has a great page on how to install the library bundle.

Copy the following file from the bundle to the lib folder on your CIRCUITPY drive:

  • neopixel.mpy

Before continuing make sure your board's lib folder or root filesystem has the neopixel.mpy file copied over.

Programming NeoPixel LEDs

Now that you've wired up your NeoPixel LEDs and loaded the NeoPixel library onto your CIRCUITPY drive, it's time to begin programming.

Update your code.py to the following. You should verify that num_pixels matches the number of NeoPixel LEDs you have connected to your board, if you connected a different form factor or strip length to your Pico board.

"""
NeoPixel example for Pico. Turns the NeoPixels red.

REQUIRED HARDWARE:
* RGB NeoPixel LEDs connected to pin GP0.
"""
import board
import neopixel

# Update this to match the number of NeoPixel LEDs connected to your board.
num_pixels = 30

pixels = neopixel.NeoPixel(board.GP0, num_pixels)
pixels.brightness = 0.5

while True:
    pixels.fill((255, 0, 0))

The NeoPixels light up red!

Now a quick look at the code. First, you import the necessary module and library.

import board
import neopixel
Modules are built into CircuitPython. Libraries are separate files or groups of files saved to the CIRCUITPY drive. Regardless, you import them both the same way.

Next you create a variable to set the number of pixels, which defaults to 30.

num_pixels = 30

Then, you set up the NeoPixels. The neopixel object requires two things: the pin the NeoPixels are connected to, and the number of NeoPixels connected. In this case, you connected the strip to GP0 on your Pico board, and you set the number of pixels in the previous line of code.

pixels = neopixel.NeoPixel(board.GP0, num_pixels)

Following that, you set the brightness of the NeoPixels to 0.5, or 50%. brightness expects a float between 0.0 and 1.0, where 0 is off, and 1 is 100% brightness. Point the NeoPixels away from you, or put a sheet of paper over them to diffuse them a bit, and try setting brightness to 1. They can get really bright! That's why we set the brightness to half, which is still quite bright. You can set it even lower if you like.

pixels.brightness = 0.5

LED colors are set using a combination of red, green, and blue, often in the form of an (RG, B) tuple. Each member of the tuple is set to a number between 0 and 255 that determines the amount of each color present. Red, green and blue in different combinations can create all the colors in the rainbow! So, for example, to set the LED to red, the tuple would be (255, 0, 0), which has the maximum level of red, and no green or blue. Green would be (0, 255, 0), etc. For the colors between, you set a combination, such as cyan which is (0, 255, 255), with equal amounts of green and blue.

Inside your loop, you use the fill() function to "fill", or turn on, the NeoPixels a specified color. The fill() function works with both RGB tuple colors, and hex colors. This example turns them on red.

while True:
    pixels.fill((255, 0, 0))

Note the double parentheses - this is specific to setting the color using an RGB tuple. The fill() function expects one argument, and the entire tuple, including its parentheses, is that single argument. Without the parentheses around the tuple, fill() will see it as three arguments separated by commas, and your code will not run.

Now, try turning them green. Update the loop to the following:

while True:
    pixels.fill((0, 255, 0))

Green!

Now try blue.

while True:
    pixels.fill((0, 0, 255))

Easy!

What if you wanted to make them light up the three colors in sequence without manually updating your code each time? For that, you'll need time.

Update your code.py to the following.

"""
NeoPixel example for Pico. Turns the NeoPixels red, green, and blue in sequence.

REQUIRED HARDWARE:
* RGB NeoPixel LEDs connected to pin GP0.
"""
import time
import board
import neopixel

# Update this to match the number of NeoPixel LEDs connected to your board.
num_pixels = 30

pixels = neopixel.NeoPixel(board.GP0, num_pixels)
pixels.brightness = 0.5

while True:
    pixels.fill((255, 0, 0))
    time.sleep(0.5)
    pixels.fill((0, 255, 0))
    time.sleep(0.5)
    pixels.fill((0, 0, 255))
    time.sleep(0.5)

You can change the colors by editing the color tuples, and the timing by updating the number of seconds given to time.sleep().

For example, update the loop to the following for three new colors and a slower change.

while True:
    pixels.fill((255, 255, 0))
    time.sleep(1)
    pixels.fill((0, 255, 255))
    time.sleep(1)
    pixels.fill((255, 0, 255))
    time.sleep(1)

And finally, what about rainbows? Rainbows involve using colorwheel and some math.

Update your code.py to the following.

"""
NeoPixel example for Pico. Displays a rainbow on the NeoPixels.

REQUIRED HARDWARE:
* RGB NeoPixel LEDs connected to pin GP0.
"""
import time
import board
from rainbowio import colorwheel
import neopixel

# Update this to match the number of NeoPixel LEDs connected to your board.
num_pixels = 30

pixels = neopixel.NeoPixel(board.GP0, num_pixels, auto_write=False)
pixels.brightness = 0.5


def rainbow(speed):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = (i * 256 // num_pixels) + j
            pixels[i] = colorwheel(pixel_index & 255)
        pixels.show()
        time.sleep(speed)


while True:
    rainbow(0)

In addition to the three previous imports, we also import colorwheel from the built-in _pixelbuf module.

from _pixelbuf import colorwheel

Then, there's a change to the pixel setup. Note, that in the neopixel object, you now set auto_write=False. This means that when you tell the pixels to do something, nothing will happen unless you call pixels.show(). This is necessary for the rainbow function to work. Be aware that if you add in some of the code from above, you'll have to call pixels.show() after filling the pixels a given color or they won't turn on!

pixels = neopixel.NeoPixel(board.GP0, num_pixels, auto_write=False)
If you write some NeoPixel code, and your pixels aren't lighting up, check that auto_write isn't set to False, or make sure you call pixels.show()!

Next comes the rainbow() function. This function takes one argument, speed in seconds.

def rainbow(speed):
    for j in range(255):
        for i in range(num_pixels):
            pixel_index = (i * 256 // num_pixels) + j
            pixels[i] = colorwheel(pixel_index & 255)
        pixels.show()
        time.sleep(speed)

Finally, you call the rainbow() function in a loop. For fast rainbows, set it to 0. To slow it down, increase the number.

while True:
    rainbow(0)

Rainbows!

That's all there is to basic NeoPixel LED control.

If you want to get really fancy and take it further, you can check out the guide on the Adafruit CircuitPython LED Animation library, which shows how the library makes displaying animations on RGB LEDs super simple.

This guide was first published on Jan 21, 2021. It was last updated on 2021-03-03 12:34:49 -0500.

This page (NeoPixel LEDs) was last updated on Oct 15, 2021.

Text editor powered by tinymce.