Getting started with ESP32 Module from Unboxing to Blinking an LED

This post describes quick steps for getting started with ESP32-WROOM Series module right from unboxing to loading and running your first LED Blinking project with C Programming Language and PlatformIO IDE. We describe four steps with source code on this same page.
(Rev. 18-Jun-2024)

Categories | About |     |  

Parveen,

Step 1 of 4 - A first look at ESP32 Module

ESP32 module is provided by Espressif. Their website is https://www.espressif.com/.

The module that we shall be using is ESP32-WROOM Series.

This module is available for less than $4.

The module can be connected to a PC with a micro-usb cable or a type C cable.

Please watch the following youtube video:

Step 2 of 4 - Installing VS Code and PlatformIO for Embedded Systems ESP32, etc.

First of install Visual Studio Code.

Then search for an extension called "PlatformIO IDE". Install it!

Please watch the following youtube video:

Step 3 of 4 - ESP32 Uploading and Running a C/C++ Project with PlatformIO IDE

Create a first project as explained in the video for step 3.

Attach your ESP32 module to the PC. If it is not detected, then you should install drivers from these USB-UART suppliers. In most cases the supplier is Silicon Labs. If that doesn't work, then try FTDI drivers.

Silicon Labs Driver = https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers

FTDI Driver = https://ftdichip.com/drivers/vcp-drivers/

The platformio.ini file need to be modified to look like as shown below. The baudrate must be 115200 because ESP32 uses this baud rate.


// platformio.ini file 

; PlatformIO Project Configuration File
;
;   Build options: build flags, source filter
;   Upload options: custom upload port, speed and extra flags
;   Library options: dependencies, extra library storages
;   Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = espidf
monitor_speed = 115200
monitor_filters = direct, esp32_exception_decoder

The CMakeLists.txt file that should look similar.


// CMakeLists.txt file 

# This file was automatically generated for projects
# without default 'CMakeLists.txt' file.

FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/src/*.*)

idf_component_register(SRCS ${app_sources})

Copy the hello_world_main github Espresso ESP32 project from https://github.com/espressif/esp-idf/tree/master/examples/get-started/hello_world/main

Ensure that you are able to run the project as explained in the linked video.

Step 4 of 4 - Blinking an LED with ESP32

Create a first project as explained in the video for step 3.

Replace the code with the following code for main.c file.


// THIS CODE IS FOR main.c file 
// SEE NEXT FOR main.cpp file 

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

static const char *TAG = "tag:Blink";

static const int BLINK_GPIO = GPIO_NUM_22;

void app_main()
{
    ESP_LOGI(TAG, "Starting...");

    gpio_reset_pin(BLINK_GPIO);

    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    bool isOn = false;

    while (1)
    {

        ESP_LOGI(TAG, "Turning the LED %s!", isOn ? "OFF" : "ON");

        gpio_set_level(BLINK_GPIO, isOn = !isOn);

        vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
}

ALTERNATIVE for CPlusPlus: Replace the code with the following code for main.cpp file.


// THIS CODE IS FOR main.cpp file 
// either of main.c or main.cpp will work 

// main.cpp file  

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

static const char* TAG = "tag:Blink";

static const gpio_num_t BLINK_GPIO = GPIO_NUM_22;

extern "C"
{
  void app_main()
  {
    ESP_LOGI(TAG, "Starting...");

    gpio_reset_pin(BLINK_GPIO);

    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);

    bool isOn = false;

    while (1)
    {

      ESP_LOGI(TAG, "Turning the LED %s!", isOn ? "OFF" : "ON");

      gpio_set_level(BLINK_GPIO, isOn = !isOn);

      vTaskDelay(2000 / portTICK_PERIOD_MS);
    }
  }
}

Connect an LED between the GPIO22 and GND pin as shown in the linked video.


This Blog Post/Article "Getting started with ESP32 Module from Unboxing to Blinking an LED" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.