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.
Key Learning Objectives & Robotics Concepts
By building and programming this project, you will master the following core engineering skills:
Power Architecture & Voltage Staging
5V / 3.3V Regulated PowerSupplied 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.
Bill of Materials (BOM) & Hardware Components
| Component | Specifications & Alternative | Qty | Approx Cost |
|---|---|---|---|
| Arduino Uno R3 | ATmega328P @ 16MHz | 1 | $8.00 |
| Push Button Module (or Tactile Switch) | Momentary SPST input switch | 1 | $0.50 |
| 5mm LED + 220Ω Resistor | PWM-dimmable visual indicator | 1 | $0.15 |
| Breadboard & Jumper Wires | Standard prototyping kit | 1 | $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.
Autonomous Behavior & Finite State Machine (FSM)
Deterministic Arduino loop handling Push Button Module + PWM LED acquisition, filtering, and real-time output updates.
Step-by-Step Physical Assembly & Wiring Tutorial
Follow each of the 5 sequential milestone stages with technical photos, wiring checks, and pre-flight tests.
Wire Button & PWM-Capable Pin D9
Connect the push button to D2 and place the LED on PWM pin D9 (marked with ~ on the Uno header).
- 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.
Observe Bounce with Diagnostic Sketch
Run the raw edge counter diagnostic to see how a single tap can register 2–4 false edges.
- 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.
Flash Multi-Gesture Debouncer Firmware
Upload the full debounced short-press toggle and long-press dimmer sketch.
- Flash the production sketch below.
- Open Serial Monitor at 115200 baud.
- Verify a quick tap toggles the LED cleanly with zero false double-triggers.
Test Press-and-Hold PWM Dimming
Hold the button down past 700ms and watch the LED ramp smoothly in brightness.
- 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!
Stress-Test Rapid Clicking
Perform 20 rapid taps and verify every single press toggles exactly once.
- 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.
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 DiagnosticMinimal standalone sketch to verify pin wiring and signal response for Push Button Module + PWM LED before running the full project firmware.
// 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;
}Complete Production Firmware
Full working C++ source code with pin mappings, non-blocking timers, and FSM avoidance routines.
/**
* 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
Input Signal vs. System Output Truth Table:
| Input / State | Pin / Signal | Detected Condition | Expected Output |
|---|---|---|---|
| Tap < 700ms | D2 LOW→HIGH | Short Press Gesture | Toggle LED ON / OFF |
| Hold ≥ 700ms | D2 Sustained LOW | Long Press Gesture | Sweep PWM Brightness 15↔255 |
Engineering Troubleshooting Matrix
| Problem | Likely Cause | Diagnostic Check | Engineering Solution |
|---|---|---|---|
| Button triggers toggle twice per click | DEBOUNCE_MS set too low for worn tactile switch | Monitor Serial messages per tap | Increase DEBOUNCE_MS from 50 to 70ms. |
| LED jumps straight from OFF to FULL without smooth dimming | LED 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 activates | Active-HIGH module used instead of active-LOW | Run diagnostic to see idle level | Invert 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:
Detect two clicks within 350ms to immediately jump LED brightness to 100%.
Wire 4 buttons and 4 LEDs to build a progressive sequence-recall memory game.
Attach attachInterrupt() on D2 (INT0) to log press timestamps with microsecond accuracy.