Table of Contents
Introduction
In this blog post I am going to write about GPIO buttons and how to use them with ESP32 IDF framework. The buttons are useful elements in any product. With them we can select different functions. But sometimes when we need more than one function and we are limited by space and cost, we have to either add more buttons or limit the available functionality.
With event-based button component, we can use single button for up to four different functions based on how long and how many times the button is pressed.
This functionality can help us to reduce the need for extra buttons and use one button for different functionality in our product. So eventually we can reduce cost and size of our product. The only thing to take care of here is that we make use of this functionality so that we do not accidently do something that we do not intend to do. But we careful design we can overcome this kind of problems.
The following is the list of events that we can detect using a single button on ESP32 board.
- Single short press/click
- Double click
- Repeated press
- Long press
Blog List
Button component
ESP-IDF provides button component for detecting events based on button press. The button component can be used to detect above mentioned events and trigger callback functions based on those events.
First install the button component using following command.
idf.py add-dependency "espressif/button=*"
For using button components, you can define the pin used for button and the active level of specific pin. For example, in the following code I am defining GPIO 9 for button and the event is detected when button is pressed and active level is logic low. In ESP32 boards the GPIO connected to the button are pulled up using 10k resistor and when the switch is pressed, they are connected to GND. This will trigger the corresponding events. For example, two consecutive press will trigger double click event and one long press will trigger long press event.
// Define button GPIO and active level #define BUTTON_IO_NUM 9 #define BUTTON_ACTIVE_LEVEL 0
For each event there is a callback that is triggered.
// Callback functions for button events
static void button_single_click_event_cb(void *arg, void *data)
{
ESP_LOGI(APP_TAG, "Button single click!");
}
static void button_double_click_event_cb(void *arg, void *data)
{
ESP_LOGI(APP_TAG, "Button double click!");
}
static void button_long_press_event_cb(void *arg, void *data)
{
ESP_LOGI(APP_TAG, "Button long press!");
}
static void button_repeat_event_cb(void *arg, void *data)
{
ESP_LOGI(APP_TAG, "Button press repeat!");
}
Then inside a main() loop define button related variables and register the related callback functions.
// Create button configurations
const button_config_t btn_cfg = {
.long_press_time = 5000, // Long press time in milliseconds
.short_press_time = 200, // Short press time in milliseconds
};
const button_gpio_config_t btn_gpio_cfg = {
.gpio_num = BUTTON_IO_NUM,
.active_level = BUTTON_ACTIVE_LEVEL,
.disable_pull = false,
};
// Button handle
button_handle_t btn;
// Create a new button device
esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &btn_gpio_cfg, &btn);
// Register callback for button press
ret = iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, NULL, button_single_click_event_cb, NULL);
ESP_ERROR_CHECK(ret);
ret = iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, NULL, button_double_click_event_cb, NULL);
ESP_ERROR_CHECK(ret);
ret = iot_button_register_cb(btn, BUTTON_LONG_PRESS_UP, NULL, button_long_press_event_cb, NULL);
ESP_ERROR_CHECK(ret);
ret = iot_button_register_cb(btn, BUTTON_PRESS_REPEAT, NULL, button_repeat_event_cb, NULL);
ESP_ERROR_CHECK(ret);
After that flash the code to ESP32 and open the serial terminal. If you would press a button once, it will be detected as a single press. If you press twice in a short interval, it will detect it as double click and otherwise as repeated press. If you keep the button pressed for long time > 5000ms it will be decoded as long press. You can define these intervals for the long press and short press in above code. For example, in the above code short press is approx. 200ms and long press is approx. 5000ms long.
