Switch Debouncing: 7 Proven Methods for Clean Stable Inputs

Share:
Digital Electronics
Switch Debouncing: 7 Proven Methods for Clean Stable Inputs

A mechanical contact never closes cleanly, it chatters for a few milliseconds and a fast input sees every chatter as a new press. Good switch debouncing filters that noise so one press always means exactly one event.

Contact Bounce RC and Schmitt SR Latch Software Counters

Push buttons, limit switches and relay contacts all bounce before they settle. Switch debouncing, with a small circuit or a few lines of code, turns that messy signal into one clean, trustworthy transition.

Hello everyone, today we are going to learn what contact bounce is, why switch debouncing is needed, how hardware and software methods work and how to calculate an RC debounce time.
switch debouncing

What Is Switch Debouncing?

Switch debouncing is the technique of removing the rapid, unwanted on and off transitions that a mechanical contact produces when it opens or closes, so that the logic input sees only one clean change of state. It matters for every button, keypad, limit switch and relay contact that feeds a digital circuit.

When two metal contacts meet, they strike, rebound and strike again several times before they rest. A microcontroller that samples in microseconds can read each rebound as a separate press, which makes counters jump and menus skip.

Measured bounce times of different mechanical switches from Ganssle tests
Image credit: The Ganssle Group. Chart courtesy of The Ganssle Group, shown here for educational reference.

Switch debouncing can be built in hardware, in firmware or in both. Solid state devices described in types of electronic switches do not bounce, so this topic is only about mechanical contacts.

Advertisement

How Long Does a Contact Bounce?

Embedded systems expert Jack Ganssle tested 18 ordinary switches and recorded every bounce on a logic analyser. Sixteen of them showed an average bounce of 1557 microseconds, with a maximum of 6200 microseconds.

1.6 msAverage bounce, 16 switches
6.2 msMaximum, 16 switches
157 msWorst opening bounce seen
1 to 20 msUsual design window

Two of the tested switches were much worse, and one produced a bounce of 157 milliseconds while opening. Ganssle also found that two samples of the same switch model could differ by a factor of two.

For this reason most switch debouncing designs treat 1 to 20 milliseconds as the normal bounce window and choose a debounce time a little above the worst case they expect. Old, dirty or worn contacts bounce longer than new ones.

Do You Know?

Ganssle observed some bounces shorter than one microsecond when contacts opened. Pulses that narrow can clock a fast flip flop yet remain invisible to a slow multimeter.

7 Proven Switch Debouncing Methods at a Glance

MethodTypeParts or CodeBest For
RC plus Schmitt triggerHardwareResistor, capacitor, Schmitt gateSingle buttons, interrupt pins
SR latch with SPDT switchHardwareTwo NAND gates, changeover switchClock inputs, lab trainers
Debouncer IC such as MAX6816HardwareDedicated chipHarsh ESD, many inputs
Simple delaySoftwareWait then read againQuick prototypes
Counter or integratorSoftwarePeriodic sampling, counterGeneral firmware
State machineSoftwarePressed, released, confirm statesLong press and double click
Shift registerSoftwareBit history in a variableCompact, fast code

Hardware switch debouncing cleans the signal before it reaches the logic, which suits interrupt pins and edge triggered circuits. Software methods cost nothing extra on a microcontroller board and can be tuned later.

Hardware Switch Debouncing Circuits

RC Plus Schmitt Trigger

A pull up resistor and capacitor slow the edge, and a Schmitt input squares it again.

Best for: single buttons on logic or MCU pins
Low cost
SR Latch With SPDT Switch

A changeover switch sets or resets a latch, and the first touch locks the output.

Best for: perfect edges for clocks
Instant
Debouncer IC

A chip samples the input and changes the output only after it stays stable.

Best for: industrial panels and many inputs
Robust

In the RC circuit, the capacitor cannot follow the fast rebounds, so the voltage rises or falls smoothly. A slow edge on a normal gate can cause oscillation, which is why the gate must have hysteresis, as explained in Schmitt trigger hysteresis.

The SR latch method uses two cross coupled NAND gates, described in latches in digital logic. The moving contact touches the set pin first, the latch flips, and rebounds on that pin cannot flip it back because the contact never reaches the reset pin while bouncing.

The latch needs a single pole double throw switch, so it cannot be used with ordinary two terminal push buttons. The same latch appears in SR, JK, D and T flip flops, where debounced clocks are essential.

Maxim, now part of Analog Devices, makes the MAX6816, MAX6817 and MAX6818 debouncers for one, two and eight switches. Their datasheet states that the output changes only after the input is stable for about 40 milliseconds, with ESD protection of plus or minus 15 kV and a supply current of 6 microamps typical.

Quick Tip

Add a small series resistor, around 100 Ω, between the capacitor and the switch contact. It limits the discharge current spike that otherwise erodes contacts and radiates noise.

RC Debounce Time Formula

In the common circuit, a pull up resistor R charges capacitor C toward Vcc when the button is released, and the Schmitt input switches when the capacitor voltage reaches its upper threshold Vth. The charging curve follows the time constant rule in RC and RL time constant.

Charging, button released: t = R × C × ln(Vcc ÷ (Vcc minus Vth))
Discharging, button pressed: t = R2 × C × ln(Vcc ÷ Vth low)

R in kΩ and C in µF give time directly in milliseconds

Example:
R = 10 kΩ, C = 1 µF, Vcc = 5 V, Vth = 3 V
RC = 10 × 1 = 10 ms
ln(5 ÷ (5 minus 3)) = ln(2.5) = 0.916
t = 10 × 0.916 = 9.16 ms

Vth is the upper switching threshold of the Schmitt gate at your supply voltage, taken as 3 V in this example. Always read the real value from the gate datasheet, because it changes with family and supply.

RC Debounce Time Calculator

Release Delay of an RC Debounce Network
Result
Debounce time 9.16 ms, RC = 10.00 ms
Advertisement

Second Worked Example: Designing for 20 ms

Suppose a worn panel button bounces for up to 15 milliseconds and you want 20 milliseconds of margin with R = 10 kΩ, Vcc = 5 V and Vth = 3 V. Rearranging gives C = t ÷ (R × ln(2.5)) = 20 ÷ (10 × 0.916) = 2.18 µF.

The nearest standard value is 2.2 µF, which gives t = 10 × 2.2 × 0.916 = 20.16 ms. Check the capacitor tolerance too, because a 20 percent part can shift the delay by the same amount.

Software Switch Debouncing Algorithms

Firmware methods read the pin at regular intervals and accept a new state only after it has been stable for a set time. They work well because a microcontroller already has a timer, as with the delay produced by a 555 timer IC in older discrete designs.

Idle ReleasedPin reads high, no press
Maybe PressedFirst low sample, start timer
Confirmed PressedLow for full debounce time, report press
Maybe ReleasedFirst high sample, start timer
Back to IdleHigh for full time, report release

The simplest switch debouncing method waits 20 to 50 milliseconds after the first edge and reads the pin again. It is easy to write, but a blocking delay freezes the rest of the program, so it suits only small prototypes.

The counter or integrator method samples every 5 milliseconds and counts up while the pin is low and down while it is high. The output changes only when the count reaches its limit, so random spikes are averaged away.

The state machine above adds clear states for pressed and released, which makes long press and double click detection straightforward. It is a practical example of the sequential circuits idea written in code.

The shift register method shifts each new sample into a variable, for example 8 or 16 bits wide. A press is accepted only when the history shows a run of identical samples after the old state, which needs just one line of logic per button.

Do You Know?

For software switch debouncing, Ganssle recommends sampling in a periodic timer routine instead of attaching the raw switch to an interrupt pin. A bouncing contact on an interrupt can fire dozens of interrupts for a single press.

Choosing the Right Switch Debouncing Time

1
Measure Bounce
Capture real presses on an oscilloscope or logic analyser.
2
Add Margin
Choose about 1.5 to 2 times the worst bounce seen.
3
Check Feel
Keep the total delay below about 50 ms for buttons.
4
Pick Sampling
Sample at 1 to 5 ms and confirm several samples.
5
Test Ageing
Retest with worn, dirty or cold contacts.

Encoder and counting inputs need a shorter window, since a real pulse must never be filtered out. Keypads that feed simple logic gates or counters often use 10 to 20 milliseconds.

Myth: Every button needs a capacitor across it.
Fact: Software switch debouncing alone is enough for many microcontroller inputs and saves parts.
Myth: Longer debounce time is always safer.
Fact: Too long a window misses fast presses and makes the panel feel slow.
Myth: Only cheap switches bounce.
Fact: Ganssle found bounce in every switch type he tested, including quality ones.
Myth: An RC filter alone is enough on any gate.
Fact: Without hysteresis, a slow edge can make a normal gate oscillate.

PLC Input Filters and Industrial Contacts

PLC digital input modules include their own switch debouncing filter, usually adjustable in the hardware configuration. Siemens S7 1200 inputs, for example, offer selectable filter times with a default of 6.4 ms.

Raise the filter when field contacts are old or cables pick up interference, but never above the shortest real pulse, such as an encoder or proximity count. Similar noise issues on analog channels are covered in PLC analog input problems and solutions.

Quick Tip

When a PLC counter shows double counts from a mechanical switch, first increase the module input filter. Only move to ladder logic timers if the module filter range is too short.

Advantages of Good Switch Debouncing
  • One press gives exactly one event.
  • Protects counters, menus and interlocks.
  • Software options add no hardware cost.
  • Hardware options free the CPU and suit interrupts.
Limitations and Trade Offs
  • Adds a small delay to every input.
  • Wrong time constant can hide real pulses.
  • RC parts drift with temperature and age.
  • SR latch needs a changeover switch.
Advertisement

Where Switch Debouncing Is Needed

Keypads and Panels
Membrane and tactile keys on instruments and HMIs.
Limit and Position Switches
Machine end stops feeding PLCs and controllers.
Relay Contacts
Auxiliary contacts read as status inputs.
Rotary Encoders
Mechanical encoders in knobs and menus.
Emergency Stops
Status contacts monitored by safety logic.

For safety devices, see how emergency stop push buttons use dual channels, where the logic also checks that both contacts change within a set time.

Troubleshooting Switch Debouncing Problems

  • Capture a press on an oscilloscope with single shot trigger.
  • Confirm the gate input really has Schmitt hysteresis.
  • Check that the pull up resistor value matches the design.
  • Verify capacitor value and tolerance on the board.
  • Look for noise coupled from motors or long cables.
  • Confirm the firmware switch debouncing sample rate and count.
  • Retest after cleaning or replacing worn contacts.

Noise that arrives on the wiring is not bounce, yet it produces the same false counts. Grounding and filtering advice is covered in noise reduction techniques for digital ICs, and a comparator circuit with hysteresis can clean very slow signals.

MAX6816 Switch Debouncer Datasheet

PDF
MAX6816, MAX6817, MAX6818 CMOS Switch Debouncers Datasheet
Analog Devices datasheet, ESD protected debouncer ICs

Contact Bounce Demonstration Video

Switch Debouncing FAQ

What is switch debouncing?

It is the removal of the rapid on and off transitions that a mechanical contact makes while opening or closing. The logic then sees only one clean change of state for every press.

Without it, a single press can register several times on a fast input. Counters jump, menus skip and interlock logic can react to signals that never really happened.

How long does a switch bounce?

Jack Ganssle measured an average of about 1.6 milliseconds and a maximum near 6.2 milliseconds for most of the switches he tested. A few poor switches bounced far longer, with one reaching 157 milliseconds.

Designers therefore plan for a window of roughly 1 to 20 milliseconds. Worn, dirty or cold contacts usually bounce longer than new, clean ones.

Why use a Schmitt trigger after an RC filter?

The RC network turns sharp rebounds into a slow rising or falling voltage. A normal logic gate can oscillate while such a slow edge crosses its single switching threshold.

A Schmitt input has two thresholds separated by a band of hysteresis. It switches once, ignores small ripple and delivers a clean digital edge to the rest of the circuit.

How does the SR latch method work?

A changeover switch drives the set and reset inputs of a latch built from two cross coupled NAND gates. The first touch on one side flips the latch immediately.

Rebounds on that same side cannot flip it back, since the moving contact never reaches the other side while bouncing. The method gives an instant and perfect edge with no delay.

Which software switch debouncing method is best?

The counter or integrator method suits most firmware because it is simple and resists noise well. It samples every few milliseconds and changes state only after steady readings.

A state machine is better when you need long press or double click features. The shift register method is the most compact choice when a board has many buttons to scan.

Do PLC inputs need extra switch debouncing?

Most PLC digital input modules already include an adjustable input filter in the hardware configuration. Siemens S7 1200 modules, for example, offer a default filter time of 6.4 milliseconds.

Increase the filter if old field contacts cause double counts in the program. Keep it shorter than any real pulse, such as an encoder or proximity sensor count.

How do I choose the RC values?

Pick a target time about 1.5 to 2 times the worst bounce you measured. Then use the charging formula with the Schmitt upper threshold to find the capacitor value.

For example, 10 kΩ and 1 µF give about 9.2 milliseconds at 5 volts with a 3 volt threshold. Check component tolerance and temperature drift before finalising the design.

Advertisement

Related Articles

External References

What We Learn Today

  • Mechanical contacts usually bounce for about 1 to 20 ms, and Ganssle measured an average near 1.6 ms across most of the switches he tested.
  • Switch debouncing can use an RC network with a Schmitt trigger, an SR latch, a dedicated IC or firmware counters and state machines.
  • In hardware switch debouncing, the RC release delay is R times C times ln(Vcc ÷ (Vcc minus Vth)), so 10 kΩ and 1 µF give 9.16 ms.
I hope you like above blog. There is no cost associated in sharing the article in your social media. Thanks for reading!! Happy Learning!!

Leave a Reply

Your email address will not be published. Required fields are marked *