ESP32 GPIO with ESP-IDF with LED Blinking example

The most fundamental process in any embedded development is the usage of GPIO (general-purpose input/output) pins. In this tutorial, we will learn about ESP32 GPIO pins and how to program them as digital output pins. For demonstration purposes, we will build an ESP32 LED blinking project in VS Code with ESP-IDF extension. The project will enable the readers to understand how to configure GPIO pins of ESP32 as output with a very basic example. So let us begin!

Before we move forward, make sure you have the latest version of VS Code installed in you system with the ESP-IDF extension configured.

Introduction to GPIOs

General Purpose input/output is referred to as GPIO in shorthand. These are digital signal pins found on the microcontrollers that can be used as input, output, or even simultaneous usage of both. The users have control over them during runtime.


ESP32 development board comes in a variety of models with varied numbers of GPIOs. There are 36 GPIO pins on the one we’ll be looking at. We can utilize the pins as they are configured by default or even modify them in the program code. The GPIO pins for the ESP32 DEVKIT V1 are depicted in the diagram below in their default configuration.

ESP32 DevKit V1 Pinout
ESP32 DEVKIT V1 Pinout

The 48 GPIO pins on the ESP32 chip utilized in this board are not all accessible via development boards. This indicates that not all GPIO pins are exposed in the pinout. There are 36 GPIO pins supported by the ESP32 Devkit, although not all of them may be utilized as digital output pins. Only 24 of the total 30 pins can be utilized for both digital input and output out of which GPIO34-GPIO39 can only be used as input pins.

The ESP32 DEVKIT does not expose GPIO6 to GPIO11. This is because these pins are internally connected to the integrated SPI flash on the ESP-WROOM-32 chip.

For more in-depth information regarding ESP32 GPIO pins, refer to the article below:


Before we move ahead, let us discuss some important functions that are required for GPIO programming with regard to this project.

ESP-IDF provides a driver/gpio.h library that provides helpful functions to control the digital input and output pins of ESP32. For this project we will be required to configure a GPIO port as an output port and then set its level. Lets see how it will be accomplished.

The first step is to include the header file:

#include "driver/gpio.h"

Set Direction

Next, to configure a GPIO port as an input or output we will use the gpio_set_direction() function. It takes in two arguments. The first argument is the GPIO pin that we want to set. The second argument is the mode (input or output) we want to set the pin in. It will be either ‘GPIO_MODE_OUTPUT’ if you want to configure the port as an output or ‘GPIO_MODE_INPUT‘ if you want to configure the port as an input. In our case, we want to set the GPIO pin as an output pin because we want to control the LED connected to it.

In the following line of code we are configuring GPIO0 as an output pin.

gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT);

Set Output Level

The next step is to set the output level of the GPIO port. For that we use the gpio_set_level() function. It also takes in two arguments. The first argument is the GPIO pin that we want to set the state of. The second argument is the state (0 or 1) we want to set. For setting the output of the pin HIGH, use ‘1’ as the second argument. Likewise, for setting the output of the pin LOW, use ‘0’ as the second argument.

In the following line of code we are setting GPIO0 to a HIGH state.

gpio_set_level(GPIO_NUM_0, 1);

ESP32 LED Blinking Project Overview

The aim of our project is to connect a 5mm LED with one of the 30 pins available for ESP32, configure that GPIO pin as a digital output pin and then toggle its state after a delay of a few seconds to show a blinking effect.

The diagram below shows how the state of the GPIO pin will change:

ESP32 LED Blinking with ESP-IDF Project Overview

The LED connected with the digital output pin of ESP32 is initially OFF as its state is LOW. After 1 second, the state of this GPIO pin goes to HIGH and the LED turns ON. The LED stays ON for 1 second. After the time is up, the state of the LED again goes LOW and it turns OFF for 1 second. This way the process repeats itself and the LED blinks, turning ON and OFF every second.

ESP32 LED Blinking Schematic diagram & Hardware Setup

We will require the following components for this project.

Required Components:

  1. ESP32 development board
  2. One 5mm LED
  3. One 220 ohm current limiting resistor
  4. Connecting Wires
  5. Breadboard

The diagram below shows how to setup the circuit for ESP32 LED blinking project.

ESP32 LED Blinking project schematic diagram

We have used GPIO13 to connect with the anode pin of the LED through a 220 ohm current limiting resistor. The cathode pin of the LED is connected with the GND pin of ESP32. Feel free to use any other appropriate ESP32 digital output pin to connect with the LED as well.

This is how our circuit looks like after connecting all the components.

ESP32 LED Blinking hardware setup

VS Code LED Blink Project with ESP-IDF

Open your VS Code and head over to View > Command Palette. Type ESP-IDF: New Project in the search bar and press enter.

Specify the project name and directory. We have named our project ‘LED_BLINK.’ For the ESP-IDF board we have chosen the custom board option. For ESP-IDF target, we have chosen ESP32 module. Click ‘Choose Template’ button to proceed forward.

ESP32 LED Blinking with ESP-IDF 1

In the Extension, select ESP-IDF option:

ESP-IDF in VS Code New Project 2

We will click the ‘sample_project’ under the get-started tab. Now click ‘Create project using template sample_project.’

ESP-IDF in VS Code New Project 3

You will get a notification that the project has been created. To open the project in a new window, click ‘Yes.’

ESP32 LED Blinking with ESP-IDF 2

This opens our LED_BLINK project that we created inside the EXPLORER tab. As you can see there are several folders inside our project folder (LED_BLINK). This is the same for every project which you will create through ESP-IDF Explorer. The main folder contains the source code meaning the main.c file will be found here. Inside the main.c we will write our program code.

Go to main > main.c and open it. We will define the functions and the program code here.

ESP32 LED Blinking with ESP-IDF 3

Now we are all set to start programming our ESP32 board for LED Blinking project.

ESP32 LED Blinking Script

This is the script that we will use to blink the LED connected at GPIO13 of ESP32.

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

#define LED_PIN 13

void app_main(void)
  gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
  int ON = 0;
   ON = !ON;
   gpio_set_level(LED_PIN, ON);
   vTaskDelay(1000/ portTICK_PERIOD_MS);
ESP32 LED Blinking with ESP-IDF script

How the Code Works?

Firstly, we will start by including the necessary libraries for this project. This includes the driver/gpio.h library as we have to work with GPIO pins and FreeRTOS libraries as we want to add a delay in order to blink the LED.

#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

We have connected the LED at GPIO13. Therefore, we will define a variable called ‘LED_PIN’ that will hold the GPIO pin 13. This will be used later on in the code to control the LED.

#define LED_PIN 13
Configuring GPIO for LED Blink

The next step is to configure the LED_PIN as a GPIO. To do that, we will use the gpio_pad_select_gpio() function. Specify the LED_PIN as a parameter inside this function.


Then, we will set the direction of the pin as an output using the gpio_set_direction() function. This function takes in two arguments. The first argument is the GPIO pin and the second argument is the mode (input or output) we want to set the pin in. In our case, we want to set the LED_PIN as an output pin.

gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);

To blink the LED, we will use the following lines of code inside the loop function.

int ON = 0;
   ON = !ON;
   gpio_set_level(LED_PIN, ON);
   vTaskDelay(1000/ portTick_Period_MS);

Here we are first creating an integer variable called ‘ON’ which holds the value 0. This is a flag that will monitor whether the LED is ON or OFF. Inside the while loop, we toggle the ON variable. Using gpio_set_level(LED_PIN, ON), we will set the LED_PIN to the value saved in ON. Then we will add a delay before it loops again.

To add a delay of 1 second between changing the state of the LED, we will use vTaskDelay() function. This delay will cause a blinking effect of the LED.

Compiling the Sketch

To flash your chip, type the following command in the serial terminal. Remember to replace the COM port with the one through which your board is connected. -p COMX flash monitor
ESP32 LED Blinking with ESP-IDF compile script

After the code flashes successfully, the LED will start blinking with a delay of one second.

ESP32 LED Blinking project demo

Video demo:

Where to go next?

Leave a Comment

%d bloggers like this: