digitalRead() returns HIGH, with nothing connected

Hi,

I just bought an Arduino Duemilanove and a Nano. Both do this strange phenomenon.

With absolutely NOTHING connected to either Arduino, except for USB, running this code...

int pinTachoInHz = 3;

void setup() {
  // Configure serial output
  Serial.begin(9600);
  
  //Configure pin modes
  pinMode(pinTachoInHz, INPUT);
}

void loop(){
  if (digitalRead(pinTachoInHz) == HIGH)
    {Serial.println("HIGH");}
  else
    {Serial.println("LOW");}
  
  delay(250);
  }

Gives this result:

HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH
HIGH

Now for a start, there's nothing running into Digital pin 3. I've also had it randomly return HIGH or LOW, depending on whatever it feels like doing.

Something about this just does not make sense. :-? Any thoughts?

Edit: I should also mention that it doesn't matter which pin I do this to. It still returns the wrong value.

With nothing attached a pin can float. You need to either have it grounded or powered. See: pull up resistor.

Good point.

Does that mean I need to have an external resistor connected to it, or do I just turn on the internal pull up resistor?

Looks like I have to have one externally. Thanks for your help, finally, my head has stopped aching!

It is normal, digital input doesn't know nothing. A pin is either LOW or HIGH. When left open it floats and charges (static electricity for example) accumulate in the input capacitance of the IO leading to the HIGH you read. At the moment you'll connect something to this input it state will be correctly read.

with digitalWrite(pin,HIGH); u can turn on the internal pull-up resistor of pin, if it is conf-ed as INPUT...

there is no internal pull-down resistor...