ESP32 S3 - Button - Long Press Short Press

This tutorial teaches you how to program your ESP32 S3 to detect both button long press and short press events. You will learn everything from basic wiring and debouncing techniques to combining multiple press types on a single button for more capable input handling.

What you'll build:

  1. A short press detector that measures press duration below a threshold
  2. A long press detector triggered on release or during the press
  3. A dual-mode button that distinguishes short and long press on the same pin
  4. A debounced version using the ezButton library for production-ready reliability
ESP32 S3 - Button - Long Press Short Press

Hardware Preparation

1×ESP32 S3 WROOM N16R8
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
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

Or you can buy the following kits:

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

A push button is a simple input device that completes an electrical circuit when pressed. When connected to the ESP32 S3 with the internal pull-up resistor enabled, the pin reads HIGH in the released state and LOW when the button is pressed. This makes it straightforward to detect both press and release events precisely.

Key Specifications

Push buttons used with the ESP32 S3 support multiple interaction patterns: short press, long press, and double-click. They work directly with the ESP32 S3's internal pull-up resistors, eliminating the need for external resistors. Because physical buttons produce mechanical noise (chattering), debouncing is required for reliable detection in real-world projects.

Additional button resources:

Wiring Diagram

Connect your button to the ESP32 S3 by wiring one terminal to a GPIO pin and the other terminal to GND. The internal pull-up resistor is enabled in software, so no external resistor is required.

Safety Notes

Always verify your wiring before powering the board. Using a breadboard makes it easy to make changes without soldering. Since this tutorial uses the internal pull-up resistor, the button state reads HIGH when released and LOW when pressed — keep this in mind when reading the code.

Component Pin ESP32 S3 Pin
Button Terminal 1 GPIO 9
Button Terminal 2 GND
The wiring diagram between ESP32 S3 Button

This image is created using Fritzing. Click to enlarge image

How To Detect Short Press

Detecting a short press on the ESP32 S3 involves measuring the duration between the button press event and the release event. If that duration falls below a defined threshold, the short press is confirmed.

Short press detection method:

  • 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
  • Compare press duration against your SHORT_PRESS_TIME threshold

Step-by-step implementation:

  • 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");

ESP32 S3 Code for detecting the short press

The following sketch reads the button state using the ESP32 S3's internal pull-up resistor, calculates the duration between press and release, and prints a message to the Serial Monitor when a short press is detected. The threshold is set to 500 ms by default, which you can adjust for your application.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #define BUTTON_PIN 21 // The ESP32 S3 pin GPIO21 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(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload Code: Copy the code above and upload it to your ESP32 S3 via Arduino IDE
  3. Wire Hardware: Connect the button to your ESP32 S3 following the wiring diagram
  4. Test Button: Press the button shortly several times
  5. Check Results: Open the Serial Monitor (115200 baud) to see the detection results
  6. Pro Tip: Adjust the SHORT_PRESS_TIME value to make short press detection faster or slower
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:01:12.345] A short press is detected [00:01:13.891] A short press is detected [00:01:15.204] A short press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ 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 methods for detecting long press button events on the ESP32 S3, each suited to different application requirements. The first method detects the long press after the button is released, while the second method detects it in real time as the button is being held down.

Two use cases for long press detection:

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

First method (detect on release):

  • 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

Second method (detect during press):

  • Measure the pressing time continuously while button is held
  • If the duration exceeds the pre-defined time, immediately detect long-press
  • Continue checking until the button is released

ESP32 S3 Code for detecting long press when released

This sketch measures the total press duration from the moment the button is pressed to when it is released. If the duration exceeds LONG_PRESS_TIME (1000 ms), the long press event is reported via the Serial Monitor. This method is best when you want to confirm the user held the button for the full duration before taking action.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #define BUTTON_PIN 21 // The ESP32 S3 pin GPIO21 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(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload Code: Copy and upload the above code to ESP32 S3 via Arduino IDE
  3. Test Long Press: Press and hold the button for more than one second, then release
  4. Check Serial Monitor: Open Serial Monitor at 115200 baud to see the results
  5. Pro Tip: This method is best when you need the action to happen after button release, not during the press
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:02:05.112] A long press is detected [00:02:08.774] A long press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

ESP32 S3 Code for detecting long press during pressing

This sketch continuously checks the press duration while the button is held and triggers the long press detection the instant the threshold is exceeded, without waiting for the button to be released. This is ideal for applications that require immediate feedback, such as triggering a factory reset while the button is still held.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #define BUTTON_PIN 21 // The ESP32 S3 pin GPIO21 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(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload Code: Upload the above code to your ESP32 S3 via Arduino IDE
  3. Test During Press: Press and hold the button for several seconds (don't release immediately)
  4. Observe Timing: Notice the detection happens while you're still pressing
  5. Pro Tip: This method provides instant feedback and is better for time-sensitive applications like factory reset functions
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:03:22.601] A long press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

How To Detect Both Long Press and Short Press

By combining both press-duration checks in a single sketch, one button on your ESP32 S3 can control two entirely independent functions. This reduces component count and simplifies your circuit without sacrificing usability.

Short Press and Long Press after released

This sketch distinguishes short and long press events based on how long the button was held, making the determination only after the button is released. A single LONG_PRESS_TIME threshold separates the two behaviors, enabling two different actions from one button with no additional hardware.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #define BUTTON_PIN 21 // The ESP32 S3 pin GPIO21 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(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload Code: Upload the above code to ESP32 S3 via Arduino IDE
  3. Test Both Types: Try both long press (hold >1 second) and short press (quick tap)
  4. Check Serial Monitor: Open Serial Monitor to see which press type is detected
  5. Pro Tip: Set LONG_PRESS_TIME to match your user's expected behavior (typically 500–1500 ms works well)
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:04:10.330] A short press is detected [00:04:13.890] A long press is detected [00:04:15.210] A short press is detected [00:04:16.540] A short press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

※ 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 sketch detects long press immediately when the threshold is reached while still allowing short press to be detected on release if the threshold was never met. This gives the fastest possible response to long press actions while keeping short press detection fully functional.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #define BUTTON_PIN 21 // The ESP32 S3 pin GPIO21 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(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Upload Code: Upload the code to your ESP32 S3 via Arduino IDE
  3. Test Timing: Try quick taps and long holds to see the different behaviors
  4. Observe Response: Notice long press triggers immediately, not on release
  5. Pro Tip: This method prevents accidental long press triggers since users get instant feedback
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:05:02.145] A short press is detected [00:05:05.768] A long press is detected [00:05:07.093] A short press is detected [00:05:10.412] A long press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Long Press and Short Press with Debouncing

Mechanical buttons generate electrical noise when their contacts open and close, causing the microcontroller to read multiple transitions for a single physical press. Debouncing filters out this noise and is essential for reliable long press and short press detection on the ESP32 S3.

The ezButton library handles all debouncing logic automatically, making it the recommended approach for beginners and production projects alike. It works reliably with multiple buttons and integrates cleanly with the press-duration timing logic used in previous sections.

Short Press and Long Press with debouncing after released

This sketch combines the ezButton library's debouncing with post-release press-type detection. It eliminates false detections from button chattering while accurately distinguishing short and long presses, giving you a production-ready input handler for your ESP32 S3 project.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(21); // create ezButton object for pin Arduino Nano ESP32 pin GPIO21 unsigned long pressed_time = 0; unsigned long released_time = 0; void setup() { Serial.begin(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Install Library: Install ezButton library - See How To
  3. Upload Code: Upload the above code to ESP32 S3 via Arduino IDE
  4. Test Clean Detection: Try both long and short presses - no more duplicate detections
  5. Check Results: Open Serial Monitor to see clean, accurate button press detection
  6. Pro Tip: The ezButton library eliminates all chattering issues automatically — use it for all production projects
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:06:14.221] A short press is detected [00:06:17.885] A long press is detected [00:06:19.103] A short press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Short Press and Long Press with debouncing During Pressing

This sketch adds ezButton debouncing to the real-time long press detection approach, combining instant long press response with clean noise filtering. It is the most reliable method for ESP32 S3 projects where both accuracy and responsiveness are required.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-button-long-press-short-press */ #include <ezButton.h> #define SHORT_PRESS_TIME 1000 // 1000 milliseconds #define LONG_PRESS_TIME 1000 // 1000 milliseconds ezButton button(21); // create ezButton object for pin Arduino Nano ESP32 pin GPIO21 unsigned long pressed_time = 0; unsigned long released_time = 0; bool is_pressing = false; bool is_long_detected = false; void setup() { Serial.begin(115200); 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

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Install Library: Install ezButton library - See How To
  3. Upload Code: Upload the code to your ESP32 S3 via Arduino IDE
  4. Test Reliability: Press the button multiple times - notice no duplicate readings
  5. Compare Methods: This is the most reliable method for production ESP32 S3 projects
  6. Pro Tip: Use this method for critical applications like device resets or important controls where accuracy is essential
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[00:07:31.447] A short press is detected [00:07:34.922] A long press is detected [00:07:36.250] A short press is detected [00:07:39.788] A long press is detected
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Why Needs Long Press and Short Press

Adding multi-press detection to your ESP32 S3 projects unlocks more functionality from a single button without increasing hardware complexity.

  1. Save pins: A single button can control two or more functions (short press for light, long press for fan)
  2. Prevent accidents: Use long press for critical functions like factory reset to avoid accidental activation
  3. Better UX: Users can perform multiple actions without additional hardware
  4. Cost savings: Fewer buttons means simpler circuits and lower component costs
  5. Space efficient: Ideal for compact ESP32 S3 projects with limited space
  6. Professional design: Multi-function buttons are standard in consumer electronics

Application Ideas

Long press and short press detection opens up a wide range of practical use cases for your ESP32 S3 projects.

  1. Smart home light controller: short press for on/off, long press for dimming mode
  2. Alarm system: short press to arm/disarm, long press for panic mode
  3. Timer device: short press to start/stop, long press to reset
  4. Multi-function thermostat: short press adjusts temperature, long press switches heating/cooling mode
  5. IoT door lock: short press to unlock temporarily, long press for permanent unlock
  6. Battery-powered sensor: short press to check status, long press to enter deep sleep mode

Video Section

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

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