22. Debounce switch input

Circuit

Loading simulator…

Design Approach

A mechanical switch can create short unwanted closures. The solution uses an RC network so a very brief closure does not immediately pull GPIO_IN LOW, but a sustained closure does.

How the Circuit Works

  1. Start with a defined HIGH. The supplied R_PULL = 10 kΩ connects GPIO_IN toward the 3.3 V supply when SW1 is open.
  2. Add filtering. The reference solution adds R_FILTER = 4.7 kΩ between the switch/pull-up junction and the GPIO_IN/capacitor node, plus C1 = 1 µF from the input node to ground.
  3. Reject a short closure. When SW1 closes, the charged capacitor discharges through R_FILTER with approximately τ=4.7ms\tau = 4.7\,\mathrm{ms}. After a 0.2 ms closure, VGPIO_IN≈3.3e−0.2/4.7V≈3.16VV_{\mathrm{GPIO\_IN}}\approx3.3e^{-0.2/4.7}\,\mathrm{V}\approx3.16\,\mathrm{V}, well above 2 V.
  4. Respond to a sustained closure. At 10 ms, the ideal discharge estimate is 3.3e−10/4.7V≈0.39V3.3e^{-10/4.7}\,\mathrm{V}\approx0.39\,\mathrm{V}, under 0.8 V. On reopening, the capacitor recharges through the pull-up and filter resistance, so the HIGH return is also gradual.

Verify the Circuit

In EWsim, start with SW1 open and the input settled. Close it for 0.2 ms and check GPIO_IN≥2V\mathrm{GPIO\_IN} \ge 2\,\mathrm{V}; then repeat with a 10 ms closure and check GPIO_IN≤0.8V\mathrm{GPIO\_IN} \le 0.8\,\mathrm{V}. Experiment with a larger C1 to see the response slow.

Key Learnings

  • RC debouncing uses charge storage to reject short state changes.
  • Charge and discharge paths can have different time constants even when the same capacitor is used.