Pages: [1]   Go Down
Author Topic: Analog Pins with mystery voltage in Mega 2560  (Read 695 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27357
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
pinMode (pinX, HIGH);
digitalWrite (pinX, HIGH);

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

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!).
« Last Edit: April 15, 2012, 11:34:21 pm by fedusenko » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 545
Posts: 27357
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Missing grounding occurs a lot, you are faaaaar from the first smiley-cool
Logged

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.

Pages: [1]   Go Up
Jump to: