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.
For most of us who have experimented with Morse code, the oldest key we are likely to have used will have been a piece of military surplus kit from the Second World War era. [Kyle Gabriel] however is a lucky man. His grandfather left him his key-on-board telegraph practice set, a vintage key and telegraph sounder arrangement used to learn Morse code in the days when the telegraph was king. Rather than keep the set merely as an heirloom, [Kyle] set about bringing it up to date by interfacing it to a Raspberry Pi and writing a Morse reader program.
Along the way [Kyle] had to contend with debouncing the switching signal from the key, considering an RC network before settling on a software debounce timer. He provides a brief synopsis of the mechanics of Morse decoding software, and a demonstration of the code in action which you can see in the video below the break.
[Kyle’s] decoding software, beatbybeat, is on GitHub. We can see it will be a useful tool for anyone interested in Morse, or who is writing their own Morse software.
The hourglass dramatically depicts the flow of time; gravity pulling grains of sand inevitably downward. So it is with the Bits of Time project by [Frank Andre]. The pixels drop, stopping only when the battery dies. Or, when your eggs are ready. (Pssst, it’s also on Hackaday.io.)
The project starts with a couple of [Frank’s] PixBlocks. A processor is added to one PixBlock to serve as the controller for both after they are connected via the serial bus. A tilt switch, with a debouncing circuit, is connected to an IO pin. This tells the processor the orientation of the box and therefore which way the pixels should flow.
Two switches set the duration of the timer in 15 second increments. A third starts the timer. When the box is rotated the pixels start flowing in the opposite direction. With code available on GitHub the system can be programmed for other effects such as changing colors, flickering, or even text display.
We love a good musical build, and this one is no exception. For their ECE4760 final project, [Wendian Jiang], [Hanchen Jin], and [Lin Wang] of Cornell built the nicest-looking touch piano we’ve seen in a while. It has five 4051 multiplexers that take input from 37 capacitive touch keys fashioned from aluminium foil and copper tape. Thanks to good debounce code, the sounds are clean even though the keyboard is capable of four-note polyphony.
The group’s original plans for the project included a TFT screen to show the notes on a staff as they are played. While that would have been awesome, there was just too much going on already to be able to accurately capture the notes as well as their duration. Check it out after the break.