Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : real world switch debouncing
Author | Message | ||||
viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
Hello, Now that uMite is getting better and better, I would think that many uses for it would involve simple momentary switch reading. Does anyone have experience with reading switch state directly on in input port or through I2C port expander like the 23017, and dealing efficiently with switch bounce? What is the best method to read and reliably get the state of the switch? Should one use resistors and capacitors to smooth out the signal from a bouncy switch or use code or both? What should be taken into account when interfacing and reading a switch that may be connected with a bit of a wire run, like in a control panel or machine? Any and all help is greatly appreciated as usual! |
||||
MOBI Guru Joined: 02/12/2012 Location: AustraliaPosts: 819 |
Every one has their preferences. As we are mostly dealing with MCUs, the simplest is probably to use software as it needs no extra components. Detect a key press, delay around 20mSeconds and check if there is still a key press. If so, decode it. 20mSecs has been the "industry standard" since the days of MPUs and beyond. It has never let me down. Most key bounces don't last longer than 20mS. If 20mS isn't long enough, it is no big deal to lengthen it. You can even go as far as decoding the key, delay 20 and check if it is still the same key. David M. |
||||
vasi Guru Joined: 23/03/2007 Location: RomaniaPosts: 1697 |
Here is a document http://www.eng.utah.edu/~cs5780/debouncing.pdf Hobbit name: Togo Toadfoot of Frogmorton Elvish name: Mablung Miriel Beyound Arduino Lang |
||||
Grogster Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9308 |
That's all I have ever done. Pseudo code: If button is pressed then pause 50 If button is pressed then Do what button is supposed to do Else Ignore and go back to main routine Endif [/code] As MOBI says, this has always worked for me too, and I have never had issues. I always supply-reference the input pin, and have the momentary switch pull the pin to deck(ground) rather then ground-referencing the pin, and having the push button pulse the input pin high when pressed. I used to pulse the pin high when pressed, but this did result in some false-alarm type behaviour once installed, if there was any kind of jitter on the line(press switches were meters away from the MCU, via cable in the wall) - the MCU would respond to that as a call, when it was not. Since I started supply-referencing the pin, and pulling it low with the switch, I have never had that problem again. Smoke makes things work. When the smoke gets out, it stops! |
||||
paceman Guru Joined: 07/10/2011 Location: AustraliaPosts: 1329 |
Really interesting document Vasi - trust the US secret service to know about fast ON/OFF. Greg |
||||
Lou Senior Member Joined: 01/02/2014 Location: United StatesPosts: 229 |
I always read the switch again in 50ms and decode as Grogs said. That always worked well for me and didn't noticeably slow things down. Lou Microcontrollers - the other white meat |
||||
jman Guru Joined: 12/06/2011 Location: New ZealandPosts: 711 |
If you only want ONE keypress Check it's been released first before doing anything else While Button=0:Wend Jman |
||||
MOBI Guru Joined: 02/12/2012 Location: AustraliaPosts: 819 |
The time delay with software debounce is usually not a problem if you are dealing only with manual i.e. "finger on the button" switches which could take seconds to press. Mechanical systems reading "end stops" etc must react much faster so hardware debounce becomes an imperative. David M. |
||||
MicroBlocks Guru Joined: 12/05/2012 Location: ThailandPosts: 2209 |
I have had trouble with end stops on 3d printers. Especially when the wires are running along the wires from the stepper motors. A good pullup resistor, and small r/c will cleanup the signal so that it has sharper edges and less noise. Debouncing end stops in software is indeed not a viable option. Microblocks. Build with logic. |
||||
MOBI Guru Joined: 02/12/2012 Location: AustraliaPosts: 819 |
Has anyone tried an opto interrupt or a hall effect to reduce bounce? Just a thought. David M. |
||||
robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2350 |
the solution that nobody seems to have mentioned is to use a microswitch with both n.o and n.c contacts. this removes the need for any debouncing, but does tie up two pins per switch - in some applications this may be a small price to pay. use the below code with 4k7 pullups on the two pins (see following paragraph for reason), and the microswitch 'common' contact tied to ground: activated = 0
SETPIN 2, INTL, sw_no SETPIN 3, INTL, sw_nc DO LOOP sw_no: activated = 1 IRETURN sw_nc: activated = 0 IRETURN as an aside, a while ago i did work for an outfit that made motor soft-starters. some bright spark had 'designed' a clever input filter for the remote switches that included protection diodes. unfortunately, in the field, the wires to the switch acted like an antenna, while one of the diodes made a quite effective detector! the lesson to be learnt there is to always make your switch inputs (at your micro) low-impedance. rob :-) |
||||
Print this page |