
Ultimate Guide to Switch Debounce - baybal2
https://www.eejournal.com/article/ultimate-guide-to-switch-debounce-part-1/
======
akersten
Debouncing is a useful concept in software design too.

Many UI elements are all too eager to emit hundreds of state update events
(like a slider) as the user is finalizing the input they want - but you
wouldn't want to send 100 AJAX requests to the webserver while the user's
fiddling around. So resetting a 200ms timer before sending the web request is
a great analog to the process of physical debouncing.

~~~
downerending
That's useful as an implementation detail, but even more important is the
_user experience_ of failing to debounce correctly. If I press a button (etc)
twice within 100ms, that generally does _not_ mean I want this to be seen as
two presses. Rather, my finger slipped, maybe I have minor neurological
problems, I'm drinking, the software itself glitched, whatever. For the user's
sake, user interfaces should debounce themselves.

It's all rather striking to me, as the concept of debouncing was one of the
first things I learned while reading microcontroller code as a child. (No, I'm
not kidding--that was the only book on computers I could find growing up in
BFE.)

That we _still_ haven't generally solved this in 2020 is appalling.

(There are lots of variations. There was an article in _Software: Practice and
Experience_ in the 90s (?) lamenting the UI race in most X interfaces (and
probably all other GUIs). Briefly, lots of interfaces can pop up a dialog or
whatever while the user is pressing a button underneath. The dialog often gets
the press, resulting in the software executing some action the user did not
even see, much less intend.)

~~~
mrob
If a computer is going to disobey my instructions it should be extremely sure
it's doing the right thing. 100ms is too long a threshold to make that
assumption. 100ms repetition interval is only 10Hz, which is easily
accomplished in short bursts by anybody with moderate skill in button mashing.
I'd set it no higher than 50ms (20Hz), which is very difficult even in short
bursts.

~~~
downerending
Yeah, well, wait until you're older. Or sicker.

~~~
renewiltord
When I'm older or sicker, I'll use a thing that works for me when I'm older or
sicker. Otherwise it's like turning on the screen reader for everyone because
blind people can't operate computers otherwise.

~~~
downerending
That's great for people that are technically inclined. Spend some time with
senior citizens and the disabled and you might develop a bit more compassion.

------
lmilcin
The simplest strategy I have been using successfully since forever is one that
I found on some device I have repaired.

For most devices it is enough to just probe the switch periodically.

Nobody is ever going to notice couple of milliseconds of lag. Other debouncing
strategies also introduce latency as they need to observe the signal for some
time.

It is important to set the check period to be longer than settling time.

Assume the switch is in stable state before the check, then there is check and
then it is in another stable state. It does not matter which state the check
finds as long as the state changes from one stable to another stable. Even if
the switch is bouncing at the moment of the check it will be either
interpreted as previous state before the switch or the next state after
switch.

If you have delicate switch with good (for example gold plated) contacts, to
extend the life of the switch it is then possible to only apply potential for
a brief period of the check which means that during actual bounce the contact
is very unlikely to see any sparks, extending the life of the switch. This
does not work for some switches that rely on spark to remove oxides to get
good contact.

~~~
alpaca128
> For most devices it is enough to just probe the switch periodically.

No, no, no, please don't. Almost all appliances I use ignore an estimated 60%
of my usual button presses because they're polling in intervals just long
enough to not recognise anything, and I absolutely hate it.

I always have to hold it down for a bit to make sure my input isn't just
ignored. My laptop's power switch has this issue just like the washing
machine, the dish washer and pretty much everything else that's not a PC
keyboard, and it's one of the most infuriating UI issues I encounter every
day, beside inactive text input fields with blinking cursors or selected text.

~~~
jimktrains2
Power buttons, like on a PC or washing machine, often require being held as a
means of preventing accidental activation.

~~~
alpaca128
I don't mean just power buttons. When I said almost every button not on a PC
keyboard I meant it. On the washing machine every single button has to be held
almost half a second or it won't do anything. It's super frustrating. But of
course it's possible to mess this up the other way too - one of my devices
reacts quick to button presses, but you have to wait about half a second until
it will react to the next press.

------
Animats
_Well, according to The Art of Electronics by Horowitz & Hill (Page 506 of the
Second Edition), “When the switch is closed, the two contacts actually
separate and reconnect, typically 10 to 100 times over a period of about 1
msec.” This just goes to show you how undefined this field is, because this
book is regarded as the “Electronics Bible” by many engineers, but their value
of 1 msec (which is the one I was told as a young engineer) is less than
Jack’s average findings, let alone his maximum findings._

In the Third Edition, on page 802, Horowitz and Hill write "For physically
large switches, the bounce can be as long as 50ms." That first quote is from a
section on clocked debouncing, which can be made insensitive to contact bounce
by digital means.

I like the data sheet for the automotive contact debouncing IC.[1] That's very
cute. In dirty environments, you need a minimum current and voltage, maybe
10mA, 12V, to punch through dirt and water on the contacts. That IC does that,
but it only turns on the power intermittently, so you don't drain the battery
polling the dashboard switches.

I've hit that with the antique Teletype machines. They were intended to run
with 120VDC on the keyboard contacts, and you need about 24V or so to get
reliable punch-through.

[1]
[https://www.ti.com/lit/ds/symlink/tic12400-q1.pdf](https://www.ti.com/lit/ds/symlink/tic12400-q1.pdf)

~~~
Renaud
The Maxim's MAX6816/MAX6817/MAX6818[1] are pretty good as well in that you
don't need RC filters for each input, so less components needed to debounce
inputs.

[1]
[https://datasheets.maximintegrated.com/en/ds/MAX6816-MAX6818...](https://datasheets.maximintegrated.com/en/ds/MAX6816-MAX6818.pdf)

------
roland35
In my experience with a variety of switches and microcontrollers this actually
isn't too big of a deal as it used to be. It is worth testing out, but I think
it's worth testing before investing a lot of hardware/software development on
it. Adding some resistance or a simple RC low-pass filter can help, and also
many microcontrollers come with basic anti-bounce inputs.

As other posters said another easy effective strategy is to just ignore inputs
after the initial transition. You can do this and still pick up long presses
too if you need to!

------
migueloller
This is definitely a problem with any circuit that is complex enough. It was
nice to see Ben Eater explain it and solve it in multiple ways, once using a
555 timer and a capacitor[1] and the other time using just the SR latch of the
555[2].

[1]
[https://www.youtube.com/watch?v=81BgFhm2vz8](https://www.youtube.com/watch?v=81BgFhm2vz8)

[2]
[https://www.youtube.com/watch?v=WCwJNnx36Rk](https://www.youtube.com/watch?v=WCwJNnx36Rk)

------
ivanhoe
Must admit I've always though this is an oscillation created by the
inductivity and capacitance of the circuit, never though of it as an actual
mechanical switch bouncing off the other contact. Shouldn't a spark form and
short-circuit the mechanical contacts even if they bounce around a little?

~~~
pjc50
Arcing is only present if there's a significant amount of current flowing,
which there isn't in switches used for user input.

------
REPLicated2
When I started dabbling in electronics recently, I ran into switch bouncing
apparently as I used a push button to generate the "clock" input of a CD4017
decade counter and it showed rather erratic output behavior.

Adding a capacitor fixed it. Which reminds me that I should really invest in
an oscilloscope to validate my (not so) educated guesses.

Great resource to read up on this topic, thanks.

------
Exuma
He never explains why this actually happens. As a non-engineer, I'm curious
why it's not immediate...

~~~
jpm_sd
Nothing is perfectly stiff. Nothing moves perfectly smoothly. Every mechanical
part has some amount of "springiness" and will vibrate when struck, pushed,
flexed, etc. So a switch contact literally "bounces" like a hammer on a piano
string as it is closed. His analogy of a ping-pong ball on a desk isn't great,
but it's not exactly wrong, either.

As for "immediate", the switch closure feels immediate to human perception
(supposedly limited to about 1/20 second, for touch). But to modern
electronics, nanoseconds are easily countable; and switch bounce can be
measured in microseconds, or even milliseconds for a big, heavy
electromechanical component.

Here are some better articles.

[https://www.allaboutcircuits.com/technical-
articles/switch-b...](https://www.allaboutcircuits.com/technical-
articles/switch-bounce-how-to-deal-with-it/)

[https://www.maximintegrated.com/en/design/technical-
document...](https://www.maximintegrated.com/en/design/technical-
documents/app-notes/2/287.html)

------
crshults
When I saw the title I thought it would be the old Jack Ganssle article which
is also worth a look if you're interested in this topic:
[http://www.ganssle.com/debouncing.htm](http://www.ganssle.com/debouncing.htm)

------
paypalcust83
Is it really that hard to just add a simple RC circuit?

No polling required.

And here's a component values calculator: [https://protological.com/debounce-
calaculator](https://protological.com/debounce-calaculator)

~~~
raverbashing
Well.

If you see the circuit this calaculator (sic) proposes, you'll see it won't
debounce fully the closing the switch (and you'll have to invert the logic,
which in principle it's ok).

Plus adding one component to the circuit (you'll most likely have the resistor
anyway).

It might work for your case but you'll most likely need a software solution as
well, depends on your switch and the logical gate input impedance.

------
ur-whale
aka : what functional transform can I apply to an unpredictable function
Signal(Time) to decide if it models a zero or a one.

~~~
bentcorner
Isn't the answer some form of hysteresis? Whether you implement it in hardware
or software it's up to you.

------
xellisx
When given the chance, I like using SPDT switches and an SR Latch to deal with
bouncing.

~~~
baybal2
One problem, finding SPDT momentary switches is haaard, near nothing there
that is small, and smt

~~~
xellisx
Yeah - that is one problem. I would love to have MX switches in SPDT format. I
have find these a while back ago:
[https://www.aliexpress.com/item/32430367243.html](https://www.aliexpress.com/item/32430367243.html)

------
wyck
I instantly closed this website because it loaded slow and then hit me with 2
pop-ups and a location request.

~~~
throwlaplace
I instantly skipped reading your comment when I saw "I instantly..."

