Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 02:39 29 Nov 2024 Privacy Policy
Jump to

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 States
Posts: 925
Posted: 02:16pm 09 Mar 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 819
Posted: 02:26pm 09 Mar 2014
Copy link to clipboard 
Print this post

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.Edited by MOBI 2014-03-11
David M.
 
vasi

Guru

Joined: 23/03/2007
Location: Romania
Posts: 1697
Posted: 02:31pm 09 Mar 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 9308
Posted: 02:35pm 09 Mar 2014
Copy link to clipboard 
Print this post

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.
Edited by Grogster 2014-03-11
Smoke makes things work. When the smoke gets out, it stops!
 
paceman
Guru

Joined: 07/10/2011
Location: Australia
Posts: 1329
Posted: 04:45pm 09 Mar 2014
Copy link to clipboard 
Print this post

  vasi said   Here is a document http://www.eng.utah.edu/~cs5780/debouncing.pdf

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 States
Posts: 229
Posted: 05:44pm 09 Mar 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 711
Posted: 07:22pm 09 Mar 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 819
Posted: 07:40pm 09 Mar 2014
Copy link to clipboard 
Print this post

  Quote  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.


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: Thailand
Posts: 2209
Posted: 09:07pm 09 Mar 2014
Copy link to clipboard 
Print this post

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: Australia
Posts: 819
Posted: 09:12pm 09 Mar 2014
Copy link to clipboard 
Print this post

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 Zealand
Posts: 2350
Posted: 12:53am 10 Mar 2014
Copy link to clipboard 
Print this post

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


To reply to this topic, you need to log in.

© JAQ Software 2024