Tell me more ×
Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. It's 100% free, no registration required.

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

If I load this program:

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

}

void loop() {

  mval = analogRead(1);
  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

share|improve this question

2 Answers

up vote 10 down vote accepted

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

share|improve this answer
3  
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 – SimpleCoder Jul 13 '12 at 18:48
2  
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
3  
A more robust (and space saving) approach would be to use the RFC 1149.5 standard random number generator – SimpleCoder 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 ;-))

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.