ESP-IDF: ESP32 Blink LED Example (VS Code)

Learn how to blink an LED on an ESP32 board with ESP-IDF (Espressif IoT Development Framework). The ESP-IDF was developed by Espressif, and it is the “official” way to program ESP32 boards.

If you come from the Arduino programming environment, writing your first ESP-IDF code might seem like a daunting task. Fortunately, with this guide, you’ll have your first project running in no time.

ESP-IDF ESP32 Blink LED Example VS Code

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.

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 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.

Programming ESP32 with ESP-IDF using VS Code – Blink LED Example

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

The blink LED example code 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

Here’s the full main.c code that blinks the ESP32 on-board LED:

/*  
  Rui Santos & Sara Santos - Random Nerd Tutorials
  https://RandomNerdTutorials.com/esp-idf-esp32-blink-led/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/

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

// Define the GPIO pin for the LED (GPIO 2 is common for onboard LEDs)
#define BLINK_GPIO 2

void app_main(void)
{
    // Configure the GPIO pin
    gpio_reset_pin(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    // Blink loop
    while (1) {
        // Turn LED ON
        printf("LED ON\n");
        gpio_set_level(BLINK_GPIO, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second

        // Turn LED OFF
        printf("LED OFF\n");
        gpio_set_level(BLINK_GPIO, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second
    }
}

View raw code

Copy the code to the main.c file and save it, it should look as shown in the screenshot below.

ESP-IDF ESP32 Project VS Code Main C File Blink 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 on-board LED to GPIO 2; you can change it to a different number to match your board pinout or to control a different GPIO.

#define BLINK_GPIO 2

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 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 clearing the previous configurations for the BLINK_GPIO:

gpio_reset_pin(BLINK_GPIO);

You configure the BLINK_GPIO as an output, because we want to control its state with HIGH/LOW.

gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

The while(1) runs an infinite loop that ensures the LED will be blinking indefinitely. This is similar to the loop() function in Arduino programming.

while (1) {
   // Turn LED ON
   printf("LED ON\n");
   gpio_set_level(BLINK_GPIO, 1);
   vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second

   // Turn LED OFF
   printf("LED OFF\n");
   gpio_set_level(BLINK_GPIO, 0);
   vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second
};

Inside the infinite loop, you print a message saying “LED ON” in the Serial Monitor/Terminal tab.

printf("LED ON\n");

Set the BLINK_GPIO level to 1 (it’s like setting to HIGH to turn the LED).

gpio_set_level(BLINK_GPIO, 1);

Then, add a delay of 1000 milliseconds (1 second), so the LED is ON for 1 second before you turn it off to create the blink effect.

vTaskDelay(1000 / portTICK_PERIOD_MS);

A similar process is repeated to turn the LED off.

// Turn LED OFF
printf("LED OFF\n");
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay 1 second

Build and Flash the ESP-IDF Blink Example 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 your 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

Demonstration

If you followed all the steps, the Blinking 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

The terminal should be printing a message saying “LED ON” and “LED OFF“. This process is repeated indefinitely.

ESP-IDF ESP32 Project VS Code Open Terminal Window Monitor Test Blink Code Example

Your board is now running a basic LED blinking example. It will turn on GPIO 2 (blue LED labelled as IO2 in this particular board) for 1 second.

ESP32-S3 Blink On Board LED ESP-IDF ON

Then, it will turn it off for 1 second.

ESP32-S3 Blink On Board LED ESP-IDF OFF

You can modify this line in the main.c file to change how long the LED is on and off:

vTaskDelay(1000 / portTICK_PERIOD_MS);

Wrapping Up

In this tutorial, you learned how to program the ESP32 with the ESP-IDF framework using VS Code to write a simple blink LED code. ESP-IDF is the “official way”, developed by Espressif, to program ESP32 boards.

While it gives you more control over your hardware, it’s also much more complex to use and requires more knowledge about the hardware and configurations. So, if you’re still just getting started with the ESP32, we recommend starting with Arduino IDE or MicroPython programming instead.

This was a basic guide to get you started. We hope you found it useful. Let us know if you’d like to see more tutorials for ESP-IDF.

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!

3 thoughts on “ESP-IDF: ESP32 Blink LED Example (VS Code)”

  1. Another very good tutorial published by Rui. Really looking forward to some more complex
    examples using the ESP-IDF framework.

    Well done!

    Brian

    Reply
  2. I would greatly appreciate more examples about using the ESP-IDF environment. In particular comments on how its component libraries compare with arduino, and some examples of its source code level debugging facilities.

    Reply

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.