Input pin always reads high, even with nothing

Ugh... really starting to hate this whole "programming" thing...

Board reads pin as High, even with nothing attached...

something wrong with my code? (That's basically right out of a book) Worked before :(

define Button 2

int val= 0;

void setup () { pinMode(Button, INPUT); Serial.begin(9600); }

void loop () { val=digitalRead(Button); if (val ==HIGH) { Serial.println("one");} else { Serial.println("two");} }

Actually, this is a hardware problem :) Turns out "unattached" is not the same as "ground"

With nothing attached to the pin, it's a "floating" voltage that varies randomly. If you want to make sure it's low, attach a wire from the pin to ground (digital pin 14) If you really want to see how it floats, try it on an analog pin. Watch the value change as you move your hand closer and further away.

#define Button 2
int val= 0;

void setup () {

void loop () {
  val=analogRead(Button);    //read ANALOG pin 2
  Serial.println(val);              // and show value 0-1023

Good luck!

P.S. Just to be clear, I included the sketch just so you could see how the value floats with nothing attached. Your code is fine.

Hmmh :( well that answers that one...

But I can't use a button either, always reads high if not attached to ground... isn't that how you make a switch? (Connected v. not?)

(Button checks out electromechanically with ohmmeter)

oh and thank you very much btw :)


What you're looking for is a button with a pull-up, or pull-down resistor.

With a pull-down resistor connected between the pin and ground, and then the button connected between the pin and V+ (or what have you), the pin will read low until the button is pressed.


k : ) thanks!

(Weird that I got it to work w/o that now that I think about it... I use open pins for random numbers normally anyways :)

well that answers that one...

I knew the answer cause I was doing the exact same thing just a couple of months ago... ;)

(Sry if ressurecting old thread)

One last thing that I noticed now... the book Had a pull down resistor, I wired it wrong :(

But there's no resistor in the current path? it doesn't hurt the chip to have a short circuit?

But there's no resistor in the current path? it doesn't hurt the chip to have a short circuit?

When an Arduino pin is programmed to be an input pin it is a high impedance load and will draw almost no current, so with a external pull down resistor the only current draw is from the ground through the external pull down resistor to the external voltage source, ohms law can help you determine the amount of current that the pull down resistor will draw. Now grounding a Arduino pin that is programmed to be an OUTPUT pin and setting the pin to high in software will cause a short circuit and damage the pin. Conversely wiring an output pin to +5vdc and setting the pin to a low output will also cause a short circuit and damage the pin, But Arduino pins set to input mode can only really be damaged by wiring to voltages above or below the maximum allowed, generally 0 volts and +5vdc ( so no negitive voltages of volts above +5volts allowed) for 5 volt systems.

That make sense?


neat, tyvm! :)

A convenient way to connect a switch is between an input pin and ground. The processor chip conveniently has built-in pullup resistors that you can enable with your software so you can eliminate the external pullup resistor. Additionally it's usually more convenient and better practice to connect one side of all your switches to ground instead of the +5V supply.

Your code would look like this - assuming a switch connected between pin 3 and ground:

int switchPin = 3;

void setup()
    pinMode(switchPin, INPUT);    // makes pin 3 an input pin
    digitalWrite(switchPin, HIGH); // writing a HIGH to an INPUT pin
    // connects the internal pullup resistor

void loop()
     if(digitalRead(switchPin) == LOW)
        ....... // switch is closed
        ......// switch  is open

Hmm... wish some docs had come with the chip :)

Much easier.... and keeping that pin high all the time doesn't drain batteries b/c the inputs have very high resistance?

Well think about it. Any current that might flow from the input pin through the internal pullup would have no place to go when the switch is open - as most pushbuttons normally are. Current would only flow ( and a very small current at that - 5V/20kohms = 0.25 milliamps - when you push the button.

Thanks for the info about the input, it was making me lose sleep