MyRoboPathOpen Robotics Lab
Arduino Uno #6 of 35
Est. 0.75 Hours
Budget: $1 - $3
Beginner

Debounced Push Button & Multi-Gesture Input

Eliminate mechanical contact bounce in software and distinguish short click, double click, and long press gestures.

Real mechanical switches vibrate microscopically for 5–20ms on every press, causing naïve code to count one click as five. Build a rock-solid state-machine debouncer on Arduino Uno that cleanly detects toggles, short taps, and press-and-hold PWM dimming.

Arduino Uno R3Software DebouncingState MachineShort/Long PressPWM Dimming
Debounced Push Button & Multi-Gesture Input Blueprint
Debounced Push Button & Multi-Gesture InputCAD Blueprint

Key Learning Objectives & Robotics Concepts

By building and programming this project, you will master the following core engineering skills:

1Observe raw mechanical switch contact bounce and understand why it breaks counters and toggles.
2Implement a non-blocking 50ms millis() debounce filter without blocking delay() calls.
3Measure press duration on button release to separate Short Clicks from Long Holds.
4Control LED luminosity smoothly using 8-bit PWM (analogWrite 0–255).

Power Architecture & Voltage Staging

5V / 3.3V Regulated Power

Supplied via Arduino Uno 5V USB rail. The push button module (or standalone tactile switch with INPUT_PULLUP) draws <0.2mA while D9 drives the status LED through a 220Ω resistor. 50ms debounce filter rejects contact chatter; 220Ω resistor protects PWM LED on D9.

Debounced Push Button & Multi-Gesture Input Power Architecture Schematic
Figure: Regulated Arduino Uno supply rails, current budget, and component protection.Power Schematic

Bill of Materials (BOM) & Hardware Components

Bill of Materials for Debounced Push Button & Multi-Gesture Input
ComponentSpecifications & AlternativeQtyApprox Cost
Arduino Uno R3ATmega328P @ 16MHz1$8.00
Push Button Module (or Tactile Switch)Momentary SPST input switch1$0.50
5mm LED + 220Ω ResistorPWM-dimmable visual indicator1$0.15
Breadboard & Jumper WiresStandard prototyping kit1$2.00

Electrical & System Architecture

Connect Button Signal to D2 (using INPUT_PULLUP to GND) and PWM-capable LED anode through 220Ω resistor to D9.

Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for Debounced Push Button & Multi-Gesture Input.
Figure: Complete Arduino Uno R3 pin-to-pin wiring schematic for Debounced Push Button & Multi-Gesture Input.Wiring Schematic

Autonomous Behavior & Finite State Machine (FSM)

Deterministic Arduino loop handling Push Button Module + PWM LED acquisition, filtering, and real-time output updates.

Figure: Firmware execution flow and state transitions for Debounced Push Button & Multi-Gesture Input.
Figure: Firmware execution flow and state transitions for Debounced Push Button & Multi-Gesture Input.Control Algorithm

Step-by-Step Physical Assembly & Wiring Tutorial

Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.

1

Wire Button & PWM-Capable Pin D9

Duration: 5 Mins

Connect the push button to D2 and place the LED on PWM pin D9 (marked with ~ on the Uno header).

Debounced Push Button & Multi-Gesture Input hardware layout diagram
Stage 1: Push Button Module + PWM LED hardware inspection & pinout overview.Stage 1 Diagram
Step Action Checklist:
  • Connect push button between D2 and GND (if using a 3-pin module, also wire VCC to 5V).
  • Connect D9 → 220Ω resistor → LED anode, and LED cathode → GND.
  • Confirm D9 has the tilde (~) PWM capability mark on the Arduino Uno silkscreen.
Engineering & Tuning Tip: Only pins D3, D5, D6, D9, D10, and D11 support hardware analogWrite() PWM on Arduino Uno.
2

Observe Bounce with Diagnostic Sketch

Duration: 5 Mins

Run the raw edge counter diagnostic to see how a single tap can register 2–4 false edges.

Debounced Push Button & Multi-Gesture Input breadboard wiring check diagram
Stage 2: Breadboard layout & Arduino Uno pin-to-pin wiring audit.Stage 2 Diagram
Step Action Checklist:
  • Upload the diagnostic snippet below and open Serial Monitor.
  • Tap the switch briskly several times and watch raw edge counts jump.
  • This proves why software debouncing is essential for every human interface.
Engineering & Tuning Tip: Even high-grade tactile switches bounce for 2–15ms; 50ms covers virtually all mechanical switches cleanly.
3

Flash Multi-Gesture Debouncer Firmware

Duration: 10 Mins

Upload the full debounced short-press toggle and long-press dimmer sketch.

Debounced Push Button & Multi-Gesture Input power rail protection diagram
Stage 3: Supply rail check (USB 5.0V Regulated Rail (≈15mA)) and circuit protection verification.Stage 3 Diagram
Step Action Checklist:
  • Flash the production sketch below.
  • Open Serial Monitor at 115200 baud.
  • Verify a quick tap toggles the LED cleanly with zero false double-triggers.
Engineering & Tuning Tip: Keep long-press threshold around 600–800ms for natural human ergonomics.
4

Test Press-and-Hold PWM Dimming

Duration: 10 Mins

Hold the button down past 700ms and watch the LED ramp smoothly in brightness.

Debounced Push Button & Multi-Gesture Input serial telemetry verification diagram
Stage 4: Firmware upload & 115200 baud Serial Monitor verification.Stage 4 Diagram
Step Action Checklist:
  • Turn the LED ON with a quick tap.
  • Press and hold the button for 2 seconds; watch brightness cycle smoothly from 20 to 255.
  • Release at your favorite brightness—future short taps remember that level!
Engineering & Tuning Tip: Logging brightness values over Serial helps verify smooth PWM wraparound.
5

Stress-Test Rapid Clicking

Duration: 5 Mins

Perform 20 rapid taps and verify every single press toggles exactly once.

Debounced Push Button & Multi-Gesture Input live operational logic diagram
Stage 5: Live operational logic flow & calibration verification.Stage 5 Diagram
Step Action Checklist:
  • Count 20 quick taps aloud while watching the Serial toggle counter.
  • Verify final count equals 20 with zero missed or duplicated clicks.
  • Record debounce latency if you tune DEBOUNCE_MS down to 30ms.
Engineering & Tuning Tip: Never use delay(50) inside main robotics loops—non-blocking debounce lets sensors run in parallel.

Subsystem Benchtop Verification Scripts

Test each hardware subsystem independently before deploying the full autonomous firmware.

1Bench Diagnostic: Push Button Module + PWM LED Hardware Check

Subsystem Diagnostic

Minimal standalone sketch to verify pin wiring and signal response for Push Button Module + PWM LED before running the full project firmware.

Arduino IDE Diagnostic ScriptC++ / Arduino
// Diagnostic: Raw Un-Debounced Edge Counter
const uint8_t BTN = 2;
int lastState = HIGH;
unsigned long rawCount = 0;
void setup() { pinMode(BTN, INPUT_PULLUP); Serial.begin(115200); }
void loop() {
  int cur = digitalRead(BTN);
  if (cur == LOW && lastState == HIGH) { Serial.print("Raw Edge #"); Serial.println(++rawCount); }
  lastState = cur;
}
Expected Output: Shows occasional multiple raw edges on a single physical press, illustrating contact bounce.

Complete Production Firmware

Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.

arduino_uno_debounced_push_button_gestures.ino
arduino_uno_debounced_push_button_gestures.inoArduino C++
/**
 * Project 6: Debounced Short-Press Toggle & Long-Press PWM Dimmer
 * Board: Arduino Uno R3 | Pins: D2 (Button), D9 (PWM LED)
 */
const uint8_t BTN_PIN = 2;
const uint8_t LED_PIN = 9;
const unsigned long DEBOUNCE_MS = 50;
const unsigned long LONG_PRESS_MS = 700;

bool rawLast = HIGH;
bool stableState = HIGH;
unsigned long lastBounceTime = 0;
unsigned long pressStartTime = 0;
bool longPressActive = false;

bool ledOn = false;
int brightness = 200;
int dimStep = 5;
unsigned long lastDimTick = 0;

void setup() {
  pinMode(BTN_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(115200);
  Serial.println(F("Debounced Multi-Gesture Controller Ready"));
}

void loop() {
  unsigned long now = millis();
  bool reading = digitalRead(BTN_PIN);

  if (reading != rawLast) {
    lastBounceTime = now;
    rawLast = reading;
  }

  if (now - lastBounceTime >= DEBOUNCE_MS && reading != stableState) {
    stableState = reading;
    if (stableState == LOW) {
      pressStartTime = now;
      longPressActive = false;
    } else {
      if (!longPressActive) {
        ledOn = !ledOn;
        Serial.print(F("Short Click -> LED "));
        Serial.println(ledOn ? F("ON") : F("OFF"));
      } else {
        Serial.print(F("Dimming Locked at PWM="));
        Serial.println(brightness);
      }
    }
  }

  if (stableState == LOW && (now - pressStartTime >= LONG_PRESS_MS)) {
    longPressActive = true;
    ledOn = true;
    if (now - lastDimTick >= 25) {
      lastDimTick = now;
      brightness += dimStep;
      if (brightness >= 255 || brightness <= 15) dimStep = -dimStep;
      brightness = constrain(brightness, 15, 255);
    }
  }

  analogWrite(LED_PIN, ledOn ? brightness : 0);
}

Firmware Architecture Explanation:

  • Filters out contact chatter by requiring 50ms of continuous stability before accepting a logic transition.
  • Measures elapsed hold time on LOW state: holding ≥700ms smoothly ramps Timer1 PWM brightness.
  • Short taps (<700ms) cleanly toggle the LED while preserving the last dialed brightness level.

Operational Verification & Pin State Matrix

1Double-check all 3 Arduino Uno pin connections against the wiring schematic before plugging in USB.
2Upload the Bench Diagnostic sketch first and confirm Serial Monitor at 115200 baud matches: Shows occasional multiple raw edges on a single physical press, illustrating contact bounce.
3Upload the complete production sketch and verify every condition in the operational truth table below.

Input Signal vs. System Output Truth Table:

Operational truth table for Debounced Push Button & Multi-Gesture Input
Input / StatePin / SignalDetected ConditionExpected Output
Tap < 700msD2 LOW→HIGHShort Press GestureToggle LED ON / OFF
Hold ≥ 700msD2 Sustained LOWLong Press GestureSweep PWM Brightness 15↔255

Engineering Troubleshooting Matrix

Engineering troubleshooting matrix for Debounced Push Button & Multi-Gesture Input
ProblemLikely CauseDiagnostic CheckEngineering Solution
Button triggers toggle twice per clickDEBOUNCE_MS set too low for worn tactile switchMonitor Serial messages per tapIncrease DEBOUNCE_MS from 50 to 70ms.
LED jumps straight from OFF to FULL without smooth dimmingLED wired to a non-PWM pin (like D8 or D12)Verify wire is on D9 (~)Move LED resistor wire to PWM pin D9.
Long press never activatesActive-HIGH module used instead of active-LOWRun diagnostic to see idle levelInvert LOW/HIGH checks if using an external pull-down module.

Engineering Challenges & Upgrades

Take your build to the next level with these progressive engineering upgrades:

Level 1Double-Click Max Boost

Detect two clicks within 350ms to immediately jump LED brightness to 100%.

Level 24-LED Simon Memory Game

Wire 4 buttons and 4 LEDs to build a progressive sequence-recall memory game.

Level 3Hardware Interrupt Wake

Attach attachInterrupt() on D2 (INT0) to log press timestamps with microsecond accuracy.