MicroPython color wheel on a Pico’s RGB LED
MicroPython on a Raspberry Pi Pico: a common-anode RGB LED on GP13, GP12 and GP11 drifts through a color wheel, red to green to blue. The whole build, a Raspberry Pi Pico and 4 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.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $4.40. On Mokxi they're free.
What you'll learn
- Common-anode RGB LEDs
- PWM with
duty_u16 - A color wheel from one number
- Why the duty is flipped for a common-anode LED
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 6
How it works
This is a common-anode RGB LED. The shared leg goes to 3.3 V, and the red, green and blue legs go through 220 Ω resistors to GP13, GP12 and GP11. A color lights when its pin is LOW, so the program flips each value before sending it.
A position runs from 0 to 255 and the color wheel turns it into red, green and blue levels, going from red to green to blue and back to red. The program steps through all 256 positions with 10 ms between them, then prints "round" and starts again.
Parts list
6 parts, plus the jumper wires. They retail for about $4.40; on Mokxi they're free.
- 1 × Raspberry Pi PicoIn Fight Stick Kit.
- 1 × RGB LEDIn Starter Projects Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × Power, 3.3 V
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 pin | Connects to |
|---|---|
| Resistor, 220 Ω (3) pin 1 | Raspberry Pi Pico pin GP11 |
| Resistor, 220 Ω (2) pin 1 | Raspberry Pi Pico pin GP12 |
| Resistor, 220 Ω (1) pin 1 | Raspberry Pi Pico pin GP13 |
| Resistor, 220 Ω (1) pin 2 | RGB LED pin R |
| RGB LED pin C | 3.3 V |
| Resistor, 220 Ω (2) pin 2 | RGB LED pin G |
| Resistor, 220 Ω (3) pin 2 | RGB LED pin B |
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.
# A color wheel on the common-anode RGB LED (red GP13, green GP12, blue
# GP11): the position runs from 0 to 255 and wheel() turns it into a
# color, red to green to blue and back to red.
from machine import Pin, PWM
import time
leds = [PWM(Pin(n)) for n in (13, 12, 11)]
for led in leds:
led.freq(1000)
def wheel(pos):
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
while True:
for pos in range(256):
for led, level in zip(leds, wheel(pos)):
led.duty_u16(65535 - level * 257)
time.sleep_ms(10)
print("round")
Code walkthrough
The key functions of the sketch, and what each one does.
wheel main.py
Turns a position from 0 to 255 into three levels for red, green and blue. In each third of the wheel, one color fades out while the next fades in. The main loop sends each level to its pin as 65535 - level * 257, which flips it for the common anode.
def wheel(pos):
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 * 3Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors are inverted: dark when they should be bright This project
Common-anode LEDs light when the pin is low. The shared leg must go to 3.3 V, and the code subtracts from 65535. - One color is missing This project
Check each color leg has its own 220 Ω resistor to the right pin: red GP13, green GP12, blue GP11. - The colors are wrong or one color never lights. RGB LED
Check whether your LED is common cathode (shared leg to ground) or common anode (shared leg to 5 V). Common anode works backwards: LOW turns a color on. - One color is much brighter than the others, or the LED runs too hot. RGB LED
Use one resistor per color leg, never one for the shared leg. Typical values are 220 ohm for red and 100 to 150 ohm for green and blue on 5 V. - The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure. - A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground. - 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.