Analog Pins with mystery voltage in Mega 2560

Arduino Mega 2560 reads a voltage in the analog in pins while nothing is connected. I noticed the problem while working the bugs out of a photo-resistor/IR sensor project. Finally I wrote a sketch to read the analog pins to the serial monitor and they read out a number that starts just shy of 400, over the span of a few seconds they drop to about 300.

Is this a defective board? Am I missing something? Nothing is plugged in except the usb... code and serial monitor info is provided below.

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

void loop(){

Serial.println(analogRead(1)); }

2÷393 393 394 395 397 398

a few seconds down the line...

286 286 287 286 286 287 286

Thanks for any input you have!

Floating pins (aka unconnected pins) pick up stray energy, creating a voltage on them.

It is sort of why the following note is on the analogRead() reference page:
If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).

The pins, if not connected to anything, are "floating". Neither high nor low. Use a 10K resistor and connect them high or low.

Or, you may be able to use pinMode and set them as inputs, with digitalWrite (pinX, HIGH); to enable the internal pullup resistors.

pinMode (pinX, HIGH);
digitalWrite (pinX, HIGH);

Then this int pinXvalue = analogRead (pinX); should yield close to 1023.

James, I have not yet looked at the reference pages yet. I bought the Arduino book from Radio Shack and have been working through that. I just looked at the reference page and found a wealth of information. Thank you!

Cross Roads, Thanks for your input. My electronics is a bit lacking. The issue was a lack of grounding… please don’t tell anyone. Now my Night Light Motion Detector works like Ford from the 60’s (great!).

Missing grounding occurs a lot, you are faaaaar from the first 8)