Noise on a Switch

bishop_house:
Hi all, This is my first post here since I discovered Arduino, I really like it but I am not really familiar with elettronics :frowning:
What I am tryng to do is to use one of the analog input of arduino to recognize if a switch has been turned on or of, this switchs are used to check if a sliding part has reached the top or the bottom,
Oversimplyfing the circuit, it is something like linking the +5V output of Arduino to one of the Analog inputs using a switch, when the switch is turned on I read rightly the 1023 value, but if the swtich is on the "off" position I can read values that are very far from the 0, sometimes I can read even if the swtich is down a value that is very near to 1023, I understand that it has sense and I am reading the potential of the "Word" around me, but Someone can suggest me a way to decrease this noise? Adding maybe something between the switch and the Analog input port? Maybe a capacitor?

Thank you for your help!
Ciao ciao!!
Giampiero

The problem with your method is that when the switch is not help down there is not a really electrical 0 volts being applied to the pin, but rather a 'floating input' condition. You need to wire a say 5k ohm resistor from the analog input pin and ground, that way when the switch is released the pin will see a ground voltage and give a 0 count return. Try it and see if that does not improve your situation.

Lefty