ESP32-C3 button
A pushbutton from GPIO 9 to GND, with the internal pull-up, lights the LED on GPIO 8 while it is held. The whole build, an ESP32-C3 and 3 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $13.25. On Mokxi they're free.
What you'll learn
- Reading a button with INPUT_PULLUP
- Why a pull-up button reads LOW when pressed
- Using one pin as an input and another as an output
- Wiring a button to ground with no resistor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: ESP32-C3-DevKitM-1
- Parts: 5
How it works
The button connects GPIO 9 to ground. The sketch turns on the pin's internal pull-up, so GPIO 9 sits at 3.3 V until the button pulls it down to ground.
Every 5 milliseconds the loop checks whether the pin is LOW. If so, the button is pressed and GPIO 8 goes high, lighting the red LED. When you let go, the LED goes out.
Parts list
5 parts, plus the jumper wires. They retail for about $13.25; on Mokxi they're free.
- 1 × ESP32-C3-DevKitM-1In Handheld Console Kit, Desk Buddy Kit.
- 1 × Full-size breadboard
- 1 × Resistor, 220 ΩIn Starter Kit, Claw Machine Kit, Halloween Prop Kit.
- 1 × LED, redIn Starter Kit, Halloween Prop Kit, Logic Lab Kit.
- 1 × PushbuttonIn Starter Kit, Christmas Lights Kit, Logic Lab 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 |
|---|---|
| LED, red pin C | ESP32-C3-DevKitM-1 pin GND |
| Pushbutton pin 2a | ESP32-C3-DevKitM-1 pin GND |
| Resistor, 220 Ω pin 1 | ESP32-C3-DevKitM-1 pin 8 |
| Pushbutton pin 1b | ESP32-C3-DevKitM-1 pin 9 |
| LED, red pin A | Resistor, 220 Ω pin 2 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// Button: a pushbutton on GPIO 9 lights the LED on GPIO 8.
//
// INPUT_PULLUP switches on the pad's 45 k pull-up, so the pin sits at 3.3 V
// until the button pulls it to GND. That is why the button reads LOW when it
// is pressed: wire one side to GPIO 9 and the other to GND, no resistor.
const int BUTTON_PIN = 9;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once. pinMode(BUTTON_PIN, INPUT_PULLUP) makes GPIO 9 an input with its internal pull-up on. pinMode(LED_BUILTIN, OUTPUT) makes GPIO 8 an output for the LED.
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
}loop sketch.ino
Sets pressed to true when digitalRead(BUTTON_PIN) is LOW. Then it writes HIGH to the LED if pressed, LOW if not. The delay(5) is a short rest between checks.
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
digitalWrite(LED_BUILTIN, pressed ? HIGH : LOW);
delay(5);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- The LED is on until you press the button This project
Remember the input is inverted: LOW means pressed. The sketch already handles this by checking for LOW. - 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 LED does not light, or it lights once and then dies. LED
LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin. - A blue or white LED stays dark on 3.3 V. LED
Blue and white LEDs drop about 3 V, so 3.3 V leaves almost nothing for the resistor. Use a 5 V supply, or a smaller resistor and a red or green LED. - The input reads random HIGH and LOW when the button is not pressed. Pushbutton
A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW. - The button seems always pressed, or never changes. Pushbutton
A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected. - 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.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.