hack4electronics.com

ESP32 GPIO with ESP-IDF: LED Blinking

Understanding the General Purpose Input/Output (GPIO) functionality is crucial for embedded system development. In this tutorial, we will explore how to use ESP32 GPIO pins and program them as digital output pins using ESP-IDF (Espressif IoT Development Framework). This guide will demonstrate the ESP32 LED blinking project using VS Code and the ESP-IDF extension.

What is GPIO?

GPIO pins on microcontrollers can be configured as digital inputs or outputs, and they can even perform both functions simultaneously. These pins allow the microcontroller to interface with various hardware components.

ESP32 GPIO Pins

The ESP32 development board comes in various models with different numbers of GPIO pins. The ESP32 DEVKIT V1, for instance, has 36 GPIO pins, though not all are exposed on the development board. The ESP32 DEVKIT typically provides access to 24 GPIO pins that can be used as digital inputs or outputs. However, GPIO34 to GPIO39 are input-only pins.

ESP-IDF GPIO APIs for LED Blink

The ESP-IDF provides a driver/gpio.h library with functions to control the ESP32’s digital pins. The key functions for our project are:

  • gpio_set_direction(): Configures a GPIO pin as input or output.
  • gpio_set_level(): Sets the output level of a GPIO pin.

Setting Up GPIO Pins

To set up a GPIO pin as an output:

  1. Include the header file: #include "driver/gpio.h"
  2. Set the pin direction to output: gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT)
  3. Set the output level to HIGH or LOW: gpio_set_level(GPIO_NUM_0, 1)

ESP32 LED Blinking Project Overview

In this project, we will connect a 5mm LED to one of the ESP32’s GPIO pins, configure the pin as a digital output, and toggle its state to create a blinking effect.

Required Components

  • ESP32 development board
  • 5mm LED
  • 220-ohm resistor
  • Breadboard and connecting wires

Schematic and Setup

Connect the anode of the LED to GPIO13 through a 220-ohm resistor and the cathode to the GND pin. The setup involves configuring GPIO13 as a digital output pin.

VS Code Project Setup with ESP-IDF

  1. Open VS Code and use the command palette to create a new ESP-IDF project.
  2. Name the project ‘LED_BLINK’ and select the custom board and ESP32 module.
  3. Open the main.c file in the main directory and write the program code.

ESP32 LED Blinking Script

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

#define LED_PIN 13

void app_main(void)
{
    gpio_pad_select_gpio(LED_PIN);
    gpio_set_direction(LED_PIN, GPIO_MODE_OUTPUT);
    int ON = 0;
    while (true)
    {
        ON = !ON;
        gpio_set_level(LED_PIN, ON);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

Code Explanation

  1. Include Libraries: Necessary libraries for GPIO and FreeRTOS.
  2. Define LED Pin: Specify GPIO13 for the LED.
  3. Configure GPIO: Set GPIO13 as output.
  4. Toggle LED State: In an infinite loop, toggle the LED state and add a 1-second delay using vTaskDelay().

Compiling and Flashing the Code

To compile and flash the code to the ESP32, use the command:

idf.py -p COMX flash monitor

This tutorial demonstrates the basics of using ESP32 GPIO pins with ESP-IDF to create an LED blinking project. By understanding how to configure GPIO pins and control their states, you can develop more complex projects and applications with ESP32.

Leave a Reply

Your email address will not be published. Required fields are marked *