This assignment has two tasks:
- Create a
led-togglecomponent - Refactor the
hello_ledexample using the created component
led-toggle component#
The first task is to create a led-toggle component.
Create a new component#
- Open your project
hello_ledin VSCode - Create a new component:
> ESP-IDF: Create New ESP-IDF Component - Type
led_togglein the text field appearing on top (see Fig.1)

Fig.1 - Create new component
The project will now contain the folder components and all the required files:
.
└── hello_led/
├── components/
│ └── led_toggle/
│ ├── include/
│ │ └── led_toggle.h
│ ├── CMakeList.txt
│ └── led_toggle.c
├── main
└── build
Create the toggle function#
Inside the led_toggle.h, add:
#include "driver/gpio.h"
typedef struct {
int gpio_nr;
bool status;
}led_gpio_t;
esp_err_t config_led(led_gpio_t * led_gpio);
esp_err_t drive_led(led_gpio_t * led_gpio);
esp_err_t toggle_led(led_gpio_t * led_gpio);
esp_err is an enum (hence an int) used to return error codes. You can check its values in the documentation.
This enum is used also with logging and macros like ESP_ERR_CHECK, which you will find almost all esp-idf examples.In the led_toggle.c, we have to implement the toggling logic:
esp_err_t config_led(led_gpio_t * led_gpio){
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL<<led_gpio->gpio_nr);
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
return gpio_config(&io_conf);
}
esp_err_t drive_led(led_gpio_t * led_gpio){
return gpio_set_level(led_gpio->gpio_nr, led_gpio->status); // turns led on
}
esp_err_t toggle_led(led_gpio_t * led_gpio){
//TBD
return 0;
}
As we’ve seen in the previous lecture, you first need to configure the peripheral. This is done with the function config_led, where you can see the configuration structure we discussed in the lecture.
To test that you chose the correct GPIO and that the LED is working properly, you can also write a drive_led which simply drives the GPIO up or down.
Now you have to:
- Include the appropriate header file in your main file.
- Call the
drive_ledfunction and check the led is turning on and off
Refactor the hello_led code#
Now you are ready to:
- Implement the
toggle_ledfunction - Refactor the
hello_ledcode to use the newly created component.
Conclusion#
You can now create your own components, which makes your code easier to maintain and to share. In the next assignment, you will face a typical development problem and use the skills you just learned.
Next step#
Next assignment → Assignment 3.2
