Short Answer: Mechanical switches don't close cleanly — they "bounce" open/closed for 1–20 ms, causing multiple false triggers.
Detailed: When you press a switch, the metal contacts literally bounce like a dropped spring. A microcontroller reading the pin might see:
Press: HIGH ──┐┌─┐┌─┐┌──────────
││ ││ │
Actual: LOW ──┘└─┘└─┘└──────────
↑ bounce (1-10ms)Fixes (from easiest to most robust):
| Method | How | Good for |
|---|---|---|
| Software debounce | Read pin, wait 10ms, read again | Most microcontrollers (Arduino, ESP32) |
| RC filter + Schmitt trigger | 10kฮฉ + 1ยตF to ground | Simple, no code |
| Hardware debounce IC (MAX6816) | Dedicated chip | Multiple switches, professional |
| Schmitt trigger gate (74HC14) | Clean edges | When software isn't an option |
Arduino code example:
const int buttonPin = 2; int lastState = LOW; unsigned long lastDebounce = 0; void loop() { int reading = digitalRead(buttonPin); if (reading != lastState) { lastDebounce = millis(); } if ((millis() - lastDebounce) > 10) { // Button is stable — act on it } lastState = reading; }
Rule of thumb: 10–50 ms debounce is enough for most buttons. Use 5 ms for tact switches, 20 ms for large toggle switches.

Comments
Post a Comment