How to Debounce a Mechanical Contact or Switch
Debouncing Algorithms
- As soon as a transition is detected, accept the new state. This is an easy method for low-speed (25 Hz or less) polling applications, but it may mistake a glitch for a transition if the glitch happens exactly at the sample moment;
- After the detection of a transition, start a timer to check the input again 20 milliseconds or so later. Use as contact state the value at the input when the timer expires. This works with both polling and interrupts but requires a timer. Keeping the pin interrupt disabled while the timer is running will avoid unnecessary stress due to bouncing;
- A step further is to require two or more identical samples before accepting a state change, for instance three out of four or seven out of ten. This is the digital equivalent of the integrating RC debounce network. Instead of requiring x out of y valid reads, you can also require z consecutive valid samples. Its responsiveness depends on the number of samples and the sampling rate.
Example Code?
So now you probably expect example code that you can cut & paste into your program. Well, there isn’t. How to best implement debouncing algorithms highly depends on the cleverness of the programmer and the resources available. I did draw up some though flowcharts though that you can refer to.
Schematics
Here is an LTspice simulation of the RC debounce circuit that we started with. To make the simulation produce different results for every run, you should check the option ‘Use the clock to reseed the MC generator[*]’ on the ‘Hacks!’ tab of the ‘Control Panel’ (the button with the little hammer on it) of LTspice. The ‘{100*mc(1,1)}’ part in the value for B1 makes this possible. Play with the different R/C values to change the debounce characteristics.So, What Do I Use?
As with every subject in electronics, in engineering and in general, there is much more to be said about it. Every switch bounces in its own way, and its bounce characteristics may change over time. Bounce behavior may be different on opening or closing. Contacts wear and the surface properties change; contact surface has an influence on conductivity. Some systems require fast response times, others don’t care and so on and so further. As a result, it is impossible to say which debounce method is best for you. Learn and understand your system first before deciding on a technique.Read full article
Hide full article
Discussion (1 comment)