Go Down

Topic: Analog Pins with mystery voltage in Mega 2560 (Read 717 times) previous topic - next topic

fedusenko

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.

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

void loop(){
 
Serial.println(analogRead(1));
}


Quote
2รท393
393
394
395
397
398

a few seconds down the line...
Quote

286
286
287
286
286
287
286



Thanks for any input you have!

James C4S

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.)."
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

CrossRoads

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.
Code: [Select]

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


Then this
int pinXvalue = analogRead (pinX);
should yield close to 1023.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

fedusenko

#3
Apr 16, 2012, 06:30 am Last Edit: Apr 16, 2012, 06:34 am by fedusenko Reason: 1
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!).

CrossRoads

Missing grounding occurs a lot, you are faaaaar from the first 8)
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Go Up