MicroPython color mixer on a Pico: three knobs, RGB LED
MicroPython on a Raspberry Pi Pico: knobs on GP26, GP27 and GP28 set the blue, green and red of a common-anode RGB LED on GP13 to GP15. The whole build, a Raspberry Pi Pico and 7 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 30 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $5.90. On Mokxi they're free.
What you'll learn
- Reading analog inputs on a Pico
- PWM output with
duty_u16 - Common anode LEDs need inverted duty
- Pairing each input with its output in a list
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: Raspberry Pi Pico
- Parts: 12
How it works
Three knobs on GP26, GP27 and GP28 mix one RGB LED. Blue is on GP13, green on GP14 and red on GP15. The program reads each knob and sets the matching color's brightness.
The LED is common anode, wired to 3.3 V. A color lights when its pin goes low, so the code writes 65535 - value as the PWM duty. Each PWM runs at 1000 Hz.
The program prints a line only when a level changes. It shows each knob as a percentage, in the order blue, green and red.
Parts list
12 parts, plus the jumper wires. They retail for about $5.90; 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.
- 3 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 4 × 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 GP13 |
| Resistor, 220 Ω (2) pin 1 | Raspberry Pi Pico pin GP14 |
| Resistor, 220 Ω (1) pin 1 | Raspberry Pi Pico pin GP15 |
| Potentiometer (3) pin 2 | Raspberry Pi Pico pin GP28 |
| Potentiometer (2) pin 2 | Raspberry Pi Pico pin GP27 |
| Potentiometer (1) pin 2 | Raspberry Pi Pico pin GP26 |
| 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 |
| Potentiometer (1) pin 1 | Ground |
| Potentiometer (1) pin 3 | 3.3 V |
| Potentiometer (2) pin 1 | Ground |
| Potentiometer (2) pin 3 | 3.3 V |
| Potentiometer (3) pin 1 | Ground |
| Potentiometer (3) pin 3 | 3.3 V |
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.
# Mix a color with three knobs: GP26 sets blue (GP13), GP27 green (GP14)
# and GP28 red (GP15) on a common-anode RGB LED, so a pin low is a color on.
from machine import ADC, Pin, PWM
import time
pairs = [(ADC(26), PWM(Pin(13))), (ADC(27), PWM(Pin(14))), (ADC(28), PWM(Pin(15)))]
for _, led in pairs:
led.freq(1000)
last = None
while True:
levels = []
for knob, led in pairs:
value = knob.read_u16()
led.duty_u16(65535 - value)
levels.append(value * 100 // 65535)
if levels != last:
print("blue %d%% green %d%% red %d%%" % tuple(levels))
last = levels
time.sleep_ms(50)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The brightest color is when the knob is at zero This project
This is expected here. The LED is common anode, so the duty is inverted. - The knobs control the wrong colors This project
Look at the comments: GP26 is blue, GP27 is green and GP28 is red. - 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 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.