2

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,070
  • 1
  • 28
  • 62
user36976
  • 135
  • 7
  • 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. – user36976 Jan 26 '14 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 '14 at 18:58
  • when there is input its on and when theres not its off – user36976 Jan 26 '14 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 '14 at 19:06
  • No resistors its a direct connection from arduinos 3.3V pin to the inputs. – user36976 Jan 26 '14 at 19:14
  • So are you saying that if you leave one of the ADC inputs floating (not connected) the result of the other one changes? Is that what on/off means? – alexan_e Jan 26 '14 at 19:21
  • yes when one of the inputs is receiving signal the read of the other changes too – user36976 Jan 26 '14 at 19:22
  • When the pin is "off", is it grounded, or just open circuit (not connected to anything)? If a pin is not connected, I can imagine leakage, crosstalk, or stored charge could give false readings. – Peter Bennett Jan 26 '14 at 19:22
  • off is completely disconnected aka open circut – user36976 Jan 26 '14 at 19:23
  • Have you tried successive readings of the same pin? Do you get the same wrong reading continuously or only for the first few conversions? Maybe you should just discard the first reading. – alexan_e Jan 26 '14 at 19:26
  • I have done multiple tests always the same result it goes like this: all disconnected is at about 350-400 connect one pin: pin skyrockets to 680-690 and at the same time within 5-6 seconds the other pin rises to 580-650 – user36976 Jan 26 '14 at 19:34
  • 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 '14 at 19:49
  • Thanks that solved it! will need a bit of tweaking to adapt my full code but anyways thanks(if you want convert it to an answer to accept it) – user36976 Jan 26 '14 at 19:52
  • How about making the pin that is not connected a digital input with pull up enabled. – alexan_e Jan 26 '14 at 19:58
  • @Nick How about to clean up this question a little bit and add the information about the floating pin to the question, so important thing is not hidden in the comments. – Johan Jan 27 '14 at 06:29
  • @Johan changed the output from "on" or "off" to floated and connected that should clear things up – user36976 Jan 27 '14 at 13:06

1 Answers1

4

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.

Jarhmander
  • 443
  • 2
  • 7
  • 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 01 '14 at 08:51