1
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ CodeTao... I tried multiple reads with up to 10 second delays to no avail sorry to say... Also tried ground the unused pins.. still no joy.. \$\endgroup\$ – Mark Fellowes May 14 '13 at 22:48
2
\$\begingroup\$

It's quite normal for unconnected ADC pins that are floating to produce random results so you're probably looking for a problem that doesn't exist. Do the readings from the actual channels used look OK? Rather than looping through the whole lot maybe try something like the following which is untested because I don't have the Arduino IDE loaded:

#define SENSOR_COUNT 2
const int analog_pins[SENSOR_COUNT] = {0, 15};

void loop() {
   for(int I=0; I < SENSOR_COUNT; I++){
      int sensorValue = analogRead(analog_pins[I]);    
      float voltage = sensorValue * (5.0 / 1023.0) - 0.04;
      Serial.print("Address = ");
      Serial.print(I);
      Serial.print(" ");
      Serial.print(voltage);
      Serial.println();
      delay(5000);  
    }
}

If the channels you're interested in don't look good maybe the input impedance is too high, try something below 10K which I believe is the usual recommendation for most AVR parts. If you have a higher impedance and low sampling rate you could probably do two reads in a row so that the analog MUX has been stable a while disregarding the first reading:

analogRead(analog_pins[I]);
delay(1000);
int sensorValue = analogRead(analog_pins[I]);
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ PeterJ... Using this method is good to isolate the individual pins that have a probe on them... Maintenance point of view I would need to recompile / upload the program each time a probe is added / removed.. Is it possible to test the pin for a true voltage..?? also you mention the impedance.. what do you mean by "try something below 10k) are we talking putting a resistor on each feed..?? Excuse my ignorance. \$\endgroup\$ – Mark Fellowes May 14 '13 at 22:46
  • \$\begingroup\$ @MarkFellowes, if you can have more probes installed one idea that come to mind is inserting a pull-down plug in the unused sockets, say a 1K resistor to ground so it reads close to zero instead of floating. The 10K impedance means if say you your temperature sensor can look like say a 100K resistor you might need the extra delay, the standard Arduino routines won't allow enough time for the input to settle. An ADC is in effect charging up a capacitor so with a high input impedance (resistance) it will take longer to charge up to the proper voltage. \$\endgroup\$ – PeterJ May 15 '13 at 0:23
  • \$\begingroup\$ Hi Peter... I did not have a 1k resistor but had a 1k8 and this pulled it down to 0.00. Huge thank you...Ok.. next part.. Easy enough to put a resistor from the ground to the vacant pins but that will mean removing the resistor when a probe is to be put into production.. Yes..?? CoderTao mentions below to use the digitalWrite(HIGH) but this will put it to a 5v. What if I actual have a maximum reading (100 degrees C) possible but not likely. \$\endgroup\$ – Mark Fellowes May 15 '13 at 5:16
  • \$\begingroup\$ @MarkFellowes, yes you'd either have to remove the the resistor or compensate for it in the readings, but depending on the probe that's probably not a good option and might resolution. Lots of audio jacks have pins that are normally closed that are pushed apart when a plug is inserted, that might be a neat way to go about it. So you'd connect your pull-down to that pin and it would be automatically disconnected when the sensor is plugged in. \$\endgroup\$ – PeterJ May 15 '13 at 5:29
1
\$\begingroup\$

Quote from here

The Atmega datasheet also cautions against switching analog pins in close temporal proximity to making A/D readings (analogRead) on other analog pins. This can cause electrical noise and introduce jitter in the analog system. It may be desirable, after manipulating analog pins (in digital mode), to add a short delay before using analogRead() to read other analog pins.

Like CoderTao says - try grounding the unused pins.

The similarity in values read from the unused pins to the values on your used pins is symptomatic of multiplexing several analogue inputs to the input of an ADC. It has a sample and hold amplifier which will "hold" the voltage from An even when An+1 is read UNLESS the input into An+1 forces a new voltage. The force takes a few microseconds and having an input floating does not force a change.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Team... Thank you very much for your advice.... I will try both methods.. Grounding each unused pins and also do a multi read on each... \$\endgroup\$ – Mark Fellowes May 14 '13 at 21:26
  • \$\begingroup\$ Ive also tried doing a digital read on the analog pin address.. ie 69 for pin A15. The result was -0.04.. Huh..?? I have searched for "manipulating analog pins (in digital mode)" but can not find a result. Is this not using a digitalRead on a analog pin..?? \$\endgroup\$ – Mark Fellowes May 14 '13 at 22:50
  • \$\begingroup\$ @Mark the digital read coming back as -0.04 is due to your voltage conversion: sensorValue * (5.0 / 1023.0) - 0.04; sensorValue=0 gives -0.04. \$\endgroup\$ – CoderTao May 15 '13 at 4:31
  • \$\begingroup\$ Yeah saw that one and had to smile.. thank you.. I had this piece of code in there as the display V the output is 0.04 out.... Oooops. Removing that and having the resistor I get the 0.00.. Thanks \$\endgroup\$ – Mark Fellowes May 15 '13 at 6:03

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.