ESP32 Button Simulator: Wiring, Code and a Live Circuit
This is a pushbutton wired to an ESP32 DevKit, with a debounced press counter running live: hold the button on the drawing and the serial monitor counts each press exactly once.
Two wires and no resistor: GPIO 4 (D4) to one side of the button, GND to the other, and the ESP32’s internal pull-up does the rest.
- 3.3 V logic
- No library
- Input on GPIO 4 (D4)
- Internal pull-up, 20 ms debounce
Wiring the Button to the ESP32
Every connection in the circuit above, with the board’s own pin names.
| Button pin | ESP32 pin | Why |
|---|---|---|
| One side (1a) | GPIO 4 (D4) | With INPUT_PULLUP the ESP32 (Xtensa LX6)’s own resistor holds the pin high. |
| The other side (2a) | GND | Pressing connects the pin to ground, so it reads LOW. |
No resistor and no supply wire: the ESP32’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 VIN (5 V from USB): the press would put 5 V on a 3.3 V pin.
Pins to leave alone on the ESP32
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
On a real bench
The thirty-pin DevKit straddles a breadboard with one row of holes free on each side, which is where the Button’s jumpers go. Many ESP32 boards are wider and cover both rows; then use two breadboards side by side. Hold BOOT while uploading if the IDE cannot connect.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
const int BUTTON_PIN = 4; // GPIO 4 (D4) 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);
}
}
}
pinMode(BUTTON_PIN, INPUT_PULLUP) turns on the pull-up inside the chip, so the pin reads HIGH while the button is up and LOW while it is held. That is backwards from what most people expect the first time, and it saves a resistor.
A real button bounces: for a few milliseconds after it closes, the contacts open and close many times. The sketch notes the time of every change and only accepts a new state once the reading has stayed put for 20 ms, so one press counts once. The simulated button bounces like a real one, so you can see it fail by setting DEBOUNCE_MS to 0.
Serial.begin(115200) opens the serial monitor. Serial is UART0 on the USB bridge. ESP32 sketches usually run it at 115200 baud, which this one does.
Common mistakes with the Button on the ESP32
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.
Putting the button on an input-only pin
GPIO 34, 35, 36 and 39 on the ESP32 have no output driver at all. pinMode() to OUTPUT on one of them does nothing and the line never moves, which looks exactly like a dead part. GPIO 4 (D4) is a safe choice; so are GPIO 13, 14, 16 to 19, 23, 25 to 27 and 32 to 33.
Questions and answers
Can I simulate a button with an ESP32 DevKit?
Yes. The circuit at the top of this page is an ESP32 DevKit with a pushbutton, running in your browser on a simulated ESP32 (Xtensa LX6). Hold the button on the drawing to press it; the contacts bounce like a real switch. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Why does the button read LOW when pressed?
Because the pull-up holds the pin HIGH and the button connects it to ground. Compare with LOW in the code, or wire the button to the supply with a pull-down resistor if you want the opposite.
Should I use attachInterrupt() for a button?
You can, but an interrupt fires on every bounce too, so it still needs a debounce. Polling every pass of loop() is simpler and fast enough for a person.
Is there a library for buttons?
Bounce2.h, OneButton.h and AceButton.h all compile here. The sketch above does the same job in a dozen lines, which is worth understanding before reaching for one.
Build your own Button project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.