analogRead gives raw value without any connection

Any ideas why an analogRead to a pin with nothing connected would give me a value of approximately 300?

Here is my code:

int analogPin = 1;
int raw = 0;           // variable to store the value read

void setup()
{
  Serial.begin(9600);          //  setup serial
  pinMode(analogPin, INPUT);
}

void loop()
{
  raw = analogRead(analogPin);    // read the input pin
  Serial.println("Raw value is");
  Serial.println(raw);        // debug value
  delay(10000);
}

Again, nothing is connected to any of the analog inputs.

Thanks!!

Would you expect it to return NULL or -1 or infinity or some other impossible number?

A balloon that you rub in your hair gains electrons, and you can feel the varying amounts of "charge" from a distance, because your skin hairs are attracted by that charge. The skin and the balloon aren't touching, but there's a potential difference in charge.


http://regentsprep.org/Regents/physics/phys03/aeleclab/ballrub.htm

An unconnected input is the same way: the charge "floats" if you compare it to a known reference like GND. You can't predict the reading, it's going to float at some random value within the range of the analog-digital converter. The analogRead() function always returns something between 0 and 1023 inclusive, and the digitalRead() function always returns HIGH or LOW, but an unconnected input means nothing. It will vary with heat and the proximity of other objects, but not in an easily predictable way. In fact, it could be quite misleading, because no circuit is perfect. If you plug something in to the next input over, you might get a shadow of that reading on the unconnected input.

Any unconnected input on the Ardunio will "float" to a random value. If you want the value to read zero when unconnected, you'll need a pull-down resistor. Around 10k Ohms would be OK.