SWITCH DEBOUNCING

Switches are a very important part of the machine interface. Pressing horn in your car or hitting the call button on your cell phone, we talk to various machines through switches. There are varieties of switches we press, rotate or touch daily.

A radio jumps 2 or 3 channels ahead when you press the tuning button only once. A TV does not respond unless you press the remote button very hard.  A counter in the mall door advances several digits when only one person is passed through the door. We all know these are classic problems with bad switch debouncing. Novice embedded designers often face issues in switch debounce design. This article is a small introduction to the switch debouncing techniques.

Switches and Mechanical Contact Bouncing

Interfacing a switch with a microcontroller looks very simple thing, but a lot of things happen behind the scene when a user presses a button. Understanding the behavior of switches helps in designing a good debouncing policy.

When the contacts of a mechanical switch are pressed, they rebound several times with different frequencies and then settle down to a fixed state. The rebound of mechanical contact is known as bounce. We have a variety of switches; pushbuttons, SPDT, DPDT, latch-able buttons, rotary switches, joysticks, etc. Again these switches come in different sizes and different current ratings. Because of such variations in switches, the bounce is also different in each type of switches. People press buttons in different ways. Some people press buttons very quickly, some push it very slowly, some users do it by hand, some use different objects to reach the switches. So designing the debouncing solution could be tricky sometimes.

What type of switch you choose mainly depends on the application needs. Once you know the bounce timing stats of a particular switch, you can easily design a debouncing policy. The first crucial step is to find Maximum bounce time and that can be easily done by observing the voltage levels at the switch contacts using an oscilloscope. Operate switch with different techniques, slow, fast, with soft-touch, with hard touch, release gently, release fast. Try different combinations of these techniques and collect the bounce statistics. With these statistics, you can easily arrive at the Maximum bounce time for your switches. The switch can have different bounce times when it is closed and when it is opened.

How to Get rid of Switch Bounce

There are some proven techniques used to remove the effect of switch bounce in digital applications. You can use hardware or software debouncing methods. Hardware debouncing consists of simple hardware filters, digital debouncing circuits and dedicated ICs. The software debouncing method utilizes different algorithms, some are microcontroller platform dependant using specific interrupts, some use counters and some use simple delays before re-sampling the inputs.

Hardware Solutions

Simple RC Filter

Single throw buttons, tactile pushbuttons, and board mounted plates are the most common types of switches used in small appliances. Using a simple RC filter circuit is a lot cheaper solution than any other hardware debouncing technique. Low pass RC filter removes high-frequency changes in the switch bounce.

When the switch is open the voltage across capacitor ‘C’ rises at a rate determined by values of ‘R1’, ‘R2’ and ‘C’. Bouncing of the switch slows down the charging rate of capacitor ‘C’. In time the capacitor charge and the output of the NOT gate (Schmitt trigger) give logical zero output.

When the switch is closed the capacitor starts discharging via ‘R2’. Till the time the capacitor is discharged to a significant level, NOT gate sees logical one at its input. Once the switch settled in a stable closed position after bouncing the capacitor ‘C’ discharges completely and reach level zero. And the NOT gate gives logical one output.

Consider the discharging cycle of capacitor ‘C’,

Where

Vc= Voltage across capacitor ‘C’ at time t

V0= initial voltage across the capacitor ‘C’

t = time in seconds

Switch Debouncing using low pass RC filter

To calculate ‘R2‘, select ‘Vc‘ such that it is greater than the switching voltage of the NOT gate in the above circuit.

So the equation for ‘R2‘ will become

Now consider the charging cycle of capacitor ‘C’.

Let R = R1 + R2 then

With this equation, ‘R’ comes to be

Vf is the final voltage of the charged capacitor. It could be 5V or 3.3V or any other value based on your digital circuit. To calculate ‘R’, select ‘Vc‘ that the less than the lower switching limit for high going signal for NOT gate.

The values of ‘R1’ and ‘R2’ can be calculated by the above formulas.

If the value of ‘R’ is less than ‘R2’, then use diode parallel to the ‘R2’ as shown in the diagram below so that you can eliminate ‘R2’ from the charging path.

Switch Debouncing using RC Filter and Diode

SR Flip Flop Latch

SR flip flop latch is a very good debouncer for switches with the double throw. It consists of 2 NAND gates connected as shown in the below diagram.

Switch Debouncing using SR flip Flop

When the switch is in moved to position 1, the output of the latch will be one. If the switch is moved to position 2, the output of the latch will be zero. When the switch is moved between contacts, it bounces a little bit on one contact but does not touch the other contact. So the stable bouncing free output is given by the SR latch.

Switch Debouncing ICs

Since the cheaper alternatives available to fix the bouncing problem, specialized switch debouncing ICs are not very popular. Maxim Integrated Products Inc. is the one leading manufacturer of debouncing ICS. They have different variants with single, dual and octal switch debouncers in one package. These debouncing ICs provide inbuilt ESD protection. More information can be found in the application note 287.

Maxim Switch Debouncing IC MA6816

Debouncing in Software

The basic idea in software debouncing is to sample the input at regular intervals and filter out the glitches. The switch debouncing can be implemented in a number of ways. If you are not much concerned about real-time performance just check the switch input state, then wait a bit and recheck again. If the same state is detected then that can be considered as a stable state of the switch. Another way is to use a counter and check for how long the switch is low or high and if it is consistently low or high for a predefined time then it is considered a stable state of the switch. Sometimes EMI can produce false signals on the microcontroller pin inputs. Software debouncing also filters such noise.

Some care must be taken while designing a debouncing policy. Avoid sampling of switch input at a rate synchronous to other events that can create EMI, e.g. motor direction change frequency or 50Hz/60Hz frequency. Do not connect un-debounced switches directly to the interrupt pins of microcontrollers. Some microcontrollers provide special interrupt pins that have an inbuilt RC filter. Such pins can be used as switch interrupts. Refer to this article to find interrupt handling in 8051 microcontrollers.

Hardware debouncing involves some cost of components and also needs some space on the circuit board. So software debouncing is very popular and different algorithms are being used for the debouncing.