ESP32 LED Flashing Using Original SDK And Task


Introduction

Using ESP32 and the original SDK (ESP-IDF) to make the LED flash, you can use the FreeRTOS task (Task) to achieve it.

Install VSCode & ESP-IDF VSCode Extension

Make sure you have installed and configured the ESP-IDF development environment. You can also refer to the article the ESP32 Tutorial – ESP-IDF With VSCode.

ESP32 DevKit Development Module

Choose an ESP32 module such as ESP32 DevKitC V4.

Material Preparation & Connecting LED

1. Select a suitable LED, such as a red, green or blue LED.
2. It is recommended to use a current-limiting resistor to limit the current of the LED to protect the GPIO pins of ESP32.
3. Connect the male leg of the LED (usually the longer leg) to the GPIO pin of choice (such as GPIO2).
4. Connect the female leg of the LED (usually the shorter leg) to ground (GND).

Create A New ESP32 Project

We can use VSCode's IDF plug-in to create a new ESP32 project. Please refer to ESP32 Tutorial – How To Create An ESP32 Project With VSCode.

Coding

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

// LED GPIO 
#define BLINK_GPIO 2

// Blink task
void blink_task(void *pvParameter)
{
    while (1)
    {
        // LED ON
        gpio_set_level(BLINK_GPIO, 1);
        printf("LED ON\n");
        vTaskDelay(1000 / portTICK_PERIOD_MS);

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

void app_main(void)
{
    // Set GPIO
    gpio_pad_select_gpio(BLINK_GPIO);
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    // Create blink task
    xTaskCreate(&blink_task, "blink_task", 1024, NULL, 5, NULL);
}

Compile, Flash & Monitor

Find the "ESP32-IDF: Build, Flash and Monitor" ICON in VSCode to execute and burn.

Results

We can look at the flashing of the LED or check the printing results of the terminal, and we will see changes every second, as follows...
LED ON
LED OFF
LED ON
LED OFF
...

Conclusion

Through the above steps and methods, you can use VSCode to develop ESP32 applications and realize the LED blinking function. If there are any errors, please check the configuration of ESP-IDF, VSCode, etc. to make sure that the paths and environment variables are set correctly.