thaaat_guy:
In all arduino examples with buttons they are also connected (usually with a 10k resistor) to ground. Making the power flow not only between 5V and input, but also to ground.
Why is that so? Shouldn't the circuit be complete with just a connection to the input pin?
I certainly don't want to have energy flow more than necessary (plan to do a few things that run on battery)
if I have to, what is "best" value? (in Tinkercad it still works with a 100MOhm)
Because an Arduino input is unbiased. To anthropomorphize it: It doesn't care if the voltage, applied to it, is HIGH or LOW. That's what makes it an Input.
As such, it also doesn't influence the voltage applied to it. So, if you just connect a switch to it, with it's other end connected to either HIGH [~5V], or LOW [~0V] -- when that switch is closed, the nearly 0 resistance will pull the input to HIGH, if the switch is connected to ~5V, and to LOW, if the switch is connected to ~0V.
But, what happens when the switch is opened? There's the rub. An open switch has a VERY high resistance -- essentially, the resistance of air. Now, an Arduino input also has a very high resistance. But, in comparison, the resistance of air is VERY high! In other words MUCH higher than the resistance of the Arduino, which is around 100MΩ, whereas air...Resistance Of Air
So, what does the very high resistance of air have to do with what happens to that input when the switch is opened? It means that when the switch is opened, there is nothing to pull the input anywhere. The input, to use the actual technical term: floats. That's just a fancy way of saying, the state of an open input is unpredictable.
So, we add a pull-up, or pull-down resistor, on the input, to create a known condition for when the switch is opened. If the switch is connected to ~5V [the side of the switch not connected to the Arduino input], a pull-down resistor is added so when the switch is open, a LOW state is insured. The same for when the switch is connected to ground [i.e. ~0V]. A Pull-Up resistor is added to insure a HIGH state, when the switch is opened.
And, to address the question, how high a resistance [pull-down or pull-up] is too high? Well, that's a question somewhat outside my expertise. I can say this, though, the higher the resistance, the less immune to noise, the input is [as Yoda would say ;)]. Theoretically, using the math of voltage dividers, and the knowledge of the input resistance, this can be computed. Now, everyone is fond of saying that the input resistance is 100MΩ, but actually, according to the Atmega328P datasheet [Table 28-8], that's the Analog Input Resistance. The Digital Input Resistance is more like [worst case, over a temperature range of –40°C to +125°C] 5MΩ [Table 28-2: Input leakage Current I/O pin: 1µA -- at 5V, that's 5MΩ].
Also from the datasheet are the definitions of Input HIGH & Input LOW:
-
Lowest Voltage Guaranteed to be Interpreted as Input HIGH: 0.9*VCC (for a VCC of 5V, that's 4.5V)
-
Highest Voltage Guaranteed to be Interpreted as Input LOW: 0.1*VCC (for a VCC of 5V, that's 0.5V)
Lets take the case of 4.5V as the lowest HIGH:
In order to insure at least 4.5V on the input, R1 needs to be:
** **R1 = RI(VCC - VRI)/VRI = 5M(5.0V - 4.5V)/4.5V = [u]555k[/u]** **
So, there you go. For worst case conditions [the only thing specified in the Data Sheet], the maximum Pull-Up resistance to insure a HIGH state on the input, is 555k. You can do similar math to determine the Max Pull-Down resistance, but it will probably come out to be the same.
If anyone finds an error in my math, or my philosophy, feel free to post corrections 