Two weeks ago, i discovered about ESP-IDF, the official framework for ESP32 and ESP8266 Microcontroller development.
Developing for the ESP32 with ESP-IDF offers many features:
- Support for FreeRTOS
- Writing program in the C language
- More low level of control, memory management, and hardware options
Write a Code in ESP-IDF
The workflow between Arduino and ESP-IDF is different, but you dont have to worry because i dont think it will be a big problem.
There are no void setup()
and void loop()
functions in ESP-IDF. Instead, you do everything in the void app_main()
function.
Also, primary language ESP-IDF is C language, not plus-plus in the end.
Some people write ‘Arduino-IDE uses C/C’ which makes others believe C and C++ are the same. In fact, they are not. I wont explain it in here, but you can read about in these article.
Simple Passive Buzzer with ESP-IDF
The first time I tried writing a program with ESP-IDF, i was shocked by the length of code required for a simple program when compared to writing it with the Arduino IDE.
So, let me show you how to use GPIO with ESP-IDF. In this context, we will create a simple bassive buzzer.
I hope you have finished installing ESP-IDF. If not, you should read “Get Started - ESP32 ESP-IDF”.
Create a project directory. I will name it as buzzer
.
$ idf.py create-project buzzer
Change the directory to buzzer
and start editing the file inside main/
with your favorite text editor.
$ cd buzzer
$ vim main/buzzer.c
We will add several libraries and i will explain each of them.
As the name suggests, we will use this for GPIO activity.
#include <driver/gpio.h>
With task.h
, we will use the vTaskDelay()
function which works like delay()
in the Arduino IDE.
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
Lastly, we use this library for logging activity when monitoring.
#include "esp_log.h"
The 2
stands for GPIO2. You can change it according your taste.
#define BUZZER_PIN 2
Now, we will write a code inside void app_main()
function.
This function sets the direction of the GPIO to be used as an output.
gpio_set_direction(BUZZER_PIN, GPIO_MODE_OUTPUT);
Add while(1)
to work like void loop()
. Next, we will make the buzzer works inside the while
loop.
while(1) {
/* writing code in here */
}
We will start by turning the assigned gpio with gpio_set_level
function, which has two modes:
- 1 for active
- 0 for inactive
gpio_set_level(BUZZER_PIN, 1);
Add a one-second delay.
vTaskDelay(1000 / portTICK_PERIOD_MS);
Give a sign that the code is working.
ESP_LOGI("Buzzer", "Active");
Now, its your turn to imagine how buzzer will turn off :). Make sure to close all code with }
.
Here is the full code:
#include <driver/gpio.h>
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#define BUZZER_PIN 2
void app_main() {
gpio_set_direction(BUZZER_PIN, GPIO_MODE_OUTPUT);
while(1)
{
gpio_set_level(BUZZER_PIN, 1);
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI("Buzzer", "Active");
gpio_set_level(BUZZER_PIN, 0);
vTaskDelay(1000 / portTICK_PERIOD_MS);
ESP_LOGI("Buzzer", "Non-Active");
}
}
After that, we will flash the code to the ESP32 device.
$ idf.py flash
And monitor the results
$ idf.py monitor