MicroPython color wheel on a common-anode RGB LED, on an ESP32-DevKitC V4
MicroPython on an ESP32: PWM on GPIO 4, 2 and 15 walks a common-anode RGB LED smoothly around the color wheel, red to green to blue. The whole build, an ESP32-DevKitC V4 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 $10.40. On Mokxi they're free.
What you'll learn
- PWM to mix colors on an RGB LED
- Common-anode LEDs light on LOW
- A color wheel as a smooth blend of two colors
- Inverting a duty value to match the wiring
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32-DevKitC V4
- Parts: 6
How it works
This RGB LED has a 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 GPIO 4, green on GPIO 2 and blue on GPIO 15. A color lights when its pin goes low, so more low time means a brighter color.
The MicroPython code runs PWM at 1000 Hz on each pin and walks around a color wheel from red to green to blue and back to red. Each step moves a little and waits 15 ms, so the color blends smoothly. It prints "wheel" after every full trip around.
Parts list
6 parts, plus the jumper wires. They retail for about $10.40; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 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 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 |
|---|---|
| Resistor, 220 Ω (1) pin 1 | ESP32-DevKitC V4 pin 4 |
| Resistor, 220 Ω (2) pin 1 | ESP32-DevKitC V4 pin 2 |
| Resistor, 220 Ω (3) pin 1 | ESP32-DevKitC V4 pin 15 |
| 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.
# Gradient color: the RGB LED walks around a color wheel, red to green to
# blue and back to red, one small step every 15 ms.
from machine import Pin, PWM
import time
red = PWM(Pin(4), freq=1000)
green = PWM(Pin(2), freq=1000)
blue = PWM(Pin(15), freq=1000)
def wheel(pos):
# pos 0 to 1022 around the wheel: two colors share it at any point
pos %= 1023
if pos < 341:
return 1023 - pos * 3, pos * 3, 0
if pos < 682:
pos -= 341
return 0, 1023 - pos * 3, pos * 3
pos -= 682
return pos * 3, 0, 1023 - pos * 3
while True:
for pos in range(0, 1023, 3):
r, g, b = wheel(pos)
red.duty(1023 - r) # common anode: LOW lights
green.duty(1023 - g)
blue.duty(1023 - b)
time.sleep_ms(15)
print("wheel")
Code walkthrough
The key functions of the sketch, and what each one does.
wheel main.py
Takes a position from 0 to 1022 and returns a red, green and blue amount. The wheel has three parts, each about 341 steps long. In each part one color fades out while the next fades in, and the third stays at 0. That is why only two colors share the wheel at any point.
def wheel(pos):
# pos 0 to 1022 around the wheel: two colors share it at any point
pos %= 1023
if pos < 341:
return 1023 - pos * 3, pos * 3, 0
if pos < 682:
pos -= 341
return 0, 1023 - pos * 3, pos * 3
pos -= 682
return pos * 3, 0, 1023 - pos * 3Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors look backwards or inverted This project
The LED is common anode, so the code writes1023 - rto the duty. Keep that inversion, or the dark colors will be bright. - Only some colors show This project
Check that red, green and blue go to GPIO 4, 2 and 15, each through its own 220 ohm resistor, and that the common leg is on 3.3 V. - 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 board is not found, or upload fails with a connecting message. ESP32-DevKitC V4
Use a USB cable that carries data, not charge only. Pick the ESP32 Dev Module board. If it still fails, hold BOOT while uploading starts, then release. - Some pins never read right, or the board will not boot with a wire attached. ESP32-DevKitC V4
GPIO34 to 39 are input only and have no pull-ups. GPIO6 to 11 run the flash, so leave them alone. GPIO0, 2 and 12 are boot pins; avoid pulling them the wrong way at reset.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.