MicroPython rainbow on eight NeoPixels, on an ESP32-DevKitC V4
MicroPython on an ESP32: a color wheel spread across eight WS2812 LEDs on GPIO 2 turns step by step, a rainbow going round. The whole build, an ESP32-DevKitC V4 and 1 more part, 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13. On Mokxi they're free.
What you'll learn
- A color wheel that turns one number into a color
- Spreading colors across pixels with an offset
- Animating by shifting a start value
- Wrapping a counter with the modulo operator
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
Eight WS2812 LEDs share one data line on GPIO 2. The program gives each pixel a color from a color wheel, spacing them 32 steps apart, so together they show a rainbow.
Every 10 ms the start point moves along the wheel by 4 steps, so the rainbow appears to turn. Each color is divided by 10, so the pixels run at a tenth of full brightness.
Parts list
3 parts, plus the jumper wires. They retail for about $13; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights 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 |
|---|---|
| Addressable RGB LEDs pin DIN | ESP32-DevKitC V4 pin 2 |
| Addressable RGB LEDs pin VDD | 3.3 V |
| Addressable RGB LEDs pin VSS | Ground |
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.
# Rainbow: the color wheel spread across the eight pixels, turning one
# step at a time, at a tenth of full brightness.
from machine import Pin
import neopixel
import time
np = neopixel.NeoPixel(Pin(2), 8)
def wheel(pos):
pos %= 255
if pos < 85:
return 255 - pos * 3, pos * 3, 0
if pos < 170:
pos -= 85
return 0, 255 - pos * 3, pos * 3
pos -= 170
return pos * 3, 0, 255 - pos * 3
start = 0
while True:
for i in range(8):
r, g, b = wheel(start + i * 32)
np[i] = (r // 10, g // 10, b // 10)
np.write()
start = (start + 4) % 255
if start < 4:
print("turn")
time.sleep_ms(10)
Code walkthrough
The key functions of the sketch, and what each one does.
wheel main.py
Takes a position and returns a red, green, blue triple. The wheel has three parts of 85 steps each. In each part, one color fades out while the next fades in, so the colors blend smoothly and loop back to the start.
def wheel(pos):
pos %= 255
if pos < 85:
return 255 - pos * 3, pos * 3, 0
if pos < 170:
pos -= 85
return 0, 255 - pos * 3, pos * 3
pos -= 170
return pos * 3, 0, 255 - pos * 3Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The rainbow moves too fast or too slow. This project
Change thetime.sleep_ms(10)pause or the step of 4 added tostart. - The pixels are too dim. This project
The// 10divides each color to a tenth. Use a smaller divisor for more brightness. - The first LED flickers, shows wrong colors, or the whole strip glitches. Addressable RGB LEDs
Join the grounds of the strip and the controller. Add a 300 to 500 ohm resistor in series with the data line. Put a 1000 uF capacitor across the strip's 5 V and GND. - The strip works at low brightness but dims, turns yellow, or resets when many LEDs go white. Addressable RGB LEDs
Each LED can draw about 60 mA at full white. Use a 5 V supply sized for the count, feed power in at several points, and lower the brightness in code. - 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.