Electronic – Arduino all input pins same value

analogarduinoinput

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?

Best Answer

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.