MicroPython knob turns a NeoPixel rainbow on an ESP32-DevKitC V4
MicroPython on an ESP32: a potentiometer on GPIO 36 sets where the rainbow starts on eight WS2812 LEDs on GPIO 2. The whole build, an ESP32-DevKitC V4 and 2 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 20 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.50. On Mokxi they're free.
What you'll learn
- Reading a knob with
ADCin MicroPython - Driving NeoPixels with the
neopixelmodule - Mapping a position to a color on a wheel
- Using a loop to color each pixel
- Difficulty: Intermediate
- Time: About 20 minutes
- Board: ESP32-DevKitC V4
- Parts: 5
How it works
The knob's middle pin goes to GPIO 36, and its ends go to 3.3 V and ground. The program reads it as a number from 0 to 1023 and scales that to a start position from 0 to 255 on a color wheel.
Eight NeoPixels sit on GPIO 2. Pixel number i gets the color found 32 steps further around the wheel than pixel i minus one, so together they show a rainbow. Turning the knob slides the whole rainbow around. Each color is divided by 10 to keep it dim. The program updates every 100 ms and prints the start value when it changes.
Parts list
5 parts, plus the jumper wires. They retail for about $13.50; on Mokxi they're free.
- 1 × ESP32-DevKitC V4
- 1 × PotentiometerIn Halloween Prop Kit, Starter Projects Kit.
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
- 2 × 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 |
|---|---|
| Potentiometer pin 2 | ESP32-DevKitC V4 pin VP |
| Addressable RGB LEDs pin DIN | ESP32-DevKitC V4 pin 2 |
| Potentiometer pin 1 | Ground |
| Potentiometer pin 3 | 3.3 V |
| 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.
# Soft rainbow: the knob on GPIO 36 turns the rainbow on the eight
# NeoPixels on GPIO 2, so the colors follow your hand.
from machine import ADC, Pin
import neopixel
import time
pot = ADC(Pin(36))
pot.atten(ADC.ATTN_11DB)
pot.width(ADC.WIDTH_10BIT)
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
last = -1
while True:
start = pot.read() * 255 // 1023
for i in range(8):
r, g, b = wheel(start + i * 32)
np[i] = (r // 10, g // 10, b // 10)
np.write()
if start != last:
print("start", start)
last = start
time.sleep_ms(100)
Code walkthrough
The key functions of the sketch, and what each one does.
wheel main.py
Takes a number and returns a red, green, blue color. 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. The line pos %= 255 wraps any number back onto the wheel.
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 colors never change when you turn the knob This project
Check the middle pin of the potentiometer goes to GPIO 36, with 3.3 V and ground at the ends. - The pixels stay dark This project
The data line goes to the DIN pin. Also check the pixels' VDD and VSS connections. - 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 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.