I'm working with a very simple circuit to understand push buttons. The code in the tutorial uses the INPUT_PULLUP pinmode. I understand the idea of the pinmode, but I have two questions:
There must be a difference between a LOW reading and no connection, correct? How does the arduino differentiate between the two?
I tried to experiment with this idea. So my thought was that you could create a circuit where the LED only stays lit when you hold the button down. In this setup, I used just normal INPUT for pinmode. So the logic was: if LOW then turn light on, else light off. The turnout was the light was almost always on and just really finicky when i moved the board. Why?
Thanks!
-
1To us to fully answer this question you need to post your schematic and your code.Nick Gammon– Nick Gammon ♦02/18/2018 06:30:14Commented Feb 18, 2018 at 6:30
-
1Upvoted for the good question which I learnt from by the comments and answer of Nick.Michel Keijzers– Michel Keijzers02/18/2018 22:08:37Commented Feb 18, 2018 at 22:08
1 Answer 1
There must be a difference between a LOW reading and no connection, correct?
No, the input circuitry measures a voltage. The exact details are in the datasheet, but how could it know there is nothing connected? There is not some sort of sensor that detects the insertion of a wire into the input. Therefore it is trying to read a voltage all the time.
With nothing connected then stray voltages from nearby circuitry will influence the reading.
... and just really finicky when i moved the board ...
Yep, you are influencing the voltage by capacitance from your hand.
You need to have a definite voltage for "no switch pressed" which can be a resistor to ground (eg. 10k) or a resistor to +5V.
I have a tutorial on this.
It sounds like you have wired your switch like this:
Because you don't have a definite voltage there, the input could "randomly" switch between LOW and HIGH.
You could add a pull-down resistor:
Or a pull-up resistor:
Or use the INPUT_PULLUP mode to create a pull-up resistor in the processor hardware. (There is no INPUT_PULLDOWN mode).
Then you will get consistent results.
-
Upvoted ... thanks Nick for this great answer... I learnt a lot from it (and thought the Arduino also had an INPUT_PULLDOWN mode.Michel Keijzers– Michel Keijzers02/18/2018 22:09:11Commented Feb 18, 2018 at 22:09