CircuitPython NeoPixel rainbow on a Pico
CircuitPython on a Raspberry Pi Pico: Adafruit’s neopixel library turns a rainbow around eight WS2812 LEDs on GP16, sent by the Pico’s PIO. The whole build, a Raspberry Pi Pico and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner Runs in your browser. Free, and no account needed.
The parts retail for about $7. On Mokxi they're free.
The code
The MicroPython code exactly as the editor opens it. Change a line there and press Run: the board runs it on the official MicroPython firmware, with nothing to compile.
# CircuitPython on a Raspberry Pi Pico: a NeoPixel rainbow.
# Eight WS2812 LEDs on GP16, driven by Adafruit's neopixel library (which
# the editor puts in lib/ for you) through the Pico's PIO.
import board
import neopixel
import time
pixels = neopixel.NeoPixel(board.GP16, 8, brightness=0.3, auto_write=False)
def wheel(pos):
# 0 to 255 around the color wheel: red, green, blue and back to red.
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)
print("Rainbow on 8 NeoPixels")
step = 0
while True:
for i in range(8):
pixels[i] = wheel((i * 32 + step) % 256)
pixels.show()
if step % 64 == 0:
print("step", step, "first pixel", pixels[0])
step = (step + 4) % 256
time.sleep(0.02)
Parts list
2 parts, plus the jumper wires. They retail for about $7; on Mokxi they're free.
How it is wired
3 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.
- Raspberry Pi Pico pin VBUS; Addressable RGB LEDs pin VDD
- Ground: Raspberry Pi Pico pin GND; Addressable RGB LEDs pin VSS
- Raspberry Pi Pico pin GP16; Addressable RGB LEDs pin DIN
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.