In this lesson you will learn how to use a momentary push button with your ESP32. Push buttons allow you to send input signals to the microcontroller and control outputs such as LEDs.
What You Will Learn
- How a push button works
- How to wire a push button to the ESP32
- How to use the ESP32 internal pull up resistor
- How to read button presses using digitalRead()
- How to control an LED using a push button
Components Needed
Step 1 - Understanding Push Buttons
A momentary push button is a switch that connects two pins together only while it is being pressed. When released, the connection is broken.
The ESP32 has built in pull up resistors that allow us to connect a button directly between a GPIO pin and GND without using an external resistor. This keeps the wiring simple and reliable.
Step 2 - Wiring the Push Button and LED
Wiring the Push Button
- Place the push button on the breadboard
- Connect one side of the button to GPIO 4 on the ESP32
- Connect the other side of the button to GND
No external resistor is required because the ESP32 will use its internal pull up resistor.

Wiring the LED
- Place the LED on the breadboard
- Connect a 220 Ω resistor to the LED anode long leg
- Connect the other end of the resistor to GPIO 2 on the ESP32
- Connect the LED cathode short leg to GND

Step 3 - Writing the Code to Read the Button
Open the Arduino IDE and enter the following code. This code turns the LED on while the button is pressed and off when released. Once done, upload the code to the ESP32.
int buttonPin = 4; // GPIO connected to button int ledPin = 2; // GPIO connected to LED int buttonState = 0; void setup() { pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull up resistor pinMode(ledPin, OUTPUT); Serial.begin(115200); } void loop() { buttonState = digitalRead(buttonPin); // Read button state Serial.println(buttonState); if (buttonState == LOW) { // Button pressed digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } delay(100); }
How the Code Works
This program reads the push button and turns the LED on while the button is pressed.
int buttonPin = 4;This line stores the GPIO pin number used for the push button.
int ledPin = 2;This line stores the GPIO pin number used for the LED.
int buttonState = 0;This variable stores the current state of the push button.
void setup()This function runs once when the ESP32 starts.
pinMode(buttonPin, INPUT_PULLUP);Enables the ESP32’s internal pull up resistor for the button.
pinMode(ledPin, OUTPUT);Sets the LED pin as an output.
Serial.begin(115200);Starts serial communication for monitoring button presses.
void loop()
This function runs continuously.
-
digitalRead(buttonPin)reads the current state of the push button. LOW means pressed, HIGH means not pressed -
Serial.println(buttonState)prints the button state to the Serial Monitor - The
ifstatement turns the LED on if the button is pressed and off if released -
delay(100)pauses for 100 milliseconds before checking again
Step 4 - Testing the Push Button
- Press the button and observe the LED
- The LED should turn on while the button is pressed and turn off when released
- Watch the Serial Monitor to see the button state update as 0 or 1
Expected Output
ESP32 Lessons
- ESP32 Tutorials Hub
- Lesson 1: ESP32 Setup using Arduino IDE
- Lesson 2: ESP32 Libraries and Using the Serial Monitor
- Lesson 3: LED Blink
- Lesson 4: Passive Buzzer
- Lesson 5: Reading a Photosensitive Resistor (LDR)
- Lesson 6: Using a Push Button with ESP32
Lesson Complete
You have successfully used a push button with your ESP32 to control an LED and read input. In the next lesson you will learn how to use the DHT11 temperature and humidity sensor.