Take the 2-minute tour ×
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.

Here's the situation: I have an Arduino mega 2560 and I'm trying to analogRead from 2 different pins.

code:

void setup()
{
  pinMode(A0, INPUT);
  pinMode(A15, INPUT);
  Serial.begin(9600);
}

void loop()
{
  Serial.print("0:");
  Serial.print(analogRead(A0));
  Serial.print(" 15:");
  Serial.println(analogRead(A15));
}

The voltage I'm trying to measure is arduino 3.3V supply connected directly to the ADC input pin and the result I get varies depending on the connection of the inputs:

Both inputs floating:
A0: 376, A15: 376

A0 connected, A15 floating:
A0: 771, A15: 655

A0 floating, A15 connected:
A0: 409, A15: 696

A0 connected, A15 connected:
A0: 782, A15: 780

What is the cause of this fluctuation?

share|improve this question
    
I did some changes to the code to make the print easier to read and didnt add the colon sorry about that. when I say on or off I mean with a 3.3V input from arduino. –  Nick Jan 26 at 18:56
    
OK, so what exactly do you mean by "a 3.3V input from arduino"? Does that represent "on" or "off"? These details matter very much. –  Joe Hass Jan 26 at 18:58
    
when there is input its on and when theres not its off –  Nick Jan 26 at 18:59
    
Can you show what feeds the the ADC inputs? Are the inputs shorted and connected to the same signal? Is there any resistor or capacitor? A schematic of the relevant pins would help. –  alexan_e Jan 26 at 19:06
1  
Connect the "off" pin to ground, so leakage/crosstalk doesn't give false readings. I wouldn't expect an open input to have any valid value. –  Peter Bennett Jan 26 at 19:49
show 11 more comments

1 Answer

up vote 3 down vote accepted

As user alexan_e said in the comments, you've got these problems because your analog input pins are floating.

Never leave any input pin floating if possible. With digital I/O this could lead to increased power consumption. This problem does not show up with analog I/O, nevertheless you will run into other troubles like the one you described. Have a glance of this question to see other reasons to avoid letting input pins floating.

What I suggest you is to all your inputs, especially the ones you care about, have a path to a known voltage through some resistance—often, a pull-down resistor is the answer, but that typically depends of what you're trying to achieve. How big is this resistor depends of the impedance of the driving circuit and noise, but usually something between 10K and 1M ohms; lower values are good for noise, higher values is good for maximizing input impedance and low frequency response for AC-coupled circuits.

share|improve this answer
    
Alexan hasnt answered im accepting yours. –  Nick Jan 28 at 18:04
1  
@Nick I'm sorry, after my last comment I didn't receive any notification for follow-up comments. Jarhmander, thank you for mentioning me although it wasn't necessary. I think you have avoided mentioning the solution of setting the pin as an output leaving me space to post a reply but for completeness I think you should add it in your reply, there is no reason for me to post any new reply. –  alexan_e Feb 1 at 8:51
add comment

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.