Study debounce circuits that eliminate signal noise caused by mechanical switch bouncing to ensure reliable digital inputs.
When pressing a mechanical button, the microcontroller often reads multiple presses instead of one. The contacts bounce for a few milliseconds before settling, and each bounce is an edge that the digital circuit detects. The debounce circuit, or anti-bounce circuit, eliminates these unwanted pulses, either in hardware with a filter or a latch, or in software with a time verification [1][2][3][4].
When two contacts touch, the spring and the mass of the contacts cause them to bounce, and the signal switches multiple times from high to low before stabilizing. The same happens on opening. The bounce duration ranges from less than $1\ \mathrm{ms}$ in high-quality switches to about $10\ \mathrm{ms}$ in cheap ones. A fast input, such as a counter, can register dozens of pulses for a single press [1][2].
The simplest hardware solution is a low-pass filter, made of a resistor and a capacitor, followed by a Schmitt trigger gate, which has two different thresholds for rising and falling edges. The capacitor cannot change voltage abruptly, and the hysteresis of the thresholds prevents noise from causing output oscillations. The time constant $\tau = RC$ must be chosen longer than the bounce duration but shorter than the acceptable response time [1][3][4].
With a two-position switch (toggle), an SR latch with two NAND gates switches on the first contact and ignores subsequent bounces, because the state remains until the opposite contact. It does not work with a simple single-position pushbutton [1][2].
The program reads the pin at regular intervals and accepts the new level only after it has remained the same for a certain number of consecutive readings. It is also possible to ignore the pin for a fixed time after the first edge. The method requires no components but uses processor time and, if implemented in an interrupt routine, requires care [1][2].
1. Filter with $R = 10\ \mathrm{k\Omega}$ and $C = 100\ \mathrm{nF}$: $\tau = 1\ \mathrm{ms}$.
2. Supply $5\ \mathrm{V}$, high threshold $3\ \mathrm{V}$: to change the output level, the voltage on the capacitor must rise to $3\ \mathrm{V}$, which requires $-\tau\ln(1 - 3/5) = 0.92\ \mathrm{ms}$ of continuous charging.
3. A bounce interval of $0.5\ \mathrm{ms}$ brings the capacitor only to $5\,(1 - e^{-0.5}) = 1.97\ \mathrm{V}$: not enough, and the bounce is ignored.
4. Low threshold $1.8\ \mathrm{V}$ on release: the fall from $5\ \mathrm{V}$ requires $\tau\ln(5/1.8) = 1.02\ \mathrm{ms}$.
5. Software reading every $1\ \mathrm{ms}$ and ten identical consecutive readings: the decision requires $10\ \mathrm{ms}$ from the last bounce.
Buttons and keyboards, rotary encoders, limit switches, microcontroller input switches, event counters, and user interfaces.
The RC filter delays the response by a few milliseconds, and a capacitor that is too large causes rapid pulses to be lost. Without a Schmitt trigger, a slowly rising signal can cause a digital input to oscillate.
[1] Wikipedia (de): Prellen
[2] Wikipedia (en): Switch
[3] Wikipedia (en): Schmitt trigger
[4] Wikipedia (en): Low-pass filter
Generating summary…