How to wire a push button to an ESP32-C3
To wire a push button to an ESP32-C3, connect 1a to GPIO 4 and 2a to GND. The button goes between the pin and GND and uses the board’s internal pull-up, so there is no resistor and no supply wire, and the pin never sees more than 3.3 V. The sketch below uses no library beyond what the board includes. It waits for the button, debounces it for 20 ms and prints a running count of presses.
- 3.3 V logic
- Library: none
- 2 wires
Push button to ESP32-C3 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| Push button pin | ESP32-C3 pin | Wire color | Why |
|---|---|---|---|
| One side (1a) | GPIO 4 | amber | With INPUT_PULLUP the ESP32-C3 (RISC-V)’s own resistor holds the pin high. |
| The other side (2a) | GND | blue | Pressing connects the pin to ground, so it reads LOW. |
Power and logic levels for push button and ESP32-C3
Logic level
The ESP32-C3’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
This pair
No resistor and no supply wire: the ESP32-C3’s internal pull-up, about 45 k on this chip, ties the pin to 3.3 V. Never wire a button between a pin and 5V: the press would put 5 V on a 3.3 V pin.
Pins to leave alone
GPIO 8 carries the onboard LED and GPIO 9 is the BOOT button, and both are also the default I2C pins. GPIO 2, 8 and 9 are strapping pins, and only GPIO 0 to 4 can read an analog voltage.
The working code for the ESP32-C3
Library: none, the sketch reads the pin with digitalRead() and its internal pull-up. This is the sketch the circuit above runs.
const int BUTTON_PIN = 4; // GPIO 4 to the button, the button to GND
const unsigned long DEBOUNCE_MS = 20;
int lastReading = HIGH;
int state = HIGH;
unsigned long changedAt = 0;
unsigned int presses = 0;
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // HIGH when up, LOW when pressed
Serial.println("Press the button");
}
void loop() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastReading) changedAt = millis();
lastReading = reading;
if (millis() - changedAt > DEBOUNCE_MS && reading != state) {
state = reading;
if (state == LOW) {
presses++;
Serial.print("Pressed, count ");
Serial.println(presses);
}
}
}
What you should see
Open the serial monitor at 115200 baud. The sketch prints Press the button once. Each press then adds a line, Pressed, with a count that goes up by one; holding the button down counts once, because the sketch reacts to the press and not to the level.
A button that reads pressed all the time is wired to the wrong pin pair or the pull-up is off; one that counts twice per press is bouncing and needs the debounce time raised.
The serial monitor prints this, recorded from the circuit above with the button pressed once after 300 ms:
Press the button
Pressed, count 1Common mistakes wiring a push button to an ESP32-C3
A floating input
With plain INPUT and no resistor, the pin reads whatever noise is nearby and the sketch sees presses nobody made. Use INPUT_PULLUP, or a 10 k resistor to the supply.
Counting the bounce
Without a debounce, one press can count as three or four. The simulated button bounces for a few milliseconds the way real contacts do, so the fix here is the fix on a desk.
Using GPIO 8 or 9
GPIO 8 drives the onboard LED and GPIO 9 is the BOOT button, and both are strapping pins read at power-on. Wired to the button, either can stop a real board from booting. GPIO 4 and GPIO 5 are free of both.
Build your own push button project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.