element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products & Partners
    Products & Partners
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Embedded and Microcontrollers
  • Technologies
  • More
Embedded and Microcontrollers
Blog ESP32 GPIO buttons event-based detection.
  • Blog
  • Forum
  • Documents
  • Quiz
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Embedded and Microcontrollers to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: embeddedguy
  • Date Created: 3 Dec 2026 9:47 AM Date Created
  • Views 31 views
  • Likes 2 likes
  • Comments 0 comments
  • esp32
  • embedded system
Related
Recommended

ESP32 GPIO buttons event-based detection.

embeddedguy
embeddedguy
3 Dec 2026

Table of Contents

  • Introduction
  • Blog List
  • Button component
  • References

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

ESP32 UART tutorial

ESP32 ADC tutorial

ESP32 I2C tutorial

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. 

image

References

  • ESP-IDF documentation
  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube