MicroPython NeoPixel: eight WS2812 LEDs on GPIO 2, on an ESP32-DevKitC V4
MicroPython on an ESP32: the neopixel module fills a strip of eight WS2812 LEDs on GPIO 2 one by one in red, green, blue and white. 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
- Addressable LEDs on a single data line
- Colors as red, green, blue triples
- Setting pixels, then calling write
- Keeping brightness low
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-DevKitC V4
- Parts: 3
How it works
Eight WS2812 LEDs sit in a chain. Each one has an RGB color you can set, and they all share one data line, DIN, which is wired to GPIO 2. They get 3.3 V and ground.
The program fills the chain one pixel at a time in red, then green, blue and white, then fills it with black to turn everything off. It waits 50 ms between pixels and uses a level of only 10 out of 255, so the light stays dim.
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.
# NeoPixel: eight WS2812 LEDs on GPIO 2. Fill them one at a time in red,
# green, blue and white, then turn them all off, at a low brightness.
from machine import Pin
import neopixel
import time
np = neopixel.NeoPixel(Pin(2), 8)
level = 10 # out of 255: easy on the eyes
colors = [(level, 0, 0), (0, level, 0), (0, 0, level), (level, level, level), (0, 0, 0)]
while True:
for c in colors:
for i in range(8):
np[i] = c
np.write()
time.sleep_ms(50)
print("color", c)
Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The pixels look very dim. This project
That is on purpose. Raiselevelabove 10, up to 255, for more brightness. - Nothing lights up. This project
Check that DIN goes to GPIO 2, and that the count of 8 inNeoPixelmatches your strip. - 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.