Simultanously Reading Two Analog Inputs with Arduino

20.9k views Asked by At

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);
 }
4

There are 4 answers

0
Pyrce On

You most likely have a ungrounded or miswired grounding on your temperature sensor. The pin configurations on the analog pins in Arduinos are layed out very close to each other such that floating voltages will move up or down when nearby pins have an applied voltage. If your ground connection (or power, though if it's correct at the beginning it's probably ground) for the sensor is disconnected or fixed to a high impedance line the analog voltages will move all over the place as other normally minuscule voltage sources will dominate the signal pathing. It probably fluctuates heavily if you put your finger near the A3 pin as well.

0
Josh Kosar On

I have run into this issue before when reading multiple analog sensors in rapid succession. One possible cause(and the one I experienced) is the fact that the arduino only has 1 ADC and it charges a capacitor to take that reading. That capacitor can remain charged between readings thus skewing them.

Introducing a delay could potentially help with this as another user has pointed out, however the cleanest solution I was able to come up with was to "Reset" and discharge the ADC capacitor by taking an analog read of a third pin that is connected directly to ground.

 int sensorValue;
 int sensorValue2;
 float tsens;
 float tset;
 int resetADC;
 
 void setup() 
 {    
     Serial.begin(9600);
     pinMode(A0,input);
     pinMode(A1,input);
     pinMode(A3,input); 
 }

 void loop() 
 {  
     resetADC = analogRead(A0);
     sensorValue = analogRead(A3);
     tsens =  map(sensorValue, 0, 1023, 0, 500); 
     
     resetADC = analogRead(A0);
     sensorValue2 = analogRead(A1);
     tset =  map(sensorValue2, 0, 1023, 0, 70);

     Serial.println(tsens); 
     Serial.println(tset);
  }
1
Randall Cook On

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.

0
JonasB On

Is it noise or a bad value? I did a little test routine that looks at a pin and checks it against previous max and min values. I printed it to the serial monitor whenever a new boundary value showed up. If the wrong value is stable, check the circuit. If its noisey around a valid value, a digital low pass filter works pretty well. Take 34 readings of the pot. Find the highest and lowest values and discard those. Then take the average of the remaining 32 readings. I saw a 90% improvement in my setup (40 count error down to 3). 36 readings with 2 high and 2 low discarded would probably improve things further. If you have the time, you can do a double pass filter. Do this same process 34 times, then throw away the high and low and average it again. All together this is 34 x 34 readings, so noise should go away, but you are taking a long time to get a sample and a pot change will take awhile to get detected. To help with the time, I read the pot every pass through the main loop and save each value in a circular buffer. When I need to read a pot, I look at the historical 33 readings along with a 34th new one.