Arduino Nano - Button - Long Press Short Press

This tutorial instructs you how to use Arduino Nano to delect the button's long press and short press. In detail, we will go through the following examples:

In the last section, we will learn how to use debounce to detect button presses in practical applications. To learn more about why debouncing is necessary for buttons, please refer to this article. Without it, we may mistakenly detect a short press of the button.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Button

If you are not familiar with buttons (including pinouts, functionality and programming), the following tutorials can help you:

Wiring Diagram

The wiring diagram between Arduino Nano and Button

This image is created using Fritzing. Click to enlarge image

In this tutorial, we will be utilizing the internal pull-up resistor. As a result, the state of the button is HIGH in its normal condition and LOW when it is pressed.

How To Detect Short Press

We calculate the interval between the pressed and released events. If the duration is less than a pre-defined time, then the short press event is identified.

Specify the duration of a short press.

const int SHORT_PRESS_TIME = 500; // 500 milliseconds
  • Identify when the button has been pressed and record the time it was pushed.
if(prev_button_state == HIGH && button_state == LOW) time_pressed = millis();
  • Detect when the button is released and record the time of release.
if(prev_button_state == LOW && button_state == HIGH) time_released = millis();
  • Pressure
  • Calculate the length of time and the amount of pressure applied.
long press_duration = time_released - time_pressed;
  • Compare the press duration to the specified short press time to identify a short press.
if( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected");

Arduino Nano Code for detecting the short press

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ const int BUTTON_PIN = 2; // The number of the pushbutton pin const int 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 time_pressed = 0; unsigned long time_released = 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 time_pressed = millis(); else if(prev_button_state == LOW && button_state == HIGH) { // button is released time_released = millis(); long press_duration = time_released - time_pressed; 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

  • Upload the code to Arduino Nano using the Arduino IDE.
  • Press the button briefly multiple times.
  • Check the output on the Serial Monitor.
COM6
Send
A short press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The Serial Monitor may display multiple short press detections for a single press. This is the expected behavior of the button, known as the “chattering phenomenon”. We will address this issue in the concluding part of this tutorial.

How To Detect Long Press

There are two scenarios for detecting a long press:

  • The long-press event is identified once the button has been released
  • The long-press event is identified while the button is being held down, even before it has been released.
  1. For the initial application, we will calculate the time period between the pressed and released events. 2. If the duration is more than a predetermined amount of time, then the long-press event is identified.

In the second use case, when the button is pressed, the pressing time is measured continuously and the long-press event is checked until the button is released. While the button is being pressed, if the duration exceeds a predefined time, the long-press event is detected.

Arduino Nano Code for detecting long press when released

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ const int BUTTON_PIN = 2; // The number of the pushbutton pin const int 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 time_pressed = 0; unsigned long time_released = 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 time_pressed = millis(); else if(prev_button_state == LOW && button_state == HIGH) { // button is released time_released = millis(); long press_duration = time_released - time_pressed; 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

  • Upload the code to Arduino Nano using the Arduino IDE.
  • Wait one second and then press and release the button.
  • Check out the result on the Serial Monitor.
COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The event of long-pressing is only identified when the button is let go.

Arduino Nano Code for detecting long press during pressing

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ const int BUTTON_PIN = 2; // The number of the pushbutton pin const int 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 time_pressed = 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 time_pressed = 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() - time_pressed; 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

  • Upload the code to Arduino Nano using the Arduino IDE.
  • Press and hold the button for a few seconds.
  • Check the output on the Serial Monitor.
COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

The event of long-pressing is only registered when the button has not been released.

How To Detect Both Long Press and Short Press

Short Press and Long Press after released

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ const int BUTTON_PIN = 2; // The number of the pushbutton pin const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int 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 time_pressed = 0; unsigned long time_released = 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 time_pressed = millis(); else if(prev_button_state == LOW && button_state == HIGH) { // button is released time_released = millis(); long press_duration = time_released - time_pressed; 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

  • Upload the code to the Arduino Nano using the Arduino IDE.
  • Press the button for a short or a long time.
  • Check the output on the Serial Monitor.

※ NOTE THAT:

The Serial Monitor may display multiple short press detections when a long press is made. This is the expected behavior of the button and is referred to as the "chattering phenomenon". A solution to this issue will be provided in the concluding section of this tutorial.

Short Press and Long Press During pressing

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ const int BUTTON_PIN = 2; // The number of the pushbutton pin const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int 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 time_pressed = 0; unsigned long time_released = 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 time_pressed = millis(); is_pressing = true; is_long_detected = false; } else if(prev_button_state == LOW && button_state == HIGH) { // button is released is_pressing = false; time_released = millis(); long press_duration = time_released - time_pressed; 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() - time_pressed; 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

  • Upload the code to the Arduino Nano using the Arduino IDE.
  • Press the button for a long time and then a short time.
  • Check out the result on the Serial Monitor.

※ NOTE THAT:

The Serial Monitor may display multiple short presses being detected when a long press is made. This is the expected behavior of the button and is referred to as the “chattering phenomenon”. This issue will be addressed in the concluding section of this tutorial.

Long Press and Short Press with Debouncing

It is essential to implement debouncing on the button in numerous applications.

Debouncing can be complicated, particularly when multiple buttons are involved. To make it simpler for novices, we have developed a library, known as ezButton.

We will use this library in the codes below.

Short Press and Long Press with debouncing after released

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ #include <ezButton.h> const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds ezButton button(2); // create ezButton object for pin 2 unsigned long time_pressed = 0; unsigned long time_released = 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()) time_pressed = millis(); if(button.isReleased()) { time_released = millis(); long press_duration = time_released - time_pressed; 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

  • Install the ezButton library.
  • Refer to How To for instructions.
  • Upload the code to Arduino Nano using the Arduino IDE.
  • Press and hold the button for a short or long period of time.
  • Check out the results on the Serial Monitor.

Short Press and Long Press with debouncing During Pressing

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-button-long-press-short-press */ #include <ezButton.h> const int SHORT_PRESS_TIME = 1000; // 1000 milliseconds const int LONG_PRESS_TIME = 1000; // 1000 milliseconds ezButton button(2); // create ezButton object for pin 2 unsigned long time_pressed = 0; unsigned long time_released = 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()){ time_pressed = millis(); is_pressing = true; is_long_detected = false; } if(button.isReleased()) { is_pressing = false; time_released = millis(); long press_duration = time_released - time_pressed; 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() - time_pressed; if( press_duration > LONG_PRESS_TIME ) { Serial.println("A long press is detected"); is_long_detected = true; } } }

Detailed Instructions

  • Include the ezButton library in your project. Refer to How To for instructions.
  • Compile and upload the code to your Arduino Nano board using the Arduino IDE.
  • Press and hold the button for a short or long period of time.
  • Check out the output on the Serial Monitor.

Video Tutorial

Why Needs Long Press and Short Press

  • To minimize the number of buttons, a single button can have multiple functions. For instance, a short press can be used to switch the operation mode, while a long press can be used to turn off the device.
  • Long pressing is used to prevent accidental activation. For example, some devices use a button for factory reset. If it is pressed unintentionally, it could be hazardous. To prevent this, the device is designed so that factory reset is only triggered when the button is held down for a certain amount of time (e.g. 5 seconds).

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!