Arduino Nano 33 IoT - Button - Long Press Short Press

This guide explains how to use the Arduino Nano 33 IoT to tell the difference between a long press and a short press. In simple terms, you will learn:

Hardware Preparation

1×Arduino Nano 33 IoT
1×Micro USB Cable
1×Breadboard-mount Button with Cap
1×Breadboard-mount Button Kit
1×Panel-mount Push Button
1×Push Button Module
1×Breadboard
1×Jumper Wires
1×Optionally, DC Power Jack
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

We have easy guides about buttons. Each guide gives clear details and step-by-step instructions on hardware pinouts, how they work, connecting wiring to the Arduino Nano 33 IoT, and using the Arduino Nano 33 IoT code. Find out more at these links:

Wiring Diagram

The wiring diagram between Arduino Nano and 33 IoT Button

This image is created using Fritzing. Click to enlarge image

This guide uses the built-in pull-up resistor. The button is normally HIGH and goes LOW when pressed.

※ NOTE THAT:

Please note that the Arduino Nano 33 IoT pins A4 and A5 have built-in pull-up resistors for I2C communication. Although these pins can be used as digital input pins, it is recommended to avoid using them for digital input. If you must use them, do not use internal or external pull-down resistors for these pins

How To Detect Short Press

  • Measure the time from when the button is pressed until it is released. If this time is shorter than the set amount, it counts as a short press.

Let's go through it one step at a time.

  • Set the maximum time a short press can last.
#define SHORT_PRESS_TIME 500 // Defines the duration for a short press as 500 milliseconds
  • Check if the button is pressed and record the time it was pressed.
if(prev_state == HIGH && button_state == LOW) pressed_time = millis();
  • Detect when the button is no longer pressed and record the time.
if(prev_state == LOW && button_state == HIGH) released_time = millis();
  • Find out how long the button is pressed.
long press_duration = released_time - pressed_time;
  • Figure out if the button press is short by comparing how long it lasted to the set short press time.
if( press_duration < SHORT_PRESS_TIME ) Serial.println("A short press is detected");

Arduino Nano 33 IoT Code for detecting the short press

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT 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

If you are new to the Arduino Nano 33 IoT, be sure to check out our Getting Started with Arduino Nano 33 IoT tutorial. Then, follow these steps:

  • Connect the components to the Arduino Nano 33 IoT board as depicted in the diagram.
  • Use a USB cable to connect the Arduino Nano 33 IoT board to your computer.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano 33 IoT board and choose its corresponding COM port.
  • Upload the code above to your Arduino Nano 33 IoT using the Arduino IDE.
  • Press the button quickly a few times.
  • Look at the result on the Serial Monitor. It should look like this:
COM6
Send
A short press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ NOTE THAT:

The Serial Monitor might show several short presses even when you press the button once. This is normal and is called the "chattering phenomenon." Later in this tutorial, we will explain how to fix this problem.

How To Detect Long Press

There are two situations where you might check for a long press.

  • The long press is noticed right after you let go of the button.
  • The long press is noticed while you are still holding the button.

For the first example:

  • Measure how long the press lasts. If it goes on longer than a set time, it counts as a long press.

In the second case, while the button is pressed, repeat the following steps:

  • Check how long the button is pressed.
  • If it is pressed longer than the set time, it counts as a long press.
  • Otherwise, keep checking until the button is released.

Arduino Nano 33 IoT Code for detecting long press when released

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT 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

  • Upload the code to the Arduino Nano 33 IoT using the Arduino IDE.
  • After one second, press the button and then let go.
  • Look at the result on the Serial Monitor. It should look like the image below.
COM6
Send
A long press is detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Arduino Nano 33 IoT Code for detecting long press during pressing

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT 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

  • Load the code above onto your Arduino Nano 33 IoT using the Arduino IDE.
  • Wait a few seconds, then press and release the button.
  • Open the Serial Monitor to see the result. It should look like this.
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 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT 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

  • Use Arduino IDE to upload the code to your Arduino Nano 33 IoT.
  • Press the button quickly and then hold it down.
  • Open the Serial Monitor to see the result. It should look like the example shown.

※ NOTE THAT:

The Serial Monitor might show several quick presses when you hold the button down. This is normal and is called the bouncing effect. We will explain how to fix it at the end of this guide.

Short Press and Long Press During pressing

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #define BUTTON_PIN 2 // Arduino Nano 33 IoT 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

  • Upload the code above to the Arduino Nano 33 IoT using the Arduino IDE.
  • Press the button quickly and then press it for a longer time.
  • Open the Serial Monitor to see the result. It should look like this:

Long Press and Short Press with Debouncing

It is very important to make sure the button doesn't send extra signals in many uses.

Debouncing can be tricky, especially when you use several buttons. To make it easier for beginners, we created a library called ezButton.

We will use this library in the code below.

Short Press and Long Press with debouncing after released

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(2); // create ezButton object for pin Arduino Nano 33 IoT 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

  • Install the ezButton library. For help, visit: https://arduinogetstarted.com/tutorials/arduino-button-library#content_how_to_install_library
  • Upload the code to your Arduino Nano 33 IoT using the Arduino IDE.
  • Press the button briefly and press it for a long time.
  • Look at the Serial Monitor to see the results, which should look similar to the example below.

Short Press and Long Press with debouncing During Pressing

/* * This Arduino Nano 33 IoT code was developed by newbiely.com * * This Arduino Nano 33 IoT code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-iot/arduino-nano-33-iot-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(2); // create ezButton object for pin Arduino Nano 33 IoT 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

  • First, install the ezButton library. Check this guide for help: https://arduinogetstarted.com/tutorials/arduino-button-library#content_how_to_install_library
  • Next, send the code above to your Arduino Nano 33 IoT using the Arduino IDE.
  • Press the button briefly and then press it for a longer time.
  • Look at the result in the Serial Monitor. It will appear similar to the example shown below.

Video Tutorial

Why Needs Long Press and Short Press

  • Use one button to do many tasks and reduce the number of digital input pins. For example, you can set a short press to turn on the light and a long press to start the fan.
  • Choose a long press instead of a short press to stop accidental button presses. Some devices use the button for a full reset, and pressing it by mistake could be risky.

※ 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!