Pages: [1]   Go Down
Author Topic: Input pin always reads high, even with nothing  (Read 1671 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-sad

#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");}
}
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, this is a hardware problem smiley  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.

Code:
#define Button 2
int val= 0;

void setup () {
Serial.begin(9600);
}

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.
« Last Edit: July 15, 2009, 10:38:39 pm by Kitep » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmmh  smiley-sad   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)
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

oh and thank you very much btw smiley
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 6
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jack,

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

http://en.wikipedia.org/wiki/Pull-up_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.

-Matt
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley
Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 170
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
well that answers that one...

I knew the answer cause I was doing the exact same thing just a couple of months ago...   smiley-wink
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(Sry if ressurecting old thread)

One last thing that I noticed now...  the book Had a pull down resistor, I wired it wrong smiley-sad

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?

Lefty
« Last Edit: July 16, 2009, 04:18:28 pm by retrolefty » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

neat, tyvm! smiley
Logged

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:


Code:
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
     }
     else
     {
        ......// switch  is open
     }
    ....
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
The Stuff of Legend!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm... wish some docs had come with the chip smiley

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

0
Offline Offline
Sr. Member
****
Karma: 4
Posts: 329
KC4MM
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: