Programming button with ESP32 using ESP-IDF

Introduction

ESP32 is a powerful Wi-Fi and Bluetooth integrated microcontroller, widely used in IoT applications and remote control. ESP-IDF (Espressif IoT Development Framework) is the official development toolkit for ESP32 by Espressif, providing the necessary APIs and libraries for programming and managing the microcontroller.

Requirements

  • ESP32: ESP32 microcontroller.
  • Computer: PC or laptop with Windows, macOS, or Linux operating system.
  • USB Cable: To connect ESP32 to the computer.
  • ESP-IDF Software: The official development toolkit by Espressif.

Step 1: Install ESP-IDF

  • Download ESP-IDF

Visit the official ESP-IDF website: ESP-IDF GitHub Repository.
Download the latest version or stable version according to the instructions on the GitHub page.

This is a new tutorial series where we will cover the ESP32 microprocessor with Espressif-IDE. ESP32 is becoming more and more popular, but there are not many tutorials covering this microprocessor with eclipse based IDE. That is why I decided to cover Espressif-IDE instead of arduino. This way we will understand the process better, and we will have more control over our application.

As we progress with these tutorials, we will cover some of the basic components at first, and then we will move on to more complex protocols.
This particular tutorial will cover how to setup the IDE on windows, and how to blink the LED. For this series, I will be using the ESP32 WROOM Devkit, shown in the image below.
Let’s start with setting up the IDE first.
espblink 6

IDE Setup

– First we have to download the IDE. It can be downloaded from the official espressif website,
– You have to go to Windows Installer and for best installation you can choose online installer as shown in the image above.
When the installation starts, it will do some checks and if it asks for any fixes, you need to click apply. Then just click next
As shown in the image above, I did not enable desktop or startmenu shortcuts for powershell and command prompt. Also, everything was kept default as it was.

Click next and wait for the IDE to be installed.

Creating our first project
After opening IDE, click on “Create a new EspressIf IDF project”. Then Name this project and click next.
For this tutorial, we will be using an example template. As shown below, I am using the blink template. Once you select a template, the project name will change depending on the template, so you can leave it as is or change it back to whatever you want.
Now before we build the project, we have to select the appropriate board that we are using. Click on the settings icon, and select the target and com port
The code generated at this point is very small, and we only need to make very few changes to make the LED blink.
espblink 8

#define BLINK_GPIO GPIO_NUM_2

static uint8_t s_led_state = 0;

The LED on the ESP32 WROOM board is connected to GPIO_2, and so I defined BLINK_GPIO as GPIO_NUM_

Besides that, we also have s_led_state, which will be used later to change the state of the LED.

LED functions

static void configure_led(void)
{
    gpio_reset_pin(BLINK_GPIO);
    /* Set the GPIO as a push/pull output */
    gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
}

static void blink_led ( void )
{
/* Set the GPIO level according to the state (LOW or HIGH)*/
gpio_set_level ( BLINK_GPIO , s_led_state ) ;
}

The first function, configure_led, configures the LED pin (GPIO_2) as an output pin.
The second function, blink_led, will be called in the main function and is used to set the level of the LED pin.

main function

void app_main(void)
{

/* Configure the peripheral according to the LED type */
configure_led ( ) ;

while ( 1 ) {
blink_led ( ) ;
/* Toggle the LED state */
s_led_state = ! s_led_state ;
vTaskDelay ( 1000 / portTICK_PERIOD_MS ) ;
}  In the main function, we will first configure the LED pin.
Then in the while loop, we will blink the LED every 1000 ms.
The value of s_led_state will change after each call to the blink function, and its value will be either 0 or 1, depending on the final value.

Result

Below is a picture of the LED on the board in ON and OFF 

image 2024 05 29T04 49 29 848Z 1
Getting Started with ESP32 and ESP-IDF…

Additional Learning Resources

  • Official Documentation: Espressif Documentation
  • Support Forum: Espressif Community
  • Practical Guide: Articles and tutorial videos on YouTube and Espressif’s website.

Conclusion

By following the steps above, you have successfully set up the ESP-IDF development environment and run your first code on the ESP32. With ESP-IDF, you can develop powerful IoT applications and explore the many features of the ESP32.

 

Leave a Reply