Switch Debouncing

Switch Debouncing

Contents

Haven’t you all noticed about the fact that sometimes while pressing a remote of a TV once, the channel shifts by 2 levels or more. Some may have even seen when a tuning button of an old FM radio has pressed the channel advances more than the desired. What is the reason behind it?  Have anyone thought about it?  The answer lies within the switches. The problem is ‘Switch Bounce‘.

Switch Bounce

Switch bounce or contact bounce or even called as chatter is a common problem associated with mechanical switches and relays. Switch, relay contacts are made up of spring metals which are forced to contact each other by an actuator. While they collide each other there is  a possibility of rebounding for some time before they make a stable contact. It’s similar to case of dropping a bouncing ball or basketball. The ball keeps on bouncing till it comes to rest. This case can be taken analogous to on/off of a switch. When the ball touches the ground analogous to on and when it rises to certain level analogous to off of a switch. As a result of this effect there will be on/off transitions generated as the contacts rapidly open and close.

  • In short Switch bounce is a non ideal behavior which generates multiple transitions for a single user input.
Switch Bouncing in Pull Down Connection
Switch Bouncing in Pull Down Connection
Switch Bouncing in Pull Up Connection
Switch Bouncing in Pull Up Connection

This effect is not taken as important while doing power circuits or electrical circuits but creates a major problem while we deal with logic circuits. For example if you made a counter and it increments not by one but by any other value when you press the input switch and what if the units of increment is not uniform?

So here we have to remove bounces. And the method to get rid of such bounces is called Switch Debouncing.


Switch Debouncing

It can be implemented in 4 ways,

  1. Hardware Debouncing
  2. R-C Debouncing
  3. Software Debouncing
  4. Using Debouncing IC’s

1. Hardware  Debouncing

Hardware debouncing technique uses an S-R latch to avoid bounces in the circuit along with the pull up resistors. S-R circuit is most effective of all debouncing approaches

The figure below is a simple debouncing circuit which is often used.

Hardware Switch Debouncing
Hardware Switch Debouncing

The circuit uses two cross coupled NAND gates which form an S-R latch, A SPDT (Single Pole Double Throw) switch, two pull up resistors. The resistor generates a logic ‘one’ for the gates, Switch pulls one of the inputs to ground.

If  the switch is in position as shown in figure  the output of the upper gate is ‘1’ irrespective of the input of the other gate and the one created by the bottom pull up resistor which drives the lower NAND gate to zero which in return races back to the other gate. If the switch moves back and forth between the contacts and is suspended for a while in neither region between the terminals,the latch maintains its state because ‘0’ from the bottom NAND gate is fed back. The switch may move between the contacts but the latch’s output ensures it never bangs back and thus switch is bounce free.

2. R-C Debouncing

The S-R circuit is common but the bulkiness of the circuit causes it to be used rarely also SPDT switches are costlier than SPST (Single Pole Single Throw) switch. Another method of debouncing is to use a R-C circuit. The basic idea behind such circuit is to use a capacitor to filter out quick changes in the switch signal.

RC Switch Debouncing
RC Switch Debouncing

The basic R-C circuit used for debouncing is shown above. The circuit uses two Resistors, Capacitor, Schmidt trigger hex inverter (eg : 7414) , SPST switch.

  • If  the switch is open, the voltage across capacitor which is initially zero now charges to Vcc through the R1 & R2. The voltage at Vin is high  hence the output of the inverting Schmitt trigger is low ( logic 0 )
  • If the switch is closed the capacitor discharges to zero hence the voltage at Vin is ‘0’  and output of the inverting Schmidt trigger is high ( logic 1 )

During the bouncing condition the capacitor will stop the voltage at Vin when it reaches either Vcc or Gnd.

There will be question in every one’s mind that why a standard inverter is not used ??

We can’t use the standard inverter gate here. TTL defines a zero input when the applied voltage is in between 0 and 0.8. The output becomes unpredictable in some situations.So we use a Scmitt trigger hex inverter. The output remains stable even if the inputs vary or dither also it prevents the output from switching because of its hysteresis property.

  • To increase the circuit speed we can connect a diode across R2 thereby reducing the charging time of the capacitor as it charges only through R1 when switch is off.
RC Switch Debouncer with Diode
RC Switch Debouncer with Diode
RC Switch Debouncer with Diode Working
RC Switch Debouncer with Diode Working

 

3. Software Debouncing

Software debouncing is another method to get rid of bounces in the circuit. The basic principle is to sample the switch signals and filter out glitches if any. There are two methods for software debouncing.

  • Using counters
  • Using shift registers

The algorithm for software debouncing is shown.

Counter Method

  1. Initially set up a count value to zero
  2. Using a timer set up a sampling event with a period (say 1 ms)
  3. On the sample event:
  4. If  switch signal high then
  5. Set count=0
  6. Set internal switch state released
  7. else
  8. Increment count to a max of 10
  9. end if
  10. if count =10 then
  11. Set internal switch state to pressed
  12. end if

Shift Register Method

Similar to that of counter method.Only difference is that it uses shift register. The algorithm assumes  unsigned 8 bit reg value usually  found in microcontrollers

  1. Initially set up a shift register variable to xFF
  2. Using a timer set up a sampling event with a period (say 1 ms)
  3. On the sample event:
  4. Shift the variable towards MSB
  5. Set LSB to current switch value
  6. if shift register value =0 then
  7. Set internal switch state to pressed
  8. else
  9. Set internal switch state to released
  10. end if

4. Switch Debouncing IC

Switch debouncing can also be done by some specialized IC. They are only a few of them and rarely used.

  • MAX6816, MAX 6817, MAX 6818
  • MC 14490
  • LogiSwitch LS118
Switch Debouncing using IC MAX6816
Switch Debouncing using IC MAX6816
Switch Debouncing using LS118
Switch Debouncing using LS118

Share this post

  • That is enough for most cases.. .If you don’t put that delay.. The switch may get read too many times as the microcontroller program is running very fast.

  • HI ANKIT . sir great tutorial . but you use delay_ms(100) in your push button tutorial ,

    is it the best way to use switch debounce?

  • sir great tutorial . but you use delay_ms(100) in your push button tutorial ,
    is it the best way to use switch debounce?

  • Very Good Tutorial. I knew only 2 methods earlier but now i will try other method also in my circuits.
    Thanks , doing great job.


  • >