Microcontrollers · #8 of 20
GPIO Inputs
Floating, Pull-ups, Debouncing
Why it matters
A real button press should be one event. Without debouncing you get phantom presses, double-clicks, and flaky menus — even with perfect code everywhere else.
The idea
Goal
Turn a noisy mechanical switch into a clean digital signal you can trust. <h3>What's actually happening</h3>
A switch is two pieces of metal touching. When they first touch, they bounce for a few milliseconds:
HIGH/LOW/HIGH/LOW… then finally settle. Your CPU is fast enough to see all those transitions.
<div class=
Demo
The top timeline is the raw GPIO signal (with bounce). The bottom is the debounced output.
Use:
- Bounce Severity: how
Key takeaways
- Mechanical switches bounce for a few milliseconds
- A floating input will randomly flip — use pull-ups/pull-downs
- Debouncing is a small state machine: detect change → wait for stability → accept
- On ESP32, some pins are input-only (34–39) and some are strapping pins (boot-sensitive)
Going deeper
Hardware debounce (RC + Schmitt trigger) can reduce CPU work and improve EMI robustness. For event-driven code, interrupts still need debouncing — either by masking interrupts for a window, or by using a timer task to confirm stability.
Math details
Definitions: sample_rate = N samples / second debounce_window = W seconds Rule of thumb: W should be several times the worst-case bounce duration. Simple debounce state machine: if raw != pending: pending = raw stable_time = 0 else: stable_time += dt if stable_time >= W: debounced = pendingImplementation
LLM Prompt: GPIO Debounce