If you’ve ever turned a rotary encoder or pushed a cursor button and had it skip a step or two, you’ve suffered directly from button bounce. My old car stereo and my current in-car GPS navigator both have this problem, and it drives me nuts. One button press should be one button press. How hard is that to get right?
In the last session of Embed with Elliot, we looked into exactly how hard it is to get right and concluded that it wasn’t actually all that bad, as long as you’re willing to throw some circuitry at the problem, or accept some sluggishness in software. But engineers cut corners on hardware designs, and parts age and get dirty. Making something as “simple” as a button work with ultra-fast microcontrollers ends up being non-trivial.
And unsurprisingly, for a problem this ubiquitous, there are a myriad of solutions. Some are good, some are bad, and others just have trade-offs. In this installment, we’re going to look at something special: a debouncer that uses minimal resources and is reasonably straightforward in its operation, yet which can debounce along with the best of ’em.
In short, I’ll introduce you to what I think is The Ultimate Debouncer(tm)! And if you don’t agree by the end of this article, I’ll give you your money back.
“Psst…hey buddy! Wanna see the sweetest little debouncing routine this side of Spokane? C’mon over here. Step right over those bit-shift operators, they don’t bite. Now look at this beauty right here: I call her The Ultimate Debouncer(tm)!”
Everybody who works with microcontrollers eventually runs into the issue of switch bounce or “chatter”, and nearly everyone has their own favorite solution. Some fix it in hardware, others fix it in software. Some hackers understand chatter, and others just cut-and-paste the classic routines. Some folks even try to ignore it, and they might even get lucky, but everyone’s luck runs out sometimes.
In the next two “Embed with Elliot” installments, I’ll look a little bit at bouncing, look into doing hardware debouncing both the simple way and the right way, and build up a basic software routine that demonstrates some of the principles and which works just fine, though it’s not optimized. We’ll be laying the groundwork.
In the next installment, I’ll let you in on my personal favorite debounce routine. It’s a minor tweak on a standard, but with some special sauce that’s worth spreading around. I’ll call it the Ultimate Debouncer(tm), but will it stand up to the scrutiny of the Hackaday commenteers? (How’s that for a cliffhanger?!?)
For now, though, let’s look into switch bounce and the standard ways to fix it in hardware and software.
Last month we asked you to send in your debounce code. You didn’t disappoint and it’s time to share the code received. There were some guideline for sending in code so if you don’t see yours here, it probably didn’t follow the rules, sorry. We also tried to weed out code that using delay loops for debounce. These tend to be a poor way to handle inputs because they monopolize the processor.
We wanted to add upvote/downvote buttons to each set of code to give some idea of a group consensus on code quality but there’s no good system available for multiple up/down vote widgets on one wordpress page. This results in a huge code dump for any one person to go through. If you’ve got any ideas on how to better organize this let us know: firstname.lastname@example.org.
We make no guarantees that this code is safe to use, or that it even works. Test it carefully before using for important tasks.
Join us after the break for a whirlwind of code examples.
If you’ve ever designed an embedded system with at least one button you’ve had to deal with button debouncing. This is also know as contact bounce, a phenomenon where a button press can be registered as multiple button presses if not handled correctly. One way to take care of this is with a hardware filter built from a resistor-capacitor setup, or by using a couple of NAND gates. We find that [Jack Ganssle] put together the most comprehensive and approachable look at contact bounce which you should read through if you want to learn more.
We’re interested in software solutions for debouncing buttons. This seems to be one of the most common forum questions but it can be hard to find answers in the form of reliable code examples. Do you have debounce code that you depend on in every application? Are you willing to share it with the world? We’d like to gather as many examples as possible and publish them in one-post-to-rule-them-all.
When we think of relays, we tend to think of those big mechanical things that make a satisfying ‘click’ when activated. As nice as they are for relay-based computers, there are times when you don’t want to deal with noise or the unreliability of moving parts. This is where solid-state relays (SSRs) are worth considering. They switch faster, silently, without bouncing or arcing, last longer, and don’t contain a big inductor.
An SSR consists of two or three standard components packed into a module (you can even build one yourself). The first component is an optocoupler which isolates your control circuit from the mains power that you are controlling. Second, a triac, silicon controlled rectifier, or MOSFET that switches the mains power using the output from the optocoupler. Finally, there is usually (but not always) a ‘zero-crossing detection circuit’. This causes the relay to wait until the current it is controlling reaches zero before shutting off. Most SSRs will similarly wait until the mains voltage crosses zero volts before switching on.
If a mechanical relay turns on or off near the peak voltage when supplying AC, there is a sudden drop or rise in current. If you have an inductive load such as an electric motor, this can cause a large transient voltage spike when you turn off the relay, as the magnetic field surrounding the inductive load collapses. Switching a relay during a peak in the mains voltage also causes an electric arc between the relay terminals, wearing them down and contributing to the mechanical failure of the relay.
A mouse with malfunctioning buttons can be a frustrating to deal with — and usually a short leap to percussive maintenance. Standard fixes may not always last due to inferior build quality of the components, or when the microswitch won’t close at all. But, for mice that double/triple-click, will release when dragging, or mis-click on release, this Arduino-based hack may be the good medicine you’re after.
Instructables user [themoreyouknow]’s method cancels click malfunctions by latching the mouse’s controller switch trace to ‘on’ when pressed, keeping it there until the button normally closed contact closes again completely. Due to the confined spaces, you’ll want to use the smallest Arduino you can find, some insulating tape to prevent any shorts, and care to prevent damaging the wires this process adds to the mouse when you cram it all back together.
Before you take [themoreyouknow]’s guide as dogma, the are a few caveats to this hack; they are quick to point out that this won’t work on mice that share two pins between three buttons — without doing it the extra hard way, and that this might be trickier on gaming or other high-end mice, so attempt at your own peril.
All the trap mechanisms are based on a 555 monostable solenoid triggering circuit that ensures that a pulse of sufficient duration is sent to the solenoid to trigger the trap correctly. This way even intermittent contacts will trigger the trap rather than just causing the solenoid to twitch without fully actuating. This is the same technique used to debounce a switch using a 555 timer.
A Raspberry Pi Zero detects motion using an IR camera to film the interesting parts. This is also a good indicator for when you’ve trapped your quarry – if you’re trying be humane then leaving it in a trap for days is counterproductive.