2.3 GPIO - LED Button
In previous guide we learned how to use IO port output, and here we will use IO port as input. This example reads digital inputs like a button switch and control digital outputs like an LED using the ESP32-S3 with PlatformIO IDE.
Introduction
A push button is an electronic switch. You can turn the switch on by pressing the switch button. When you release your hand, the switch is turned off.


To show you how to use digital inputs and digital outputs, we’ll build a simple project example with a pushbutton and an LED. We’ll read the state of the pushbutton and light up the LED accordingly as illustrated in the following figure.

Please note that when pushing the button swith, there is vibration that is not felt by humans, but it is completely sensed by the microcontroller. Usually we will use software delay of 10ms for debouncing. For example, when a button is pressed, the pin is low at the first reading, and then read the pin level again after a delay of 10ms. If it is still at a low level, it proves that the button has been pressed.
Hardware Circuit
1. Parts Required
Here’s a list of the parts to you need to build the circuit:
ESP32-S3-N16R8
LED
330 - 1k Ohm resistor
Pushbutton
Breadboard
Jumper wires
2. Wiring
Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 12 and the pushbutton to GPIO 5.

Coding and Testing
Here we use the GPIO pin for input, so when we use the pinMode
method, the second parameter cannot pass OUTPUT, and we need to use the digitalRead
method to get the input value.
Unlike output, when setting input pins, we need to configure pull-up or pull-down resistors to determine the high or low level in the circuit. To do this, we use the pinMode
function and the INPUT_PULLUP or INPUT_PULLDOWN constant. Such as you want to set a pin as a pull-up resistor, you need to call the pinMode(pin, INPUT_PULLUP) function, where pin is the pin number.
1. Code design
Copy the following code to your PlatformIO IDE.
#include <Arduino.h>
// set pin numbers
const int buttonPin = 5; // the number of the pushbutton pin
const int ledPin = 12; // the number of the LED pin
// variable for pushbutton status and led logic
bool buttonState = false;
int led_logic = 0;
void setup() {
Serial.begin(115200);
// initialize the pushbutton pin as an input
pinMode(buttonPin, INPUT_PULLDOWN);
// initialize the LED pin as an output
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the pushbutton value
buttonState = digitalRead(buttonPin);
Serial.println(buttonState);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH
if (buttonState == HIGH) {
// turn LED on
digitalWrite(ledPin, HIGH);
} else {
// turn LED off
digitalWrite(ledPin, LOW);
}
}
// For Debouncing
void loop() {
if (digitalRead(button_pin)) {
delay(20);
if (digitalRead(button_pin) && !buttonState) {
led_logic = !led_logic;
digitalWrite(led_pin, led_logic);
buttonState = !buttonState;
}
else if (!digitalRead(button_pin)) {
buttonState = false;
}
}
}
2. How the Code Works
In the following two lines, you create variables to assign pins:
const int buttonPin = 5;
const int ledPin = 12;
The button is connected to GPIO 5 and the LED is connected to GPIO 12. Next, you create a variable to hold the button state. By default, it’s 0 (not pressed).
bool buttonState = false;
In the setup(), you initialize the button as an INPUT, and the LED as an OUTPUT. For that, you use the pinMode() function that accepts the pin you are referring to, and the mode: INPUT or OUTPUT.
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
In the loop() is where you read the button state and set the LED accordingly.
In the next line, you read the button state and save it in the buttonState variable. As we’ve seen previously, you use the digitalRead() function.
buttonState = digitalRead(buttonPin);
The following if statement, checks whether the button state is HIGH. If it is, it turns the LED on using the digitalWrite() function that accepts as argument the ledPin, and the state HIGH.
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
If the button state is not HIGH, you set the LED off. Just set LOW as a second argument to in the digitalWrite() function.
else {
digitalWrite(ledPin, LOW);
}
You can add debouncing as the following code.
3. Uploading the Code
Refer the previous tutorial: 2.1 GPIO - LED Blink
Summary
In this section, you’ve learned how to read digital inputs and control digital outputs with the ESP32 using PlatformIO IDE.
Finally, if you want to learn more about other controllers, take a look at our Youtube channel:
Last updated