Built-in project · Raspberry Pi Pico

MicroPython rainbow on a Pico’s 8 LED module

MicroPython on a Raspberry Pi Pico: a rainbow spread over eight WS2812 LEDs on GP16 turns round and round, kept dim with a brightness scale. 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 About 15 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $7. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Addressable LEDs and one data wire
  • Using the neopixel module
  • A color wheel from position to red, green and blue
  • Animating by shifting a start position
  • Difficulty: Beginner
  • Time: About 15 minutes
  • Board: Raspberry Pi Pico
  • Parts: 3

How it works

Eight WS2812 LEDs are chained together and fed by one data wire from GP16. Each LED passes the signal along to the next, so one pin controls all eight.

The program gives each LED a color from a wheel, with each one 32 steps further around than the one before. That spreads a rainbow across the eight. Every 10 ms the whole wheel moves on 4 steps, so the rainbow turns. The colors are kept dim at 20 out of 255. When the wheel comes back to the start, it prints "round".

Parts list

3 parts, plus the jumper wires. They retail for about $7; on Mokxi they're free.

Kits that can build it

These 2 starter kits have every part in the circuit. Each kit's page lists what else it can build.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Addressable RGB LEDs pin DINRaspberry Pi Pico pin GP16
Addressable RGB LEDs pin VDD3.3 V
Addressable RGB LEDs pin VSSGround

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.

main.py · Raspberry Pi Pico
# A rainbow that turns: each of the 8 LEDs on GP16 gets a color from the
# wheel, an eighth of the way around from the one before, and the whole
# wheel moves on a step at a time.
from machine import Pin
import neopixel
import time

np = neopixel.NeoPixel(Pin(16), 8)
BRIGHT = 20                          # of 255

def wheel(pos):
    pos %= 256
    if pos < 85:
        c = (255 - pos * 3, pos * 3, 0)
    elif pos < 170:
        pos -= 85
        c = (0, 255 - pos * 3, pos * 3)
    else:
        pos -= 170
        c = (pos * 3, 0, 255 - pos * 3)
    return tuple(v * BRIGHT // 255 for v in c)

start = 0
while True:
    for i in range(8):
        np[i] = wheel(start + i * 32)
    np.write()
    start = (start + 4) % 256
    if start == 0:
        print("round")
    time.sleep_ms(10)

Code walkthrough

The key functions of the sketch, and what each one does.

wheel main.py

Turns a position from 0 to 255 into a color. The wheel has three parts, and in each one a color fades out while the next fades in. At the end, every value is scaled by BRIGHT out of 255, to keep the LEDs dim and comfortable to look at.

def wheel(pos):
    pos %= 256
    if pos < 85:
        c = (255 - pos * 3, pos * 3, 0)
    elif pos < 170:
        pos -= 85
        c = (0, 255 - pos * 3, pos * 3)
    else:
        pos -= 170
        c = (pos * 3, 0, 255 - pos * 3)
    return tuple(v * BRIGHT // 255 for v in c)

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The LEDs stay dark This project
    Check GP16 goes to DIN, and the strip has 3.3 V on VDD and ground on VSS.
  • The LEDs are too dim or too bright This project
    Change BRIGHT. It is out of 255.
  • The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
    Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND.
  • The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
    Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code.
  • The Pico does not show up as a drive, or the program will not load. Raspberry Pi Pico
    Hold the BOOTSEL button while you plug in USB. A drive named RPI-RP2 appears. Then copy the UF2 file onto it.
  • A 5 V signal or sensor is wired to a GPIO pin. Raspberry Pi Pico
    Pico GPIO pins are 3.3 V only and are not 5 V tolerant. Use a level shifter or a voltage divider for any 5 V output going into the board.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.