ESP32 - Button
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-button
A push button is a momentary switch: it closes while held and opens when released. This guide shows how to wire a button to an ESP32 and read its state in code, using the ESP32's internal pull-up resistor so no external resistor is needed. It covers wiring, how the input state maps to pressed/released, and a simple sketch that detects the press event.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Breadboard-mount Button with Cap
- Breadboard-mount Button Kit
- Panel-mount Push Button
- Push Button Module
- Breadboard
- Jumper Wires
- DC Power Jack
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
A PCB-mount button has four pins, but only two of them (not in the same connected pair) matter electrically: call them Pin A and Pin B. Connect Pin A to a digital input on the ESP32, for example GPIO21, and connect Pin B to GND. With the internal pull-up resistor enabled in code, no external resistor is required. For a panel-mount push button or a button module, wiring is similar; a module instead has GND, VCC and OUT pins, where GND goes to ground, VCC to 3.3V, and OUT to a digital input.
How It Works
When the button is pressed, Pin A connects to Pin B; when not pressed, they are disconnected. With Pin A wired to the ESP32 input and Pin B to GND, and the pin configured as INPUT_PULLUP, the pin reads HIGH when the button is not pressed and LOW when it is pressed. This is the simplest wiring because it avoids needing an external pull-up or pull-down resistor, which would otherwise be required if the button pin were instead tied to VCC.
Code
The sketch sets the ESP32 pin, for example GPIO21, as an input with the internal pull-up enabled using pinMode(21, INPUT_PULLUP). The loop reads the pin with digitalRead() and checks for a state change from LOW to HIGH, printing the result to the Serial Monitor. The code can be modified to detect both the press and the release events, printing each one as it happens, so you can watch button activity live in the Serial Monitor.
esp32-button.ino - the sketch from the tutorial, unchanged.
Things That Go Wrong
Mechanical buttons chatter: a single press can register as several rapid LOW/HIGH transitions. This is harmless for many projects but breaks anything that needs an exact press count. Also remember the button only has two electrically distinct pins (Pin A and Pin B) even though it has four physical pins for mechanical stability; wiring the wrong pair, or skipping the pull-up setting in pinMode(), leaves the input floating and gives unreliable readings.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-button