MicroPython random colors on a common-anode RGB LED, on an ESP32-DevKitC V4
MicroPython on an ESP32: a common-anode RGB LED on GPIO 4, 2 and 15 through 220 ohm each jumps to a random color five times a second. 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
- Common-anode versus common-cathode LEDs
- PWM brightness control with
PWM - Mixing red, green and blue
- Inverting a value with 1023 minus brightness
- Difficulty: Beginner
- Time: About 15 minutes
- Board: ESP32-DevKitC V4
- Parts: 6
How it works
This is a common-anode RGB LED. Its shared leg connects to 3.3 V, and each color leg goes through a 220 Ω resistor to a GPIO pin: red on 4, green on 2, blue on 15. A color lights when its pin is LOW, which is the opposite of what you might expect.
The program drives each pin with PWM and flips the numbers: a duty of 1023 minus the brightness. Every 200 ms it picks three random brightness values from 0 to 1023, shows the color, and prints the numbers.
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.
# Random color: a common-anode RGB LED on GPIO 4 (red), 2 (green) and
# 15 (blue). A color lights when its pin is LOW, so each duty is
# written as 1023 minus the brightness. A new random color every 200 ms.
from machine import Pin, PWM
import random
import time
red = PWM(Pin(4), freq=10000)
green = PWM(Pin(2), freq=10000)
blue = PWM(Pin(15), freq=10000)
def color(r, g, b):
red.duty(1023 - r)
green.duty(1023 - g)
blue.duty(1023 - b)
while True:
r, g, b = random.randint(0, 1023), random.randint(0, 1023), random.randint(0, 1023)
color(r, g, b)
print("color", r, g, b)
time.sleep_ms(200)
Code walkthrough
The key functions of the sketch, and what each one does.
color main.py
Takes brightness values for red, green and blue, each 0 to 1023. Since the LED lights when a pin is low, each duty is set to 1023 minus the value. So a brightness of 1023 gives a duty of 0, and that color is fully on.
def color(r, g, b):
red.duty(1023 - r)
green.duty(1023 - g)
blue.duty(1023 - b)Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The colors look backward: bright when they should be dim This project
This LED is common anode. The shared leg goes to 3.3 V, and the code subtracts from 1023. - One color never shows This project
Check that each color leg goes through its own resistor to the right pin: 4, 2 and 15. - 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.