Arduino Nano ESP32 - Button - Long Press Short Press

This tutorial provides instructions on how to use Arduino Nano ESP32 to detect the long press and short press, In detail, we will learn:

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×Push Button
1×(Optional) Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Optional) DC Power Jack
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of Button

We have specific tutorials about button. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to ESP32, Arduino Nano ESP32 code... Learn more about them at the following links:

Wiring Diagram

The wiring diagram between Arduino Nano ESP32 and Button

This image is created using Fritzing. Click to enlarge image

This tutorial will use the internal pull-up resistor. The state of the button is HIGH when normal and LOW when pressed.

How To Detect Short Press

  • Measure the time between the pressed and released events.
  • If the duration is shorter than a pre-defined time, the short press event is detected.

Let's see step by step:

  • Define how long the maximum of short press lasts
#define SHORT_PRESS_TIME 500 // 500 milliseconds
  • Detect the button is pressed and save the pressed time
if(prev_state == HIGH && button_state == LOW) pressed_time = millis();
  • Detect the button is released and save the released time
if(prev_state == LOW && button_state == HIGH) released_time = millis();
  • Calculate press duration and
long press_duration = released_time - pressed_time;
  • Determine the short press by comparing the press duration with the defined short press time.
if( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected");

Arduino Nano ESP32 Code for detecting the short press

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button #define SHORT_PRESS_TIME 500 // 500 milliseconds int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin unsigned long pressed_time = 0; unsigned long released_time = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if (prev_button_state == HIGH && button_state == LOW) // button is pressed pressed_time = millis(); else if (prev_button_state == LOW && button_state == HIGH) { // button is released released_time = millis(); long press_duration = released_time - pressed_time; if ( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } // save the the last state prev_button_state = button_state; }

Detailed Instructions

COM6
Send
A short press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The Serial Monitor may print several short presses for a single press. This is a normal behavior of the button. This behavior is called the “chattering phenomenon”. We will learn how to eliminate this issue later in this tutorial.

How To Detect Long Press

There are two use cases for detecting the long press.

  • The long-press event is detected right after the button is released
  • The long-press event is detected while the button is being pressed.

In the first case:

  • Measure the time duration between the pressed event and released event.
  • If the duration is longer than a pre-defined time, the long-press event is detected.

In the second case: during the time the button is being pressed, do the below process repteatedly:

  • Measure the pressing time.
  • If the duration is longer than the pre-defined time, the long-press event is detected.
  • Otherwise, repeat the process until the button is released

Arduino Nano ESP32 Code for detecting long press when released

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button #define LONG_PRESS_TIME 1000 // 1000 milliseconds int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin unsigned long pressed_time = 0; unsigned long released_time = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if(prev_button_state == HIGH && button_state == LOW) // button is pressed pressed_time = millis(); else if(prev_button_state == LOW && button_state == HIGH) { // button is released released_time = millis(); long press_duration = released_time - pressed_time; if( press_duration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state prev_button_state = button_state; }

Detailed Instructions

COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Nano ESP32 Code for detecting long press during pressing

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button #define LONG_PRESS_TIME 1000 // 1000 milliseconds int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin unsigned long pressed_time = 0; bool is_pressing = false; bool is_long_detected = false; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if(prev_button_state == HIGH && button_state == LOW) { // button is pressed pressed_time = millis(); is_pressing = true; is_long_detected = false; } else if(prev_button_state == LOW && button_state == HIGH) { // button is released is_pressing = false; } if(is_pressing == true && is_long_detected == false) { long press_duration = millis() - pressed_time; if( press_duration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); is_long_detected = true; } } // save the the last state prev_button_state = button_state; }

Detailed Instructions

COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

How To Detect Both Long Press and Short Press

Short Press and Long Press after released

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin unsigned long pressed_time = 0; unsigned long released_time = 0; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if (prev_button_state == HIGH && button_state == LOW) // button is pressed pressed_time = millis(); else if (prev_button_state == LOW && button_state == HIGH) { // button is released released_time = millis(); long press_duration = released_time - pressed_time; if ( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); if ( press_duration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } // save the the last state prev_button_state = button_state; }

Detailed Instructions

※ NOTE THAT:

The Serial Monitor may show several short press detection when long press. This is the normal behavior of the button. This behavior is called the “chattering phenomenon”. The issue will be solved in the last part of this tutorial.

Short Press and Long Press During pressing

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #define BUTTON_PIN D2 // Arduino Nano ESP32 pin D2 pin connected to button #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds int prev_button_state = LOW; // The previous state from the input pin int button_state; // The current reading from the input pin unsigned long pressed_time = 0; unsigned long released_time = 0; bool is_pressing = false; bool is_long_detected = false; void setup() { Serial.begin(9600); pinMode(BUTTON_PIN, INPUT_PULLUP); } void loop() { // read the state of the switch/button: button_state = digitalRead(BUTTON_PIN); if (prev_button_state == HIGH && button_state == LOW) { // button is pressed pressed_time = millis(); is_pressing = true; is_long_detected = false; } else if (prev_button_state == LOW && button_state == HIGH) { // button is released is_pressing = false; released_time = millis(); long press_duration = released_time - pressed_time; if ( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } if (is_pressing == true && is_long_detected == false) { long press_duration = millis() - pressed_time; if ( press_duration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); is_long_detected = true; } } // save the the last state prev_button_state = button_state; }

Detailed Instructions

Long Press and Short Press with Debouncing

It is very important to debounce the button in many applications.

Debouncing is a little complicated, especially when using multiple buttons. To make it simple for newbies, we created a library, called ezButton.

We will use this library in below codes

Short Press and Long Press with debouncing after released

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(D2); // create ezButton object for pin Arduino Nano ESP32 pin D2 unsigned long pressed_time = 0; unsigned long released_time = 0; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if (button.isPressed()) pressed_time = millis(); if (button.isReleased()) { released_time = millis(); long press_duration = released_time - pressed_time; if ( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); if ( press_duration > LONG_PRESS_TIME ) Serial.println("A long press is detected"); } }

Detailed Instructions

Short Press and Long Press with debouncing During Pressing

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(D2); // create ezButton object for pin Arduino Nano ESP32 pin D2 unsigned long pressed_time = 0; unsigned long released_time = 0; bool is_pressing = false; bool is_long_detected = false; void setup() { Serial.begin(9600); button.setDebounceTime(50); // set debounce time to 50 milliseconds } void loop() { button.loop(); // MUST call the loop() function first if (button.isPressed()) { pressed_time = millis(); is_pressing = true; is_long_detected = false; } if (button.isReleased()) { is_pressing = false; released_time = millis(); long press_duration = released_time - pressed_time; if ( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected"); } if (is_pressing == true && is_long_detected == false) { long press_duration = millis() - pressed_time; if ( press_duration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); is_long_detected = true; } } }

Detailed Instructions

Video Tutorial

Why Needs Long Press and Short Press

  • To save the number of buttons and digital input pin. A single button can keep two or more functionalities. For example, short press for turning on the light, long press for turn on the fan.
  • Use the long press instead the short press to avoid pressing by accident. For example, some kinds of devices use the button for factory reset. If the button is pressed by accident, it is dangerous.