Strange sensor interference?

Hi, I have a lm19 temperature sensor and a photo resistor hooked up to analog inputs of my arduino and I'm getting some very strange readings. I dont think my code is the problem but here it is just in case:

int tempPin = A5;//temp sensor
int lightPin = A0;//photoresistor, hooked up between A0 and ground - pulled high

int light = 0;
int temp = 0;

void setup() {
  Serial.begin(300);
  
  pinMode(lightPin, INPUT);
  pinMode(tempPin, INPUT);
  
  digitalWrite(lightPin, HIGH);// pull photoresistor high
}

void loop() {
  // read sensors
  temp = analogRead(tempPin);
  light = analogRead(lightPin);
  
  //send data to computer for graphing
  Serial.print("t");
  Serial.print(temp, DEC);
  
  Serial.print("l");
  Serial.print(light, DEC);
  
  delay(100);
}

So the problem is that as the light level drops below the temperature level, I get erratic readings from the temperature sensor. I posted a picture of one of the readings I graphed, where green is light and red is temperature. The only thing I can think of is adding a capacitor between ground and the temp. sensor Vout pin but really I'd just like to know what's causing this interference.

Thanks.

Uploaded with ImageShack.us

The Arduino only hjas one ADC which is multiplexed. If readings differ too much they interfere.

solutions:

  • do double readings and ignore the first (see below)
  • do multiple readings and average them (exercise for you :wink:
  • do a running average e.g. temp = (2 * analogRead() + 14 *temp) / 16;
  • take care there is enough time between the readings (move reading of light after Serial prints (temp)
int tempPin = A5;//temp sensor
int lightPin = A0;//photoresistor, hooked up between A0 and ground - pulled high

int light = 0;
int temp = 0;

void setup() {
  Serial.begin(300);
  
  pinMode(lightPin, INPUT);
  pinMode(tempPin, INPUT);
  
  digitalWrite(lightPin, HIGH);// pull photoresistor high
}

void loop()
{
  // read sensors
  analogRead(tempPin);
  temp = analogRead(tempPin);
  
  analogRead(lightPin); 
  light = analogRead(lightPin);
  
  //send data to computer for graphing
  Serial.print("t");
  Serial.print(temp, DEC);
  
  Serial.print("l");
  Serial.print(light, DEC);
  
  delay(100);
}

robtillaart:
The Arduino only hjas one ADC which is multiplexed. If readings differ too much they interfere.

solutions:

  • do double readings and ignore the first (see below)
  • do multiple readings and average them (exercise for you :wink:
  • do a running average e.g. temp = (2 * analogRead() + 14 *temp) / 16;
  • take care there is enough time between the readings (move reading of light after Serial prints (temp)
int tempPin = A5;//temp sensor

int lightPin = A0;//photoresistor, hooked up between A0 and ground - pulled high

int light = 0;
int temp = 0;

void setup() {
  Serial.begin(300);
 
  pinMode(lightPin, INPUT);
  pinMode(tempPin, INPUT);
 
  digitalWrite(lightPin, HIGH);// pull photoresistor high
}

void loop()
{
  // read sensors
  analogRead(tempPin);
  temp = analogRead(tempPin);
 
  analogRead(lightPin);
  light = analogRead(lightPin);
 
  //send data to computer for graphing
  Serial.print("t");
  Serial.print(temp, DEC);
 
  Serial.print("l");
  Serial.print(light, DEC);
 
  delay(100);
}

Thanks, the code works perfectly but I'm still not really understanding the problem. This is how I understand it: I tell the arduino to read a pin, it sends the voltage through the ADC and stores the number it gets in the ram, then it clears the ADC and switches to the next pin...

So how could there be interference in the values?

Also, "take care there is enough time between the readings (move reading of light after Serial prints (temp)"

I tried that and it worked aswell, yet if I use my original code with a very generous 100ms delay(far more than serial printing) between reading the temp pin and the light pin I get the same interference.

So how could there be interference in the values?

The ADC is an analog device, it does not behave like a digital switch internally. I don't know how it works exactly but it builds up a charge internally and the time this takes is a measure for the voltage applied - ADC GURU's please fill in the gaps - It takes time to "remove" this old charge completely, it is definitely no digital on/off bit. If you start a new reading before the ADC is "clean" you have your interference.

Note that all measures mentioned increase the timespan to solve this.