Pages: [1]   Go Down
Author Topic: Strange sensor interference?  (Read 1143 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 5
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
Code:
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

Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley-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)

Code:
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);
}
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Offline Offline
Full Member
***
Karma: 5
Posts: 207
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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 smiley-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)

Code:
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.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
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.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: