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?

\$\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\$
    – user36976
    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\$
    – Joe Hass
    Jan 26, 2014 at 18:58
  • \$\begingroup\$ when there is input its on and when theres not its off \$\endgroup\$
    – user36976
    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\$
    – alexan_e
    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\$ 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.

\$\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\$
    – alexan_e
    Feb 1, 2014 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.