1

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!

2
  • 1
    To us to fully answer this question you need to post your schematic and your code.
    – Nick Gammon
    Commented Feb 18, 2018 at 6:30
  • 1
    Upvoted for the good question which I learnt from by the comments and answer of Nick. Commented Feb 18, 2018 at 22:08

1 Answer 1

6

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:

Floating input

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:

Pull-down resistor


Or a pull-up resistor:

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.

1
  • Upvoted ... thanks Nick for this great answer... I learnt a lot from it (and thought the Arduino also had an INPUT_PULLDOWN mode. Commented Feb 18, 2018 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.