EN

Push-button debouncing

RC filter with Schmitt trigger, simulation and software debouncing

Contact bounce

When a mechanical contact closes, its metal parts collide and bounce several times before settling. For a few milliseconds the signal flips between 0 and 1 dozens of times. A lamp does not care, but a microcontroller reads millions of times per second and counts every bounce as a press: a counter jumps by three, a menu skips options or a software toggle switch turns on and off by itself.

How long the bounce lasts depends on the type of contact, its wear and how it is pressed. Most push-buttons and microswitches bounce for less than 5 ms, but some models and worn contacts reach 20 ms or more. That is why designs usually target 10 to 20 ms: faster than a person notices and enough for almost every case.

RC filter with Schmitt trigger

With the button open, the capacitor charges to VCC through R1 and R2. When pressed, it discharges through R2: even if the contact bounces, the capacitor has no time to recover and its voltage falls smoothly. A short bounce only causes a small rise that does not reach the threshold. R2 also limits the discharge current, which without it would be a spike of several amperes that wears the contact.

The gate must have a Schmitt trigger input (74HC14, 74LVC1G17, CD40106 or the hysteresis inputs of many microcontrollers). It has two thresholds: the output only changes when the input rises above VT+ or falls below VT−. A normal input with a slowly rising voltage would cross its single threshold several times and generate pulses again.

\[ t_{\text{press}} = R_2\,C\,\ln\frac{V_{CC}}{V_{T-}} \qquad t_{\text{release}} = (R_1 + R_2)\,C\,\ln\frac{V_{CC}}{V_{CC} - V_{T+}} \]

Both delays must be longer than the bounce time. The calculator proposes standard R1 and R2 values for a given capacitor.

Requirements
ms
nF
kΩ
Schmitt trigger
V
V
V
Circuit values
kΩ
kΩ

The default thresholds are indicative for a 74HC14 at 5 V. For a conservative design use the maximum VT− and the minimum VT+ from the datasheet, and remember that high-capacitance ceramic capacitors can lose a good part of their value with the applied voltage.

Software debouncing

If the signal goes to a microcontroller, the cheapest option is to filter in the program. The idea is the same as in the RC filter: a change is only accepted once the input has been stable for a while. With an internal pull-up, the button goes straight from the pin to ground with no other components.

const uint8_t PIN = 2;
const unsigned long T_DEBOUNCE = 20;     // ms
int state = HIGH, lastRead = HIGH;
unsigned long tChange = 0;

void setup() { pinMode(PIN, INPUT_PULLUP); }

void loop() {
  int r = digitalRead(PIN);
  if (r != lastRead) {                   // the reading changed: restart the timer
    tChange = millis();
    lastRead = r;
  }
  if (millis() - tChange >= T_DEBOUNCE && r != state) {
    state = r;                           // stable for T_DEBOUNCE: valid change
    if (state == LOW) {
      // action on press
    }
  }
}

Comparison of methods

MethodComponentsAdvantagesDrawbacks
SoftwareNone (internal pull-up)Free, adjustable, works for many inputsTakes CPU time; does not protect against noise or fast interference
RC + Schmitt trigger2 resistors, capacitor, gateAlso filters noise; frees the program; valid for interrupts and discrete logicMore components and a fixed delay
SR latch with changeover switchTwo-contact push-button and 2 NAND gatesNo delay and no bounce: the first contact sets the stateNeeds a changeover contact (three terminals)
Dedicated integrated circuitOne chip (for example MAX6816 or MC14490)Guaranteed result, often with ESD protectionCost and availability

In automation, the digital inputs of PLCs already include a configurable filter, usually 3 to 10 ms (shorter on fast counting inputs). If a sensor or a contact seems to "count twice", check that filter first.

Related: pull-up and pull-down resistors, PLC ladder simulator, capacitors and RC and RLC circuits.