In this article, we’ll learn about ESP32 GPIO and how to read digital inputs (e.g. a push-button), and control digital outputs (e.g. an LED) using Arduino Core in Arduino IDE. Here we are using led and button switch to show the digital inputs and outputs function of esp32.
PARTS REQUIRED
- ESP32
- 5mm LED
- 330 ohm resistor
- Push button
- 10k ohm resistor
- Breadboard
- Jumper wires
ESP32
A family of low-cost, low-power system-on-a-chip microcontrollers with built-in Wi-Fi and dual-mode Bluetooth The ESP32 can work as a stand-alone system or as a slave device to a host MCU, lowering the communication stack overhead on the primary application CPU. Through its SPI / SDIO or I2C / UART interfaces, the ESP32 can communicate with other systems to provide Wi-Fi and Bluetooth capability.
ESP32 Digital Input and Output Functions
Digital outputs pin
First of all, you need to define the GPIO pin to operate in output mode in the setup() function, using ESP32 pinMode() Arduino function as shown below.
pinMode(GPIO_pin, OUTPUT);
Then you can modify the digital state of the pin by driving it HIGH or LOW. The LED can be turned on by writing a HIGH or a 1, which are the same thing. You may also use the LOW or 0 keys to turn it off. As demonstrated below, the Arduino digitalWrite() function is used.
digitalWrite(GPIO_pin, HIGH); // Turn ON
digitalWrite(GPIO_pin, HIGH); // Turn ON
Digital inputs pin
The digital input mode of the ESP32 GPIO pins can be utilised to read external digital signals with our microcontroller. Digital input signals can be generated by a small push button, a digital proximity sensor, or a digital optical encoder, among other things.
We need first to define the GPIO pin to operate in input mode in the setup() function, using pinMode() Arduino function as shown below.
ESP32 pinMode(GPIO_pin, INPUT);
To read a digital input, like a button, you use the digitalRead() function, that accepts as an argument, the GPIO (int number) you are referring to digitalRead(GPIO);
All ESP32 GPIOs can be used as inputs, except GPIOs 6 to 11 (connected to the integrated SPI flash).
SCHEMATIC DIAGRAM
Before proceeding, you need to assemble a circuit with an LED and a pushbutton. We’ll connect the LED to GPIO 5 and the pushbutton to GPIO 4.

SOURCE CODE
CODE
// set pin numbers const int buttonPin = 4; // the number of the pushbutton pin const int ledPin = 5; // the number of the LED pin // variable for storing the pushbutton status int buttonState = 0; void setup() { Serial.begin(115200); // initialize the pushbutton pin as an input pinMode(buttonPin, INPUT); // 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); } }
HOW CODE WORKS
const int buttonPin = 4;
const int ledPin = 5;
The button is connected to GPIO 4 and the LED is connected to GPIO 5
int buttonState = 0;
In the setup(), you initialize the button as an INPUT, and the LED as an OUTPUT. For that, we use the pinMode() function that accepts the pin we 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.
buttonState = digitalRead(buttonPin);
The if statement that follows determines whether the button state is HIGH. If it is, it uses the digitalWrite() function to turn on the LED, which accepts the ledPin as an argument as well as the state HIGH.
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else
{ digitalWrite(ledPin, LOW);
}
If the button state is not HIGH, you set the LED off. Just set LOW as a second argument in the digitalWrite() function.
CONCLUSION
Before clicking the upload button, go to Tools > Board, and select the board you’re using.
Go to Tools > Port and select the COM port the ESP32 is connected to. Then, press the upload button and wait for the “Done uploading” message.
You can also check on