MicroPython color mixer: three knobs, one RGB LED, on an ESP32-DevKitC V4
MicroPython on an ESP32: knobs on GPIO 36, 39 and 34 set the red, green and blue of a common-anode RGB LED on GPIO 15, 2 and 5. The whole build, an ESP32-DevKitC V4 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 $11.90. On Mokxi they're free.
What you'll learn
- Reading analog knobs on an ESP32
- PWM mixes brightness
- Common anode LEDs work backward
- Using a helper function to avoid repeating code
- Lists and
zippair knobs with LEDs
- Difficulty: Intermediate
- Time: About 30 minutes
- Board: ESP32-DevKitC V4
- Parts: 12
How it works
Three knobs on GPIO 36, 39 and 34 each set one color of an RGB LED. The red LED pin is GPIO 15, green is GPIO 2, and blue is GPIO 5. Turn a knob and its color gets brighter or dimmer.
The program reads each knob as a number from 0 to 1023 and writes it to a PWM pin at 10000 Hz. This particular LED is common anode, with its shared pin on 3.3 V. So a color lights when its pin goes low, and each duty is written as 1023 minus the reading.
Every 200 ms the program also prints the three readings to the console.
Parts list
12 parts, plus the jumper wires. They retail for about $11.90; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 3 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × RGB LEDIn Starter Projects Kit.
- 3 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 4 × Power, 3.3 V
Kits that can build it
These 3 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 |
|---|---|
| Potentiometer (1) pin 2 | ESP32-DevKitC V4 pin VP |
| Potentiometer (2) pin 2 | ESP32-DevKitC V4 pin VN |
| Potentiometer (3) pin 2 | ESP32-DevKitC V4 pin 34 |
| Resistor, 220 Ω (3) pin 1 | ESP32-DevKitC V4 pin 5 |
| Resistor, 220 Ω (2) pin 1 | ESP32-DevKitC V4 pin 2 |
| Resistor, 220 Ω (1) pin 1 | ESP32-DevKitC V4 pin 15 |
| 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 |
| 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.
# Soft colorful light: three knobs mix one RGB LED. The knob on GPIO 36
# sets red (GPIO 15), 39 sets green (GPIO 2) and 34 sets blue (GPIO 5).
# The LED is common anode, so each duty is written inverted.
from machine import ADC, Pin, PWM
import time
def knob(gpio):
a = ADC(Pin(gpio))
a.atten(ADC.ATTN_11DB)
a.width(ADC.WIDTH_10BIT)
return a
knobs = [knob(36), knob(39), knob(34)]
leds = [PWM(Pin(15), freq=10000), PWM(Pin(2), freq=10000), PWM(Pin(5), freq=10000)]
while True:
mix = [k.read() for k in knobs]
for led, level in zip(leds, mix):
led.duty(1023 - level)
print("mix", mix[0], mix[1], mix[2])
time.sleep_ms(200)
Code walkthrough
The key functions of the sketch, and what each one does.
knob main.py
Takes a GPIO number and builds an ADC on it. It sets 11 dB attenuation so the full 3.3 V range can be read, and 10 bit width so readings run from 0 to 1023. It returns the ADC, and the program calls it for 36, 39 and 34.
def knob(gpio):
a = ADC(Pin(gpio))
a.atten(ADC.ATTN_11DB)
a.width(ADC.WIDTH_10BIT)
return aTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors are inverted This project
Because the LED is common anode, the duty must be1023 - level. Do not remove the inversion. - A knob does nothing This project
Check each knob has 3.3 V on one end, ground on the other, and the middle pin on the right GPIO. - 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 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.