CircuitPython NeoPixel


NeoPixels are a revolutionary and ultra-popular way to add lights and color to your project. These stranded RGB lights have the controller inside the LED, so you just push the RGB data and the LEDs do all the work for you! They're a perfect match for CircuitPython.

You can drive 300 pixels with brightness control (e.g. setting brightness=0.2 to set it to 20% brightness) and 1000 pixels without (e.g. not setting brightness at all or setting brightness=1.0 in object creation). That's because to adjust the brightness we have to dynamically re-create the datastream each write.

Here's an example with a lot of different visual effects you can check out. You'll need the neopixel.mpy library file if you don't have it yet!

# Circuit Playground NeoPixel
import time
import board
import neopixel


# choose which demos to play
# 1 means play, 0 means don't!
color_chase_demo = 1
flash_demo = 1
rainbow_demo = 1
rainbow_cycle_demo = 1

def wheel(pos):
    if pos < 0 or pos > 255:
        return (0, 0, 0)
    if pos < 85:
    if pos < 170:
        pos -= 85
    pos -= 170

def color_chase(color, wait):
    for i in range(10):
        pixels[i] = color

def rainbow_cycle(wait):
    for j in range(255):
        for i in range(10):
            pixels[i] = wheel(rc_index & 255)

def rainbow(wait):
    for j in range(255):
        for i in range(len(pixels)):
            idx = int(i + j)
            pixels[i] = wheel(idx & 255)

RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHITE = (255, 255, 255)
OFF = (0, 0, 0)

while True:
    if color_chase_demo:
        color_chase(RED, 0.1)  # Increase the number to slow down the color chase
        color_chase(YELLOW, 0.1)
        color_chase(GREEN, 0.1)
        color_chase(CYAN, 0.1)
        color_chase(BLUE, 0.1)
        color_chase(PURPLE, 0.1)
        color_chase(OFF, 0.1)

    if flash_demo:
        # Increase or decrease to change the speed of the solid color change.

    if rainbow_cycle_demo:
        rainbow_cycle(0.05)  # Increase the number to slow down the rainbow.

    if rainbow_demo:
        rainbow(0.05)  # Increase the number to slow down the rainbow.

The NeoPixel object's argument list requires the pin you'll use (any pin can be used) and the number of pixels. There are two optional arguments, brightness (range from 0 off to 1.0 full brightness) and auto_write. auto_write defaults to True when not set. When auto_write is set to True, every change is immediately written to the strip of pixels, which is easier to use but way slower. if you set auto_write=False then you will  have to call when you want to actually write color data out.

You can easily set colors by indexing into the location pixels[n] = (red, green, blue). For example, pixels[0] = (100, 0, 0) will set the first pixel to a medium-brightness red, and pixels[2] = (0, 255, 0) will set the third pixel to bright green. Then, if you have auto_write=False don't forget to call!

You aren't limited to the on-board NeoPixels -- externally connected NeoPixels can be driven by any Digital IO pin.

For powering the pixels from the board, the 3.3V regulator output can handle about 500mA peak which is about 50 pixels with 'average' use. If you want really bright lights and a lot of pixels, we recommend powering direct from the power source. On the Circuit Playground Express this is the Vout pad - that pad has direct power from USB or BAT (battery), depending on which is higher voltage. 

Verify the wiring on your strip or device - plugging into the 'DOUT' side is a common mistake! Wire up NeoPixels only while the Circuit Playground Express is not on, to avoid possible damage!

If the power to the NeoPixels is > 5.5V you may have some difficulty driving some strips, in which case you may need to lower the voltage to 4.5-5V or use a level shifter

We have a ton more information on general purpose NeoPixel know-how at our NeoPixel UberGuide
This page (CircuitPython NeoPixel) was last updated on Aug 06, 2020.