MicroPython random colors on a Pico’s RGB LED
MicroPython on a Raspberry Pi Pico: PWM on GP13, GP12 and GP11 gives a common-anode RGB LED a random color five times a second. 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 versus common-cathode LEDs
- Inverting a PWM duty
- Mixing colors with three PWM pins
- Making random numbers in MicroPython
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Raspberry Pi Pico
- Parts: 6
How it works
This RGB LED is common anode: its shared leg goes to 3.3 V, and each color has its own 220 ohm resistor to a pin. Red is on GP13, green on GP12 and blue on GP11. A color is brightest when its pin is low.
Every 200 ms the program picks a random level from 0 to 65535 for each color. It writes the PWM duty as 65535 minus that level, to flip it for the common anode, and prints the three levels.
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 random color every 200 ms on a common-anode RGB LED: red on GP13,
# green on GP12, blue on GP11. The common leg is on 3.3 V, so a color is
# brightest when its pin is low: the duty is written as 65535 minus it.
from machine import Pin, PWM
import random
import time
leds = [PWM(Pin(n)) for n in (13, 12, 11)]
for led in leds:
led.freq(10000)
while True:
color = [random.randint(0, 65535) for _ in leds]
for led, level in zip(leds, color):
led.duty_u16(65535 - level)
print("color", color)
time.sleep_ms(200)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors look wrong or inverted. This project
The common leg is on 3.3 V, so keep the 65535 minus level flip. Without it, brighter values would give darker colors. - One color never shows. This project
Check each pin and resistor: GP13 to red, GP12 to green and GP11 to blue. - 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.