9. Single Double Click and Long Press Detection

Understanding and Interfacing a Push Button Switch

A standard push button has four pins, where pins 1–2 are internally connected, and pins 3–4 are also connected. When the button is pressed, all pins become connected (closed circuit).

Push-button-switch-symbol

Important: Avoid Floating GPIO Pins

If the switch is not pressed and there's no pull-up or pull-down resistor, the input pin may "float," giving random HIGH/LOW readings.

Use a pull-up or pull-down resistor to keep the pin at a known voltage when idle.

Switch Interfacing Without Pull-up/ Pull-down Resistor:

switch-interfacing-without-pullup

 

Switch Interfacing With Pull-up/ Pull-down Resistor:

switch-with-pullup-pulldown-resistor

Debouncing

  • When detecting single-clicks, double-clicks, or long-presses, two key factors must be considered: timing and debouncing.
  • Debouncing refers to the suppression of rapid on/off fluctuations caused by the mechanical contacts inside a push button. These fluctuations, known as bounces, can lead to false triggers or multiple detections for a single press.
  • To ensure reliable button detection, debouncing must be handled—either through hardware filters (like RC circuits) or software techniques (such as delay-based or state-based logic).
DSO-Output-Switch-Debouncing

Detecting Press Types

We can classify button presses using timing:

  • Single Click: One short press.
  • Double Click: Two clicks within 400 ms.
  • Long Press: Button held for more than 1 second.

So by selecting the correct switch-interfacing configurations and click timings, we can implement the given task.

Below are the solutions to the given task using different microcontrollers

  1. STM32
  2. ESP32
  3. Arduino UNO