ESP-IDF: ESP32 GPIO – Read Digital Inputs

In this guide, you’ll learn how to set and read the ESP32 board’s GPIO pins as digital inputs with ESP-IDF (Espressif IoT Development Framework).

The ESP32 is a microcontroller that offers several General Purpose Input/Output (GPIO) pins that can be configured as either inputs or outputs. With the GPIOs set as inputs, you can read the current state of devices like pushbuttons, switches, sensors, or other components by reading the pin’s voltage level 0 or 3.3V.

ESP-IDF ESP32 GPIO Read Digital Inputs

Prerequisites

Before following this guide, you need to install the ESP-IDF extension on VS Code IDE (Microsoft Visual Studio Code). Follow the next guide to install it, if you haven’t already:

You will also need an ESP32 development board model of your choice.

ESP32 GPIO Digital Inputs

In ESP-IDF, to set the ESP32 GPIOs as digital inputs, you need to do the following.

First, you need to configure the GPIO pins: assign the pin number, set the mode, enable/disable pull-up or pull-down internal resistors, and enable/disable interrupts.

For example, to read a the current level (state) of the pin, define the gpio_config() function using the following structure:

#define BUTTON_PIN 4

gpio_config_t io_conf = {
    .pin_bit_mask = (1ULL << BUTTON_PIN),   // Select GPIO 4
    .mode = GPIO_MODE_INPUT,                  // Set as input
    .pull_up_en = GPIO_PULLUP_ENABLE,     // Enable internal pull-up
    .pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
    .intr_type = GPIO_INTR_DISABLE        // Disable interrupts
};
gpio_config(&io_conf);

Then, you read the current level of the GPIO (example 1 = Released or 0 = Pressed). Use the gpio_get_level(BUTTON_PIN) function as follows:

gpio_get_level(BUTTON_PIN);

gpio_config_t structure

Public MemberValue
uint64_t pin_bit_maskGPIO pin set with bit mask, for example: (1ULL << 4)
gpio_mode_t modeGPIO_MODE_DISABLE, GPIO_MODE_INPUT, GPIO_MODE_OUTPUT, GPIO_MODE_OUTPUT_OD, GPIO_MODE_INPUT_OUTPUT_OD, GPIO_MODE_INPUT_OUTPUT
gpio_pullup_t pull_up_enGPIO_PULLUP_DISABLE, GPIO_PULLUP_ENABLE
gpio_pulldown_t pull_down_enGPIO_PULLDOWN_DISABLE, GPIO_PULLDOWN_ENABLE
gpio_int_type_t intr_typeGPIO_INTR_DISABLE, GPIO_INTR_POSEDGE, GPIO_INTR_NEGEDGE, GPIO_INTR_ANYEDGE, GPIO_INTR_LOW_LEVEL, GPIO_INTR_HIGH_LEVEL, GPIO_INTR_MAX

Most GPIOs can be used as inputs. Note that GPIO 34, GPIO 35, GPIO 36, and GPIO 39 are input only pins. These pins don’t have internal pull-up or pull-down resistors.

Learn more about the ESP32 GPIOs: ESP32 GPIO Reference Guide.

Creating an ESP-IDF Template App Project for the ESP32

The ESP-IDF extension provides an easy way to create a project from scratch with all the required files and configurations generated automatically.

To create a new ESP-IDF project on VS Code, follow these steps:

  1. Open the ESP-IDF Espressif extension
  2. Expand the “Advanced” menu
  3. Click the “New Project Wizard” option
  4. Choose the “Use ESP-IDF v5.4.1” to select the framework version
ESP-IDF ESP32 Create Open New Project Wizard Menu

A new window opens, you need to fill in these fields:

  • Project Name: type the desired project name;
  • Enter Project Directory: click the folder icon and select the target folder to save all your project files. You can use any directory. Note: do NOT use a Google Drive / One Drive / Dropbox folder, because it will write/create many files during the building process—if it’s on a cloud folder, this process might be extremely slow;
  • ESP-IDF Target: select the target device chip, I’m using an ESP32 with the esp32s3 chip;
  • ESP-IDF Board: for the esp32s3 chip, I also need to select the configuration: ESP32-S chip (via builtin USB-JTAG);
  • Serial Port: while having your ESP32 board connected to your computer, select the correct COM port number that refers to your ESP32;
  • Choose Template: click the blue button to create a new project using a template.
ESP-IDF ESP32 Create Open New Project Wizard Menu Select Directory Board Template

In the menu, select the “template-app” sample project and press the “Create project using template template-app” button.

ESP-IDF ESP32 Create New Project Select Template App Option

Opening the ESP-IDF Project on VS Code

After a few seconds, a small notification will appear at the bottom right corner of VS Code. You can click “Yes” to open the newly created ESP-IDF project template.

ESP-IDF ESP32 Open Project in a New Window VS Code

IMPORTANT: if you didn’t see the notification that allows you to automatically open the ESP-IDF project on VS Code, you can easily do it by following these instructions:

Go to File > Open Folder…

ESP-IDF ESP32 Open Project Folder VS Code File Menu

Browse on your computer for the esp-idf-project folder (your project folder name that you’ve previously defined) and “Select Folder“.

ESP-IDF ESP32 Open Project VS Code Select Folder

That’s it! Your new ESP-IDF project template has been successfully created and opened.

ESP-IDF generates many files, folders, and subfolders for your project. For this guide, I recommend keeping all the default files unchanged; we will only modify the main.c file.

The example codes will be written in the main.c file. To open it, follow these instructions:

  1. Open the project explorer by clicking the first icon on the left sidebar.
  2. Select your project folder name, in my case it’s “ESP-IDF-PROJECT“.
  3. Expand the “main” folder.
  4. Click the “main.c” file.
  5. The default main.c template file loads in the code window.
ESP-IDF ESP32 Open Project in a VS Code Browse to Main C File

Read ESP32 Digital Inputs with ESP-IDF

Here’s the full main.c code that reads a pushbutton connected to the ESP32 GPIO 4 using the gpio_get_level() function:

/*  
  Rui Santos & Sara Santos - Random Nerd Tutorials
  https://RandomNerdTutorials.com/esp-idf-esp32-gpio-inputs/
*/

#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "sdkconfig.h"

#define BUTTON_PIN 4

void app_main(void)
{
    // Configure GPIO
    gpio_config_t io_conf = {
        .pin_bit_mask = (1ULL << BUTTON_PIN),   // Select GPIO 4
        .mode = GPIO_MODE_INPUT,                // Set as input
        .pull_up_en = GPIO_PULLUP_ENABLE,       // Enable internal pull-up
        .pull_down_en = GPIO_PULLDOWN_DISABLE,  // Disable pull-down
        .intr_type = GPIO_INTR_DISABLE          // Disable interrupts
    };
    gpio_config(&io_conf);

    while (1) {
        // Read GPIO level (current state)
        int level = gpio_get_level(BUTTON_PIN);
        if (level == 0) {
            printf("Button Pressed!\n");
        } else {
            printf("Button Released...\n");
        }
        vTaskDelay(200 / portTICK_PERIOD_MS); // Delay 200ms
    }
}

View raw code

How the Code Works

In this section, we’ll take a look at the code to see how it works.

Libraries

We start by including the required libraries:

  • stdio.h – the standard C library will be used for the printf function that prints the debugging information in the serial monitor;
  • FreeRTOS.h – provides the core FreeRTOS types and functions;
  • task.h – allows to use of the non-blocking delay function vTaskDelay;
  • driver/gpio.h – includes the functions required to configure and control GPIOs;
  • sdkconfig.h – includes the project’s configuration file.
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "sdkconfig.h"

Pin Assignment

This line assigns the pushbutton connected to GPIO 4; you can change it to a different number to match your board pinout or to control a different GPIO.

#define BUTTON_PIN 4

app_main(void)

When creating an ESP-IDF project, this function will always be called to run. This function is where you need to write your code for any ESP-IDF applications; it is the equivalent of the setup() in Arduino programming. When the ESP32 boots, the ESP-IDF framework calls app_main.

void app_main(void)
{
    // your code goes here
}

In the app_main(void) function, you start by creating the GPIO config variable with the pushbutton GPIO, set it as INPUT, disable pull-down resistor and interrupts. If you notice, the internal pull-up resistor is enable, so you don’t need an external resistor connected to the pushbutton:

// Configure GPIO
gpio_config_t io_conf = {
    .pin_bit_mask = (1ULL << BUTTON_PIN), // Select GPIO 4
    .mode = GPIO_MODE_INPUT,               // Set as input
    .pull_up_en = GPIO_PULLUP_ENABLE,     // Enable internal pull-up
    .pull_down_en = GPIO_PULLDOWN_DISABLE, // Disable pull-down
    .intr_type = GPIO_INTR_DISABLE        // Disable interrupts
};

Then, call the gpio_config(&io_conf) function to configure the GPIO as an input, because we want to get its current level (returns HIGH or LOW).

gpio_config(&io_conf);

The while(1) runs an infinite loop that ensures that you are constantly checking the current GPIO level.

while (1) {
    // Read GPIO level (current state)
    int level = gpio_get_level(BUTTON_PIN);
    if (level == 0) {
        printf("Button Pressed!\n");
    } else {
        printf("Button Released\n");
    }
    vTaskDelay(200 / portTICK_PERIOD_MS); // Delay 200ms
}

Inside the infinite loop, you read and store the current GPIO level.

int level = gpio_get_level(BUTTON_PIN);

If the returned level is 0, it means the button is being pressed. You can print a message saying “Button Pressed!” in the Serial Monitor for debugging purposes.

if (level == 0) {
    printf("Button Pressed!\n");
} 

If you are not pressing the pushbutton, the message printed in the Serial Monitor will say “Button Released…”.

else {
    printf("Button Released...\n");
}

Finally, add a delay of 200 milliseconds:

vTaskDelay(200 / portTICK_PERIOD_MS);

Build and Flash Code to the ESP32 Board

To build and flash ESP-IDF code to the ESP32, you always need to follow this procedure. You need to select the flash method (UART), the COM port number, the target device (ESP32), build the code, and finally, flash it to the board. All these commands are available in the bottom menu bar of VS Code.

Make sure all your options are correct (they may already be properly configured if you used the project wizard).

VS Code ESP-IDF Check All the Configured Settings UART COM Port Target Board

However, if your setup is not correct, follow the next instructions to ensure everything is set up correctly. First, click the “Star” icon and select the flash method as UART.

VS Code ESP-IDF Select Flash UART Option to Program Flash ESP32

While the ESP32 board is connected to your computer, click the COM Port (plug icon) and select the correct port number that refers to your ESP32.

VS Code ESP-IDF Programming ESP32 Board Select Correct COM Port Number

You also need to select the target device. Click on the chip icon at the bottom bar. In my case, I have an ESP32 with the esp32s3 chip.

VS Code Select the ESP32 S3 or Correct Target Device ESP-IDF

For this board, I also need to select the configuration: ESP32-S chip (via builtin USB-JTAG).

VS Code ESP-IDF Select the ESP32 S3 chip via built in USB JTAG Target Device

Finally, your command bar on the bottom of VS Code should have similar options selected.

VS Code ESP-IDF Check All the Configured Settings UART COM Port Target Board

Now, you can build the project by clicking the wrench icon (Build Project) as shown in the image below.

VS Code Build Project Example Code ESP32 ESP-IDF

The first time you build a project, it usually takes a bit more time. Once completed, it should print a similar message in the Terminal menu and show a “Build Successfully” message.

VS Code Build Example Project ESP32 ESP-IDF Success Message

This is the final step. You can now flash the ESP-IDF project to the ESP32 by clicking the “Flash Device” button (thunder icon).

VS Code Flash Hello World Code Project to ESP32 ESP-IDF

Depending on your board, you might need to hold down the on-board BOOT button on your ESP32 to put into flashing mode. Once the process is completed, it will pop-up a info message saying “Flash Done“.

VS Code Flash Hello World Project to ESP32 ESP-IDF Done Success Message

Schematic Diagram

Here’s a list of the parts you need to build the circuit:

Connect a pushbutton to GPIO 4 as shown in the schematic diagram below. We’re using an ESP32-S3 DevKitC board.

ESP-IDF ESP32 Read Digital Input Circuit Wiring Diagram Pushbutton

Demonstration

If you followed all the steps, the pushbutton code example should be running successfully on your board. Open your Terminal window — click the “Monitor Device” tool that is illustrated with a screen icon.

VS Code Open Terminal Window Monitor Device ESP32 ESP-IDF

Press the physical pushbutton multiple times:

ESP32 ESP-IDF Press Pushbutton Read Digital Input

The terminal should be printing a message saying “Button Pressed!” and “Button Released…” according to the current GPIO state:

ESP-IDF ESP32 Read Digital Input Pushbutton Demonstration Terminal

Wrapping Up

In this tutorial, you learned how to program the ESP32 with the ESP-IDF framework using VS Code to read the current GPIO digital input value. In the next guide, we’ll cover ESP-IDF GPIO Analog Inputs.

Other ESP-IDF guides:

Meanwhile, you can check our ESP32 resources (with Arduino IDE) to learn more about the ESP32 board:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our newsletter!

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.