2
\$\begingroup\$

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?

alexan_e
11.3k1 gold badge30 silver badges62 bronze badges
asked Jan 26, 2014 at 18:11
\$\endgroup\$
16
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 26, 2014 at 18:56
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 26, 2014 at 18:58
  • \$\begingroup\$ when there is input its on and when theres not its off \$\endgroup\$ Commented Jan 26, 2014 at 18:59
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 26, 2014 at 19:06
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Jan 26, 2014 at 19:49

1 Answer 1

4
\$\begingroup\$

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.

answered Jan 26, 2014 at 20:38
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ Commented Feb 1, 2014 at 8:51

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.