Problem with toggle switch

Hi all!

I have this toggle switch:

It has only two connectors.

What i do is connect the arduinos 5V to one connector and the other connector to a digital input of the arduino.
I set the input pin as INPUT.

The problem is that i am having a constant HIGH state in one position (1,1,1,1,1,1...) of the switch but noise when is in the
supposed LOW state ( 0,0,0,1,1,0,1,0,0,1,0). I try to denoise the signal, but when i swich fom high to low it takes almost
one second to switch its state to the noisy state..

Do i need some resistor or capacitor in some place?

Thanks in advance

Daniel

Hooking it like you did, is no guarantee that what you call the low state is really low. You need a pullup resistor to make it high when open and low when closed, or a pulldown to make it low when open and high when closed.

See attached pic.

Edit.... it probably also needs debouncing. I find the bounce2 library easy to use.

Thanks Jimbo!
I'll try it out :slight_smile:

By far the easiest is to use the internal pullup, since then you don't have to mess around with extra resistors in your circuit.

So the switch goes from the i/o pin to ground, use pinMode(x, INPUT_PULLUP); and look for a low when closed (it'll be high (pulled up) when open). That's called "active low".... active meaning when you action the switch.

YOU MADE MY DAY! XD

Thanks a lot Jimbo!

Daniel