I'm having the classic debouncing headache with the Arduino. The switch is meant to turn an led on and off. So if I use a toggle button (SPDT) would it still bounce? Maybe it won't since it not a push button. How much more would it cost to use toggle? It's just that it's a project I've been asked to do for my local Fabrication lab.
|
|
Most SPST switch or pushbutton will bounce, because there are only two states: contact closed (for instance low level) and contact open (high level through a pull-up resistor). This may seem obvious, but it's that hesitation when opening/closing that causes the bounce; just once is enough to make a toggle not work. You can debounce the switch with a capacitor, but since you're using it with a microcontroller it's cheaper to do it in software. I usually have a 32 ms (software) timer for keypad scans, and only accept a state change if it persists during two consecutive scans. That means you'll have a delay of maximum 64 ms, but since the button will be manually operated you won't notice such a short delay. You mention a SPDT button, and that's the best solution if you want to do it in hardware.
But frankly, I see no reason for not doing it in software, and you'll have much more choice in SPST buttons than in SPDT buttons. If you want a button which hardly bounces then I can recommend the Alps SKQG tact switch
which with the devices I tested had an initial bounce of less than 10 ns. |
|||||
|
|
The Ganssle Group has written a very comprehensive report called A Guide to Debouncing. In it, the author, Jack Ganssle, took 18 different switches and pressed each 300 times. He recorded the results using an MSO scope to look at the actual analog signal as well as the digital representation to simulate what a microcontroller would see.
All the switches tested bounced differently. Some types worse than others. Some not at all. But even identical switches of the same type bounced differently.
So, if you're going to use a switch, having to debounce it is just a fact of life. Part 2 of the guide gives numerous methods to handle the debouncing problem. The Set-Reset (SR) latch mentioned by stevenvh is discussed:
As well as the cheaper RC method:
But, as mentioned in the comments and other answers, if you have a microcontroller, you're probably better off doing it in software. The guide also discusses various algorithms that can be implemented. From a very simple ISR:
To an expanded version to handle a whole port of switches:
Give the whole report a good read. How to debounce a switch is a skill well worth having and one you will use throughout your career. |
|||||
|
|
All dry* mechanical contact switches bounce. The amount varies.A SPDT switch is almost always break before make so staes are low / opemn / high. You CAN get make before break SPDT switches but even those can bounce. *Mercury wetting is used in some relays to improve current handling capability. I think these may tend to be bounce free but I have had no real-world experience of them, and they are rare and mercury wetting will not be found in had operated switches. Software will debounce anything - time taken depending on how ultra-violent and ongoing the bounce is. A Schmitt triggered input with suitable RC delay usually does a good job of debouncing. Bounce is possible if delay is too short relative to worst case boune times. |
|||
|
|






