Electronic – Arduino Mega 2560 – Ghost Values on Analog pins

arduino

Yet again in need of your help… I have the Arduino up and running reading 2 external temperature probes.

I'm using a common ground (tried separate ground same result) and Analog Pin 0 (A0) with 1 probe and the second probe connected to Analog Pin 16 (A15).

I am displaying the output correctly on the serial screen but as I have it looping through each of the 16 Analog pins it is displaying a value for those pins that nothing is connected to.

In other languages you should always initialize all variables, inputs & outputs.. is this the same for the Arduino and if so, what is the Correct method to initialize or set the Analog pins prior to reading.

Don't laugh at the code.. 😉

void setup() {
Serial.begin(9600);
}

void loop() {
  for(int I=0; I < 16; I++){
  int sensorValue = analogRead(I);    
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1023.0) - 0.04;

  Serial.print("Address = ");
  Serial.print(I);
  Serial.print(" ");
  Serial.print(voltage);
  Serial.println();
  delay(5000);  
}
}

The Output on the display is:-

Address = 0 2.01
Address = 1 2.09
Address = 3 1.97
…… Up to the last pin.

Only Pins A0 & A15 should have values.

Thanks Heaps Again…
Mark

Best Answer

One other item to take into account, depending on your analog sources, rapidly switching between them with analogRead's will have some noise. While there are (6? 16?) Analog Input pins, there's only 1 ADC that is muxed between the pins. When the mux changes with a high impedance source, the voltage will take time to stabilize. The advice I've always seen is to use two analogReads; the first to switch the mux, and to give time for voltage to stabilize, the second as the actual reading.

The voltages read on pins 1-14, assuming they're otherwise disconnected, are probably biased by the applied voltages on 0 and 15 (everything is close to the 2V on pin 0). If the inputs were tied to ground or voltage (digitalWrite(HIGH) typically turns on an internal pull up resistor), you should see them stabilize at 5V.