ESP32 - Button

by DIYables in Circuits > Microcontrollers

128 Views, 0 Favorites, 0 Comments

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

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.

#define BUTTON_PIN 21 // GPIO21 pin connected to button

// Variables will change:
int lastState = HIGH; // the previous state from the input pin
int currentState; // the current reading from the input pin

void setup() {
Serial.begin(9600);
// initialize the pushbutton pin as an pull-up input
pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
// read the state of the switch/button:
currentState = digitalRead(BUTTON_PIN);

if(lastState == LOW && currentState == HIGH)
Serial.println("The state changed from LOW to HIGH");

// save the last state
lastState = currentState;
}

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