Hi I have a probably simple question with my Arduino UNO.
I have a regular small light lightswitch (with one input, and one output).
I have connected the switch to the 5 volt (switch's "input"), and then from the switch's "output" to A0, and doing analogRead().
When switch is not pressed (circuit is open) I have readings from 200 -> 6-700, sometimes 8-900.
When I press the switch it goes to 1022.
When I then add a resistor from the "output" part of the switch to ground, the readings are what I expected (0 when unpressed, and 1022 when pressed).
What does the resistor do since the readings are as expected only using the resistor?
Does the resistor, through the "GND", somehow "steal" the last piece of electricity that's left in the switch from it's "output" side, but when I press the switch the current follows the least resistance way and then go to A0?
I tried to remove the resistor, but still a connection to the ground, and then the arduino just stopped every time i pressed the switch, and I had to reset the arduino every time this switch was pressed.
Aha, thanks, that means I used a pull down resistor. I assume that by "floating" they mean that there's still some sort of electricity passing left in the wire/switch, which is removed when I inserted the resistor. Thanks for the link
Since we have an election here in two days, let me give an analogy.
"Floating" is like an unmarked ballot paper. It is neither for one candidate (0) or the other (1). It could be anything. And with analog inputs it could be any number from 0 to 1023. Disconnected inputs are not defined. Wave your hand over them and they will change.
oddbeck:
I assume that by "floating" they mean that there's still some sort of electricity passing left in the wire/switch, which is removed when I inserted the resistor.
No, there's no electricity "left" in the wire, in the sense of a residual. But there is random electricity in the air, and that's what the pin is reading, and is why the value varies as you move your hand to and fro. By using the pull-down resistor, you're forcing the pin to 0v and it is then not susceptible to the randomness of the air: it's at 0v. Then when you close the switch it forces it to 5v with no resistor so that wins the 5v vs 0v battle.
Don't ever do a resistor-less pull down though- although that would be ok when the switch is open, and the pin will be at 0v, when you close the switch you'll short the 5v to 0v and fry something.