Accurate LM35 readings


Sometimes LM35 analog temperature sensors give inconsistent readings. Reading fluctuations are simply too high. Specially if LM35 sensor is connected using a cable that is a meter long or longer.  There are some techniques tu compensate for those fluctuations and get more consistent readings. In general if it is possible and higher cost is not a problem it is better to use a digital temperature sensor like DS18B20. A good documentation how to increase analog sensor accuracy can be found here. From my tests and using three LM35-s with 1-2 meter long cables and arduino I did get some ideas:

 

  • Disregard the first reading. It seems that the first sensor reading after a couple of seconds is highly fluctuating.
  • Use lower voltage. You loose some some sensory range, but get more precise results. Specially if You know that the range is not needed anyhow. Normal room temperature will rarely get to 100°C for example.
  • Get more than one reading. It gives a better virtual resolution and fluctuations get smoothed out.
  • Have a small delay between readings. This keeps fluctuations down.
  • Test Your system before putting it into production. Theory might need to be adjusted to work in real world.

For my heater controller project I made this small function to read my LM35 sensors:


// read analog LM35 sensor
float LM35_temp(int pin){
  unsigned int tempread = 0;
  unsigned int intmp;
  float temp;
  int i;

  Serial.print("Reading analog temp on pin: ");
  Serial.println(pin);

  // first sample seems to fluctuate a lot. Disregard it
    intmp=analogRead(pin),
    Serial.println(intmp);
    delay(60);

  // 11 bit virtual resolution, according to http://www.atmel.com/dyn/resources/prod_documents/doc8003.pdf
  // arduino ADC is 10 bit real resolution
  // for 12 bit resolution 16 samples and >> 4 is needed
  for (i=0;i<=3;i++){
    intmp=analogRead(pin),
    tempread = tempread + intmp;  // read  more samples. better stability.
    Serial.println(intmp);
    delay(60);
  }
  tempread = tempread>>2;.

  // default voltage is 5.0,  analogReference(INTERNAL) sets it to 1.1.
  // temp = temp / 5.0;
  //temp = (5.0 * (float)tempread* 100.0)/1024.0;  //convert the analog data to temperature
  temp = 110 * (float)tempread / 1024; // analogReference(INTERNAL); needed
  Serial.print("Temp is: ");
  Serial.println(temp);
  return temp;
}// LM35

 

Also analogReference should be set, preferably in setup

// analog sensors reference.
// more precise but smaller range
analogReference(INTERNAL);

Writing code on web page is not the most effective way, so the same code can also be downloaded from here.

 

Leave a comment

Your email address will not be published. Required fields are marked *