Author Topic: Switch debouncing in hardware  (Read 5059 times)

0 Members and 1 Guest are viewing this topic.

Offline MaxlorTopic starter

  • Frequent Contributor
  • **
  • Posts: 565
  • Country: ch
Switch debouncing in hardware
« on: November 13, 2013, 06:36:22 pm »
I've always done debouncing of switches in software, but recently I've started wondering how to do it in hardware. The accepted way seems to be to have a capacitor in series with the switch, and a pull-up resistor, like shown in figure 2.A here. Now here's the part that I don't understand: while this wonderfully debounces the switch release, I don't see how it does anything for pressing the switch. The voltage curves given indicate that the capacitor decharges slowly (same thing shown here), but it seems to me it's always decharged pretty much instantly. Oscilloscope measurements are confirming it.

So... am I missing something here? Maybe some implicit element in the circuit? I understand that I could add a resistor in series with the switch, but then I'd have a voltage divider and can't get the switch output down to GND anymore.

If I am right, and this does nothing for closing the switch, is there a circuit that does, yet still gives me unchanged VCC/GND voltage levels at the switch output?
 

Offline tszaboo

  • Super Contributor
  • ***
  • Posts: 7390
  • Country: nl
  • Current job: ATEX product design
Re: Switch debouncing in hardware
« Reply #1 on: November 13, 2013, 06:47:04 pm »
The accepted way seems to be to have a capacitor in series with the switch,
Parallel.

Why is it OK? Because software takes time to do it's stuff. If you have small glitches while you processing the button's press, you just dont check during that time if there was another press. At release, it is more important, because there you might re-trigger your circuit.
If you really want to debounce both ways, make a 1K-10K setup. It will not violate the voltage levels, and both will be denounced. Adding a schmitt trigger is usually a huge waste of resources.
 

Offline AG6QR

  • Frequent Contributor
  • **
  • Posts: 857
  • Country: us
    • AG6QR Blog
Re: Switch debouncing in hardware
« Reply #2 on: November 13, 2013, 07:12:21 pm »
Now here's the part that I don't understand: while this wonderfully debounces the switch release, I don't see how it does anything for pressing the switch. The voltage curves given indicate that the capacitor decharges slowly (same thing shown here), but it seems to me it's always decharged pretty much instantly. Oscilloscope measurements are confirming it.

The way pushbutton switches work, a closed pushbutton may temporarily spuriously go open for a brief time while the user intends to keep it pressed.  But an open switch which isn't being pressed will never go closed, not even for a brief instant.  As long as the contacts are apart, they are open.

You seem to be under the impression that a debounce circuit should wait for a while after switch closing before signalling a low output.  That would serve no purpose except to make the system slower to respond to button pushes.  The goal of debounce is just to ensure that one single transition is seen on the output.

A bounce at switch pressing involves at least contact, opening, contact, in rapid succession.  It might repeat many times.

As you point out, the switch does discharge the capacitor very quickly, limited only by the parasitic resistance and inductance in the switch and wiring.

During a bounce event, what that circuit does is prevent the very quick opening of the switch from bringing the output voltage all the way high.  That way, only one transition from high to low is seen by the microcontroller.

(Incidentally, good, responsive, software-based debounce can work the same way.  When a button closure is sensed, react to it in software immediately, but ignore any switch openings for a time period.  When an opening is sensed, wait for it to remain open for a time before reporting the open state to the software.)
« Last Edit: November 13, 2013, 08:38:04 pm by AG6QR »
 

Offline babysitter

  • Frequent Contributor
  • **
  • Posts: 893
  • Country: de
  • pushing silicon at work
Re: Switch debouncing in hardware
« Reply #3 on: November 13, 2013, 07:38:25 pm »
The one and only true hardware debouncing(tm) is done with a SPDT Switch, each side connected to a RS FLIPFLOP and the common contact connected to the appropriate stimulus for the FF. The RSFF will take the state the first touching defines. It is improbable that the bounce bounces bouncy between the NO and the NC contact.
I'm not a feature, I'm a bug! ARC DG3HDA
 

Offline jimmc

  • Frequent Contributor
  • **
  • Posts: 304
  • Country: gb
Re: Switch debouncing in hardware
« Reply #4 on: November 13, 2013, 09:30:55 pm »
AG6QR has covered why a single RC network works.
If you want to use an additional series resistor (it's kinder to the switch if the capacitor is large) then add it as shown in the attachment, this avoids having a potential divider.

As babysitter has said the surest debounce circuit is a SPDT switch and SR flipflop, but with a correctly chosen time constant the RC network /Schmitt trigger method is reliable and has the advantage that any spikes picked up on the switch leads are filtered (especially when using the two resistor circuit).

Jim
 

Offline MaxlorTopic starter

  • Frequent Contributor
  • **
  • Posts: 565
  • Country: ch
Re: Switch debouncing in hardware
« Reply #5 on: November 13, 2013, 10:36:16 pm »
Thank you for all your insights. I think I understand now the limitations and intentions involved.

I experimented a bit with a couple of different PCB mount switches I found laying around. The really cheap ones are pretty awful in terms of bounce and reliability of contact. I guess you get what you pay for ;)
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8275
Re: Switch debouncing in hardware
« Reply #6 on: November 14, 2013, 09:12:28 am »
I remember mercury-wetted contacts were used in some applications for non-bouncing switches.
 

Offline Moshly

  • Regular Contributor
  • *
  • Posts: 139
  • Country: au
  • What's wrong with this thing
Re: Switch debouncing in hardware
« Reply #7 on: November 14, 2013, 10:12:51 am »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf