In this section, we will write a simple program for the ESP32 board. We need three LEDs including cable jumpers for this. We will turn on one LED from LED 1 to LED 3. For implementation, I use the ESP-WROVER-KIT board.
Let's begin.
In this section, we will write a simple program for the ESP32 board. We need three LEDs including cable jumpers for this. We will turn on one LED from LED 1 to LED 3. For implementation, I use the ESP-WROVER-KIT board.
Let's begin.
We connect three LEDs on the ESP32 board GPIO. We then do the following wiring:
You can see a wiring scheme in the following diagram:
Next, we create a project.
In general, there is no project template for the ESP32 program with SDK. However, we can create a project with the project structure as shown in this screenshot:
Each project has the following files:
In this demo, we create a project by creating a folder called blinking. Then, we create a Makefile file. We also create a main folder. Inside the main folder, we create the blinking.c and component.mk files.
We will write code for those files in the next section.
Now, we write scripts and codes on the Makefile, component.mk, and blinking.c files:
PROJECT_NAME := blinking
include $(IDF_PATH)/make/project.mk
#
# "main" pseudo-component makefile.
#
# (Uses default behavior of compiling all source files in
directory, adding 'include' to include path.)
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#define LED1 12
#define LED2 14
#define LED3 26
void app_main()
{
xTaskCreate(&blinking_task, "blinking_task",
configMINIMAL_STACK_SIZE, NULL, 5, NULL);
}
void blinking_task(void *pvParameter)
{
// set gpio and its direction
gpio_pad_select_gpio(LED1);
gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(LED2);
gpio_set_direction(LED2, GPIO_MODE_OUTPUT);
gpio_pad_select_gpio(LED3);
gpio_set_direction(LED3, GPIO_MODE_OUTPUT);
int current_led = 1;
while(1) {
turn_on_led(current_led);
vTaskDelay(1000 / portTICK_PERIOD_MS);
current_led++;
if(current_led>3)
current_led = 1;
}
}
void turn_on_led(int led)
{
// turn off all leds
gpio_set_level(LED1, 0);
gpio_set_level(LED2, 0);
gpio_set_level(LED3, 0);
switch(led)
{
case 1:
gpio_set_level(LED1, 1);
break;
case 2:
gpio_set_level(LED2, 1);
break;
case 3:
gpio_set_level(LED3, 1);
break;
}
}
Next, we configure the project before flashing on the ESP32 board.
Now, we should configure our project using menuconfig. This tool is a part of the ESP32 toolchain that you have configured previously on your platform.
Open Terminal and navigate to your project directory. Then, you can type this command:
$ make menuconfig
You should get the dialog shown in the following screenshot:
We configure our ESP32 serial port and then select the Serial flasher config menu. From here, fill your serial port of the ESP32 board. You can see my ESP32 serial port from the ESP-WROVER-KIT board here:
If complete, click the Save button. The menuconfig program will save your project configuration. The program output can be seen in the following screenshot. You should see that this tool generates the sdkconfig file into your current project directory:
Now, your program is ready for compiling and flashing.
After we configured our project, we can flash our program into the ESP32 board. We can type this command on the current project directory from the following terminal:
$ make flash
This command performs compiling and flashing. If the make command is not found, you should install make for your platform.
If you have configured a serial port of the ESP32 board, the program will be flashed. Otherwise, you will get a timeout because the tool cannot find the ESP32 serial port. You can see my program output on the flashing process in the following screenshot:
If this succeeds, you should see lighting on LED 1, LED2, and LED 3.
Next, we develop the ESP32 program using Arduino Sketch.