I’ve been teaching students of all ages, from 8 years old to adults, and one of the most common things I’ve heard is about bouncing. Sooner or later, we are faced with this rather simple problem, so what causes it, and what can we do?
Let’s start off with a simple schematic. Press the button, and VOUT goes to VCC. Release it, and it goes back down to GND, through the pull-down resistor.
In theory, nothing could be simpler than a switch. Make an electronic connection, press the button, and here we go. The microcontroller will notice an “edge” (the change of voltage to/from VCC and GND), and act accordingly. The problem is, it is never that simple. I asked some students to wire up a simple switch, and to count the number of presses. Initially, they looked at me as if I had underrated their capabilities, but it was a trick question, and it didn’t take long before they had questions. Hey, but I only pressed once, how come the microcontroller said I pressed two, three times? (and in one case, up to seven times). They then went through the code, looking for problems. The problem wasn’t the code, it was mechanical.
I did the same thing at home, only this time I connected an oscilloscope. This is what I got when I pressed the button:
You can see where I pressed the button, the voltage jumps from 0V to 2V. However, it looks like I pressed it twice; 40µs later, it goes back to zero, and after 50µs, it goes back to 2V. My microcontroller is fast enough to notice, and so it counts two presses. This isn’t the worst I’ve seen; some of the cheaper push buttons can bounce 6 or 7 times.
Bounce, a definition
To understand how this is possible, you need to imagine what a switch actually is. It is mechanical, and so it has all the advantages, and disadvantages associated with that. Imagine two metal connectors, and a metal plate on top. When the button is pressed, the plate comes down, and makes an electrical contact. Just like when you drop a metal ruler on the floor, the plate will “bounce” slightly, so as the plate hits the contacts, it might bounce slightly, before settling down.
So, what can you do about it? Thankfully, there are several solutions.
Electronically, you can add a resistor-capacitor to slowly charge and discharge. This will add a small delay to your circuit, the time it takes for the capacitor to charge up.
The 74xx14 Schmitt inverter will detect a voltage above a certain level as a logical one, and invert it to zero. Likewise, a voltage below a certain level will be detected as a logical zero, and inverted to one. By adjusting the resistor and capacitor values, the capacitor charge time can be changed, filtering out unwanted bounces. Likewise, when the switch is released, the voltage is discharged through a resistor, removing bounces.
There are devices that are dedicated to debouncing switches and pushbuttons.
In this example, an MC14490 is used. It has internal pull-up resistors, so no external components are required. The capacitor C2 is used for the internal oscillator, and apart from that component, no other components are required. The only down side to this situation is the price of the component itself; around 5€ per component, but each one can debounce 6 switches.
If the microcontroller is fast enough to actually see multiple bounces, then it is also fast enough to be able to debounce them. There are two philosophies; act immediately, or act when sure.
If the exact timing is critical (as in less than a millisecond), then the program must register the first edge, and then ignore subsequent edges for a small amount of time. This is also the technique to use if your application is using interrupts. For an Arduino, you can register the time of the first edge using the millis() function, and then compare subsequent edges to that value.
If polling, there are two options. One is to register the first change of state, and to ignore subsequent states, the second is to validate a state change after a certain time. This has the advantage of filtering very brief pulses.
The wrong way
A third technique I’ve seen, and not the recommended way, is to simply poll less often. Imagine a push button on a breadboard. If you press the button for a second, and poll ten times a second, there is a very good chance you will never see any rebounds. Now, try to push that button as many times as you can in a second. How many times? 5? 10? Let’s say 10, even if I can’t do that personally. So, the output will be on for around 500ms, and off for 500ms. Ten pushes, so on for 50ms, off for 50ms, etc. If you poll every 10ms, the statistically, you should be fine. The down side is that this technique adds a high level of latency, and does not guarantee that you won’t be victim of very bad bouncing.