Simultanously Reading Two Analog Inputs with Arduino

user3109237 picture user3109237 · Dec 17, 2013 · Viewed 18.8k times · Source

We are simulating an oven. The potentiometer sets the desired temp and the sensor reads the current temperature of a little copper plate that is "the oven."

Both the temp sensor are connected to their own analog input pin on my arduino uno. Individually, I have gotten values for both the potentiometer and the temp sensor that make sense (I am monitoring the values on the serial window). However, when I adjust the potentiometer it significantly alters the sensor reading.

For example:

  • The potentiometer is at its 0 position, and the sensor is in the room temperature air. The serial shows TempSensor = 22 C, TSet = 0 C. This is normal.
  • Then when I turn the pot up: TempSensor= 40 C, TSet=55 C. -But the temperature sensor is still in the room temp air! So the pot value, TSet, goes up like it should, but also affects the sensor reading even though the temperature hasn't really changed.

Any advice would be greatly appreciated. Thanks!

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

 void loop() {  
     int sensorValue = analogRead(A3);
     float tsens =  map(sensorValue, 0, 1023, 0, 500); 

     int sensorValue2 = analogRead(A1);
     float tset =  map(sensorValue2, 0, 1023, 0, 70);

     Serial.println(tsens); 
     Serial.println(tset);
 }

Answer

Randall Cook picture Randall Cook · Aug 26, 2015

I've recently bumped into a similar problem and my searches suggest that inserting a delay between the reads can help. On this question, I found this answer and this answer particularly helpful.

The idea is that you need to let some time pass after making a reading, then make another reading after the ADC has stabilized. Here's a function I have been using:

int safeAnalogRead(int pin)
{
  int x = analogRead(pin);  // make an initial reading to set up the ADC
  delay(10);                // let the ADC stabilize
  x = analogRead(pin);      // toss the first reading and take one we will keep
  delay(10);                // delay again to be friendly to future readings
  return x;
}

I'm still having trouble getting an accurate reading of several potentiometers connected to the analog pins configured as a voltage dividers between vcc and ground, but at least now the values are stable.

BTW, it could be argued that since you have a delay after the first reading, it isn't necessary to have the second delay. This might matter if you call safeAnalogRead() twice in quick succession on two different pins.