Switch to Normal Style Sheet
Site Icon The Lab Book Pages Andrew Greensted (Modified: 17 June 2008)
Electronics > Switch Debouncing

Switch Debouncing

Switch debouncing is one of those things you generally have to live with when playing with switches and digital circuits. If you want to input a manual switch signal into a digital circuit you'll need to debounce the signal so a single press doesn't appear like multiple presses.

There are already a considerbale number of pages on this topic. So here's another!

Divider Bar Go to page top

• What is Switch Bounce?

The left-hand image below shows a simple push switch with a pull-up resistor. The right hand image shows the trace at the output terminal, Vout, when the switch is pressed. As can be seen, pressing the switch does not provide a clean edge. If this signal was used as an input to a digital counter, for example, you'd get multiple counts rather than the expected single count.

Note that the same can also occur on the release fo a switch.

The problem is that the contacts within the switch don't make contact cleanly, but actually slightly 'bounce'. The bounce is quite slow, so you can recreate the trace, and the problem quite easily.

Simple switch pull-up circuit Switch Bounce

In the switch waveform the bouncing lasts for about 150us.

Divider Bar Go to page top

• A Switch Debouncer Circuit

There are many different approaches to cleaning up switch bounce. Below is a debouncing circuit. The basic idea is to use a capacitor to filter out any quick changes in the switch signal.

Switch Bounce

The circuit's operation can be explained by looking at the equivalent circuits formed in the two switch states, open and closed.

Switch Bounce

Starting with the switch open.

  • The capacitor C1 will charge via R1 and D1.
  • In time, C1 will charge and Vb will reach within 0.7V of Vcc.
  • Therfore the output of the inverting schmitt tigger will be a logic 0.

Now close the switch

  • The capacitor will discharge via R2.
  • In time, C1 will discharge and Vb will reach 0V.
  • Therfore the output of the inverting schmitt tigger will be a logic 1.

But what about bounce conditions? If bounce occurs and there are short periods of switch closure or opening, the capacitor will stop the voltage at Vb immediately reaching Vcc or GND. Although, bouncing will cause slight charging and discharing of the capcitor, the hysteresis of the schmitt trigger input will stop the output from switching.

What about the diode? Well the resistor R2 is required as a discharge path for the capacitor, without it, C1 will be shorted when the switch is closed. Without the diode, D1, both R1 and R2 would form the capacitor charge path when the switch is open. The combination of R1 and R2 would increase the capacitor charge time, slowing down the circuit. So, can't you just make R1 smaller? Ideally no, when the switch is closed, R1 is connected across the supply rails, so too small a resistor value would lead to unwanted wasted current.

Divider Bar Go to page top

• Software Debounce

Debouncing a switch in software is very simple. The basic idea is to sample the switch signal at a regular interval and filter out any glitches. There are a couple of approaches to achieving this listed below. Both approaches assume a swtch circuit like that shown in the explanation of switch bounce: a simple push switch with a pull-up resistor.

Approach 1

The first approach uses a counter to time how long the switch signal has been low. If the signal has been low continuously for a set amount of time, then it is considered pressed and stable.

 1 Setup a counter variable, initialise to zero.
 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms.

 3 On a sample event:
 4   if switch signal is high then
 5     Reset the counter varaible to zero
 6     Set internal switch state to released
 7   else
 8     Increment the counter variable to a maximum of 10
 9   end if
10   if counter=10 then
11     Set internal switch state to pressed
12   end if

Approach 2

The second approach is similar to the first, but uses a shift register instead of a counter. The algorithm assumes an unsigned 8bit register value, such as that found it 8-bit microcontrollers.

 1 Setup a variable to act as a shift register, initialise it to xFF.
 2 Setup a regular sampling event, perhaps using a timer. Use a period of about 1ms.

 3 On a sample event:
 4   Shift the variable towards the most significant bit
 5   Set the least significant bit to the current switch value
 6   if shift register val=0 then
 7     Set internal switch state to pressed
 8   else
 9     Set internal switch state to released
10   end if
Divider Bar Go to page top

• Digital Switch Debouncing

Digital debouncing is achieved in basically the same way as the software approaches shown above. However, they are implemented in dedicated hardware.

Divider Bar Go to page top

• Switch Debouncing ICs

For such a common problem, you'd think there would be a plethora of ICs for performing switch debounce. However, there don't seem to be that many. Below is a list of those I've found so far. Unfortunately, one is not easily aquired, the other is expensive.

MAX6816 Number: MAX6816, MAX6817, MAX6818
Manufacterur: Maxim-Dallas
Web Page: http://www.maxim-ic.com/quick_view2.cfm/qv_pk/1896
Notes: Apart from Maxim's sample service, I've not found anyone who sells these parts in small quantities.
MC14490 Number: MC14490
Manufacterur: ON Seminconductor
Web Page: http://www.onsemi.com/PowerSolutions/product.do?id=MC14490
Notes: Farnell sell these, but they seem very over priced.
Divider Bar Go to page top