Arduino has an input value in disconnected pin

I'm new to arduino. Maybe is an elemental question...

int mval = 0;
void setup() {
Serial.begin(9600);
// set up the LCD's number of columns and rows:

}

void loop() {

Serial.println(mval);

delay(1000);
}


I get a value that varies between 300 and 400. When I think it have to be 0, being that there's nothing connected to that input.

Any ideas?? It's an arduino UNO connected to my laptop via USB

• – Hello World Jan 12 '15 at 11:55

You're getting something called "floating input". When you think about it, an ideal device for measuring input will have a very high input impedance, and thus will not remove very much electricity from the pin.

So what happens is that random electrons float on to (or off of) the pin (either from electro-magnetic interference in the environment or from direct contact with something with a slight charge), and because it acts like a very high resistance connection to ground, this charge will drain off very slowly. Thus when you go to measure it, it looks like there's some voltage there! In general, you either hook this up to some device which provides a voltage, or you put in a pull-up or pull-down resistor and adjust your expectations on that pin.

I hope that helps and if not I'm sure someone has a link to another person who has answered this question more competently (I see in the related section at least one near-identical question).

• Fun fact: The Arduino docs suggest using the return value of an analogRead call on an unused pin as the seed to the randomSeed function: arduino.cc/hu/Reference/RandomSeed – Chris Laplante Jul 13 '12 at 18:48
• Ha! That's pretty neat, I like it! I would think that adding in a little squiggily line of wire to your breadboard and/or PCB might help get a bit more randomness so long as you avoided an 60Hz antenna length (USA concern only) (then suddenly AC noise would dominate) – Kit Scuzz Jul 13 '12 at 18:54
• A more robust (and space saving) approach would be to use the RFC 1149.5 standard random number generator – Chris Laplante Jul 13 '12 at 18:56

The input is "floating", because there is nothing connected to "pull" the voltage one way or the other.
This is perfectly normal. If you connect a high impedance pull-down resistor (say 10k) from the pin to ground it will stop it doing this when nothing is connected (although there is no real need to do so as you won't be reading it when nothing is connected ;-))

• Do pull-down resistor will be change the signal if connected to ADC? – user30878 Dec 11 '15 at 11:48