Arcade button lighting on an ESP32-C3
Four arcade buttons on an ESP32-C3, each lighting its own pair of WS2812B LEDs the moment it is pressed and fading out when it is let go. The whole build, an ESP32-C3 and 5 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 25 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $22. On Mokxi they're free.
What you'll learn
- Reading buttons with INPUT_PULLUP, where pressed means LOW
- Driving addressable WS2812B LEDs from one data pin
- Fading with a per-frame multiply instead of a delay
- Timing frames with millis so button reads stay fast
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 7
How it works
Four arcade buttons connect to pins 0, 1, 10 and 4, each with its other leg on ground. The internal pull-up keeps a pin HIGH, and a press pulls it LOW. Eight WS2812B LEDs share one data wire on pin 5, two under each button.
On every pass the sketch reads all four buttons. A pressed button sets its brightness to full right away. Every 16 ms it draws a frame: each button's color is scaled by its brightness and sent to its pair of LEDs. After you let go, the brightness shrinks a little each frame, so the pair fades out.
Parts list
7 parts, plus the jumper wires. They retail for about $22; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit, ESP32-C3 Starter Kit.
- 1 × Full-size breadboard
- 1 × Addressable RGB LEDsIn Claw Machine Kit, Halloween Prop Kit, Christmas Lights Kit.
- 4 × Arcade buttonIn Claw Machine Kit, Fight Stick Kit, Pinball Table Kit.
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 VSS | ESP32-C3-DevKitM-1 pin GND |
| Arcade button (1) pin 1 | ESP32-C3-DevKitM-1 pin GND |
| Arcade button (2) pin 1 | ESP32-C3-DevKitM-1 pin GND |
| Arcade button (3) pin 1 | ESP32-C3-DevKitM-1 pin GND |
| Arcade button (4) pin 1 | ESP32-C3-DevKitM-1 pin GND |
| Arcade button (1) pin 2 | ESP32-C3-DevKitM-1 pin 0 |
| Arcade button (2) pin 2 | ESP32-C3-DevKitM-1 pin 1 |
| Arcade button (3) pin 2 | ESP32-C3-DevKitM-1 pin 10 |
| Arcade button (4) pin 2 | ESP32-C3-DevKitM-1 pin 4 |
| Addressable RGB LEDs pin DIN | ESP32-C3-DevKitM-1 pin 5 |
| Addressable RGB LEDs pin VDD | ESP32-C3-DevKitM-1 pin 5V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button lights: each arcade button lights its own pair of WS2812B LEDs.
//
// Wiring:
// buttons 1 on GPIO 0 2 on GPIO 1 3 on GPIO 10 4 on GPIO 4
// (other leg of each to GND, internal pull-up on, pressed = LOW)
// strip DIN -> GPIO 5 VDD -> 5 V VSS -> GND
//
// Eight LEDs on one wire, two under each button. A press lights that
// button's pair at full color at once; letting go fades it out over a few
// frames, which is the "reactive" lighting many custom controllers ship with.
//
// Why an ESP32-C3 and not a Pro Micro or a Pico: a WS2812 bit is a pulse a
// few hundred nanoseconds long. Real 32U4 boards make it with hand-written
// assembly (FastLED, the Adafruit library) and the RP2040 with its PIO
// blocks. Mokxi runs WS2812 strips from the ESP32-C3 and ESP32-C6 only, so
// this circuit uses the C3. The lighting logic below is the same on any
// board; only the driver underneath changes.
//
// The button reading is the same pull-up pattern as the fight stick
// examples, and the lights never slow it down: a frame is drawn at most
// every FRAME_MS, and the buttons are read on every pass of loop().
#include <Adafruit_NeoPixel.h>
const uint8_t DIN_PIN = 5;
const uint8_t LEDS = 8;
const uint8_t BUTTONS = 4;
const uint8_t BUTTON_PINS[BUTTONS] = {0, 1, 10, 4};
// One color per button, as 0..255 red, green, blue.
const uint8_t COLORS[BUTTONS][3] = {
{255, 40, 40}, // 1: red
{40, 120, 255}, // 2: blue
{40, 255, 80}, // 3: green
{255, 200, 30}, // 4: yellow
};
// How much of the light is left after each frame once a button is let go,
// out of 256. 200 is a quick fade of a few tenths of a second.
const uint16_t FADE = 200;
const unsigned long FRAME_MS = 16;
Adafruit_NeoPixel strip(LEDS, DIN_PIN, NEO_GRB + NEO_KHZ800);
// Each button's brightness, 0..255.
uint8_t level[BUTTONS];
bool wasHeld[BUTTONS];
unsigned long lastFrame = 0;
void setup() {
Serial.begin(115200);
for (uint8_t i = 0; i < BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
strip.begin();
strip.clear();
strip.show();
Serial.println("Button lights: press the arcade buttons.");
}
void loop() {
for (uint8_t i = 0; i < BUTTONS; i++) {
bool held = digitalRead(BUTTON_PINS[i]) == LOW;
if (held) {
level[i] = 255; // on at once, on the same pass the press was seen
}
if (held != wasHeld[i]) {
wasHeld[i] = held;
Serial.print("button ");
Serial.print(i + 1);
Serial.println(held ? " down" : " up");
}
}
unsigned long now = millis();
if (now - lastFrame < FRAME_MS) {
return;
}
lastFrame = now;
for (uint8_t i = 0; i < BUTTONS; i++) {
uint8_t r = (uint8_t)((COLORS[i][0] * (uint16_t)level[i]) >> 8);
uint8_t g = (uint8_t)((COLORS[i][1] * (uint16_t)level[i]) >> 8);
uint8_t b = (uint8_t)((COLORS[i][2] * (uint16_t)level[i]) >> 8);
strip.setPixelColor(i * 2, r, g, b);
strip.setPixelColor(i * 2 + 1, r, g, b);
if (!wasHeld[i]) {
level[i] = (uint8_t)((level[i] * FADE) >> 8);
}
}
strip.show();
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Sets each pin in BUTTON_PINS to INPUT_PULLUP, so an unpressed button reads HIGH with no extra resistor. Then it starts the strip, clears it and shows it so all eight LEDs begin dark.
void setup() {
Serial.begin(115200);
for (uint8_t i = 0; i < BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
strip.begin();
strip.clear();
strip.show();
Serial.println("Button lights: press the arcade buttons.");
}loop sketch.ino
First it checks every button and sets level to 255 for any that is held, so the light comes on in the same pass as the press. Then it returns early unless FRAME_MS has passed. When it is time for a frame, it scales each color by its level, writes both LEDs of the pair, and fades released buttons by FADE out of 256.
void loop() {
for (uint8_t i = 0; i < BUTTONS; i++) {
bool held = digitalRead(BUTTON_PINS[i]) == LOW;
if (held) {
level[i] = 255; // on at once, on the same pass the press was seen
}
if (held != wasHeld[i]) {
wasHeld[i] = held;
Serial.print("button ");
Serial.print(i + 1);
Serial.println(held ? " down" : " up");
}
}
unsigned long now = millis();
if (now - lastFrame < FRAME_MS) {
return;
}
lastFrame = now;
for (uint8_t i = 0; i < BUTTONS; i++) {
uint8_t r = (uint8_t)((COLORS[i][0] * (uint16_t)level[i]) >> 8);
uint8_t g = (uint8_t)((COLORS[i][1] * (uint16_t)level[i]) >> 8);
uint8_t b = (uint8_t)((COLORS[i][2] * (uint16_t)level[i]) >> 8);
strip.setPixelColor(i * 2, r, g, b);
strip.setPixelColor(i * 2 + 1, r, g, b);
if (!wasHeld[i]) {
level[i] = (uint8_t)((level[i] * FADE) >> 8);
// ... 4 more lines in the editorTroubleshooting
The usual mistakes with this circuit, and the fix for each.
- A button never lights its LEDs This project
Check that its pin matchesBUTTON_PINS(0, 1, 10, 4) and that its other leg goes to ground. - The LEDs stay dark or show wrong colors This project
Make sure the strip's DIN is on pin 5, VDD is on 5 V and VSS shares ground with the board. - 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 button reads pressed all the time, or never changes, when wired to a pin. Arcade button
Use the COM and NO terminals of the microswitch. Wire COM to ground, NO to the pin, and use INPUT_PULLUP. NC is the opposite and reads pressed when released. - One press counts as several presses in your code. Arcade button
Microswitch contacts bounce for a few milliseconds. Ignore changes for 10 to 20 ms after the first one, or react only to the edge and wait before checking again. - Upload fails or the board is not found by the computer. ESP32-C3-DevKitM-1
Pick the ESP32-C3 board in your tools and the right port. If it still fails, hold BOOT, tap RESET, then release BOOT to enter download mode and try again. - GPIO pins act strangely or the board resets. ESP32-C3-DevKitM-1
GPIO is 3.3 V only, so never feed it 5 V. GPIO 18 and 19 are USB, and GPIO 2, 8 and 9 are boot strapping pins. Avoid loading them 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.