I've wired the sensor as shown in TI's specification sheet page 14 Figure 13. The problem occurs with brand new sensors, two bought via Digikey, three bouth via Farnell.
My Metro Mini is powered from an external PSU and outputs a stable 5.033V to sensor Vin measured with my multimeter. The sensor Vout goes to pin A0 and ground to the Metro Mini GND pin.
Using the Wawa sketch, I obtain 23.1°C
Using the TI for Arduino sketch, I obtain 23.8°C
Using my multimeter on the sensor, I obtain 249.7mV
The room temperature measured with a FLUKE 80TK is 21.8°C
Where do all these differences arise and what should I do to obtain a temperature close to the 21.8°C measured?
Thank you for some advice!
Wawa sketch
// connect LM35 to 5volt A0 and ground
// calibrate temp by changing the last digit(s) of "0.1039"
const byte tempPin = A0;
float calibration = 0.1039;
float tempC; // Celcius
float tempF; // Fahrenheit
void setup() {
Serial.begin(9600);
analogReference(INTERNAL); // use internal 1.1volt Aref
// change INTERNAL to INTERNAL1V1 for a Mega
}
void loop() {
tempC = analogRead(tempPin) * calibration; // get temp
tempF = tempC * 1.8 + 32.0; // C to F
Serial.print("Temperature is ");
Serial.print(tempC, 1); // one decimal place resolution is all you get
Serial.print(" Celcius ");
Serial.print(tempF, 1);
Serial.println(" Fahrenheit");
delay(1000); // use a non-blocking delay when combined with other code
}
TI sketch
#define ADCPIN 0
//Initialize variable to hold temperature
double temperature = 0;
//Setup Block, This code runs just once
void setup()
{
//Switch to Internal 1.1V Reference
analogReference(INTERNAL);
Serial.begin(9600);
}
//Loop Block, this code runs continously
void loop()
{
//Read ADC,
temperature = analogRead(ADCPIN);
Serial.print(temperature);
//Calculate Temperature from ADC value
//Note that we use mV for Vref
//Vin = ADCresult*Vref/(2^10)
//Temp(C) = Vin/(10) = ADCresult*Vref/(1024*10)
temperature = temperature * 1100 / (1024 * 10);
Serial.println(temperature, 1);
//Wait 1 second for the next reading
delay(1000);
}
The Fluke datasheet says the basic accuracy is +- 2 deg C which is much worse than the LM35 and probably much worse than a cheap wall thermometer bought from a hardware store.
So the actual temperature is more likely to come from the LM35.
As written, both sketches will give the wrong value because you first need to know the true value of the Arduino internal voltage reference which is why you need to go through a calibration step using a temperature or voltage reference. Once you update the sketches with the true values they should give near identical numbers.
I also have an Oregon Scientific room thermometer here and that shows 21.6°C, and there's a built-in room thermostat that shows 21.9°C (higher up on the wall), so my set-up is well off.
How would I do what you describe above? Like, finding the true value of the Arduino internal voltage reference? How would I then use that true value in my sketch?
Okay, I did just that. Used a FLUKE 177, which is all I have. I obtained 1.084V so I am using 1084 in the TI sketch.
This
temperature = analogRead(ADCPIN);
Serial.print(temperature);
yields 246
and this
temperature = temperature * 1084 / (1024 * 10);
Serial.println(temperature, 1);
yields 26.0
Using my multimeter on the sensor, I obtain 262.3mV
So, something is still off here...
PS: With this, but removing the RC component from the specification sheet page 14 Figure 13, I obtain 227 and 24.0 so that is two degrees lower, at a room temperature of 21.8°C which is still 2.2°C too high.
Change that number, and re-upload, until you get the temp you want on the display.
From the displayed temp and Fluke voltage I calculated that it should be about 0.1123
The Fluke temp seems to be off.
Note that other hardware could also influence temps.
You must use a dedicated ground wire (not shared) to the Arduino for the LM35.
Leo..
Right, with your sketch and 0.1123 I obtain 24.8°C at a room temperature of 21.8°C (wall thermostat figure, Oregon Scientific tabletop thermometer near the breadboard with the LM35CZ. The LM's ground wire goes to the Metro Mini's GND pin.
With 0.096 in your sketch, I obtain 21.6°C which is very close.
However, whence that "magic number" 0.096? Is this the right way to do it - meaning, just futzing around with that number until the output is close to two independent "better" thermometers?
When I now slowly breathe on the sensor almost holding it into my mouth or squeeze it tight between thumb and index finger, I obtain 28.7°C.
Getting the temperature of the LM35 (including the conection wires) close to the temperature of the object/air you want to measure is not easy.
I think factories calibrate in close proximity of a reference in a non-conducting liquid (transformer-oil bath).
Voltage on the sensor /10 is the factory calibration of the LM35 (but it might be a fake/uncalibrated one).
No need to experiment with that 0.1039 factor.
(displayed temp - real temp) * 0.1039 = new factor. That will get you very close.
Leo..
I fetched another thermometer and placed it away from my radiating body near my set-up and finally settled on a factor of 0.1 that over night saw the set-up's displayed temperature following the thermometer's temperature fairly faithfully, which is sufficient for my use-case.
It's just that I don't like magic numbers, but, well, it works, reproducibly ; )