8

Suppose that an input on a microcontroller has a pullup resistor to 5V and a switch to ground (normally open) connected to it. I know that a switch should be debounced (in my case, in software) when closed, but is debouncing when a switch is opened necessary?

Nate
  • 4,496
  • 11
  • 56
  • 75

4 Answers4

11

Yes, debouncing in both directions is necessary if you want a guaranteed single edge each time the switch changes state.

Fortunately, you don't need any additional hardware for debouncing if the switch is connected to a microcontroller. If the micro has a internal pullup, which many do, you need nothing more externally than the switch between the micro pin and ground.

I find 50 ms is a good debounce time. Most switches bounce for well less than that, but a few can be nearly that long. But, 50 ms will still feel instantaneous to a human user, so you might as well be extra reliable. The only difference is the number you count to in the firmware, so no extra cost there. I usually have a 1 ms periodic interrupt for other reasons anyway, so if the switch is in the same state 50 times in a row in that interrupt, then you declare it debounced to the new state.

Details on debounce logic in response to comment:

Generally you will have a global bit that indicates the official debounced state of the switch. This is what any logic that needs to know which way the switch is set uses. The only additional state you need is a counter, usually a single byte, in the interrupt routine. Let's say the interrupt is every 1 ms and the debounce time is 50 ms. For each interrupt:

If instananeous state matches debounced state:
  Reset counter to 50
  Done

If instantaneous state differs from debounced state:
  Decrement counter
  If counter reaches 0:
    Declare the new debounced state
    Reset counter to 50
  Done
Olin Lathrop
  • 310,974
  • 36
  • 428
  • 915
  • There was a very good article on www.embedded.com a while back. The author grabbed a bunch of switches and put them between a power supply and a digital storage scope input, to look at the bounce characteristics. The final conclusion is that you can't pick a "one size fits all" debounce time: you have to characterize each particular switch and see how it bounces. – John R. Strohm Oct 26 '12 at 21:00
  • @John: can you give the link? I don't get why a worst-case maximum bounce time assumption can't be used for all switches, provided that it is short enough to satisfy the user. IME the 50ms Olin mentions satisfies both criteria. – Wouter van Ooijen Oct 26 '12 at 21:14
  • Thanks Olin. So how do you keep track of what state the switch has been in the past 50 interrupts? I would increment a variable if the switch is in the same state as the last interrupt, clear it if the state has changed, and consider the switch debounced if it reaches 50. Is that an efficient way to do it? – Nate Oct 26 '12 at 22:35
  • 1
    This may not be the article mentioned above, but it's got a lot of interesting info in it: http://www.ganssle.com/debouncing.htm Anyone dealing with debouncing should read it. – MickeyfAgain_BeforeExitOfSO Oct 27 '12 at 03:25
  • 1
    @Nate: See addition to answer. – Olin Lathrop Oct 27 '12 at 21:51
  • @mickeyf: Note that Ganssle came to the same conclusion I did in that 50 ms delay still feels instantaneous to a human. I came up with that by testing various delays many years ago. It's good to see humans haven't changed much in the 30 years since I did the experiment. It seems switches haven't changed much either. He also found that most switches bounced for well less than 50 ms, but a few were much longer than the pack. In short, the article basically confirms what I said. – Olin Lathrop Oct 27 '12 at 22:32
2

Yeah I would use one, the mechanism is basically the same. As you are breaking the contact you will have points in time where the switch is making good electrical contact as well as not making good electrical contact. The same as when you are making the contact.

Some Hardware Guy
  • 15,815
  • 1
  • 31
  • 44
1

In general, yes. Mechanical switches can produce electrical noise (bouncing) on both close and open.

Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
1

Generally, yes, but it always depends on what you're doing with the digital input. I've had a few systems that absolutely had no need to be debounced in either direction.

If you're driving an interrupt, though, most likely you should debounce.

Scott Seidman
  • 29,274
  • 4
  • 44
  • 109