Built-in project · Raspberry Pi Pico

MicroPython knob rainbow on a Pico: a pot turns 8 LEDs

MicroPython on a Raspberry Pi Pico: a potentiometer on GP26 slides a rainbow along an 8 LED WS2812 module on GP15. The whole build, a Raspberry Pi Pico and 2 more parts, 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.

Intermediate About 20 minutes to build and run. Runs in your browser. Free, and no account needed.

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

The circuit runs here once the page has loaded.

What you'll learn

  • Reading a knob with the Pico's ADC
  • Controlling addressable LEDs with neopixel
  • A color wheel from position 0 to 255
  • Redrawing only when something changes
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: Raspberry Pi Pico
  • Parts: 5

How it works

A potentiometer on GP26 gives the Pico a voltage from 0 to 3.3 V. An 8 LED WS2812 strip on GP15 shows the colors, using one data wire (DIN). Both parts run from 3.3 V.

The code reads the knob and squeezes the reading into a number from 0 to 255. That number is where the rainbow starts on a color wheel. Each of the 8 LEDs gets a color 32 steps further along the wheel. Turn the knob and the colors slide along the strip. The code only redraws and prints when the knob value changes.

Parts list

5 parts, plus the jumper wires. They retail for about $7.50; 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 GP15
Potentiometer pin 2Raspberry Pi Pico pin GP26
Potentiometer pin 1Ground
Potentiometer pin 33.3 V
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
# Turn the rainbow: the knob on GP26 picks where on the color wheel the
# 8 LED module on GP15 starts, so turning it slides the colors along.
from machine import ADC, Pin
import neopixel
import time

np = neopixel.NeoPixel(Pin(15), 8)
knob = ADC(26)
BRIGHT = 20

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)

last = -1
while True:
    start = knob.read_u16() >> 8          # 0 to 255
    if start != last:
        for i in range(8):
            np[i] = wheel(start + i * 32)
        np.write()
        print("start", start)
        last = start
    time.sleep_ms(50)

Code walkthrough

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

wheel main.py

Turns a position into a color. The wheel has three parts of about 85 steps. In each part one color fades down while the next fades up, and the third is 0. The result is dimmed with BRIGHT (20 out of 255), so the strip is not blinding.

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.

  • No LEDs light This project
    Check that GP15 goes to the strip's DIN, and that VDD is on 3.3 V and VSS on ground.
  • The colors do not change when you turn the knob This project
    The middle pin of the potentiometer must go to GP26, with 3.3 V and ground on the outer pins.
  • The reading jumps around or only covers part of the range. Potentiometer
    Wire the two outer legs to 5 V and ground and the middle leg (the wiper) to an analog pin. Using the wrong two legs gives a dead or partial range.
  • Turning the knob does nothing, or the value is stuck at 0 or 1023. Potentiometer
    Check that the wiper goes to an analog input (A0 to A5 on an Uno), not a digital pin. Make sure both ends are connected, since a floating end gives noise.
  • 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.