Temperature Measurement
Post date: May 03, 2015 1:32:48 AM
Components:
Arduino Uno
Breadboard
Temp sensor LM35
Temp sensor TMP 36
BreadBoard:
Schematic:
Formula:
LM35: Temp in °C = (Vout in mV) / 10=((3300/1024)*Reading)/10
TMP36: Temp in °C = [(Vout in mV) - 500] / 10=[(3300/1024)) - 500] / 10
Code
#define aref_voltage 3.3 // we tie 3.3V to ARef and measure it with a multimeter!
int tempSensor1 = A0; // Define the pin of the LM35 sensor
int tempSensor2 = A2; //define the pin of the tmp36
const int numReadings = 100; // number of samples for temp reading
int tempReadings[numReadings]; // the readings from the lm35
int index = 0; // the index of the current reading
long total = 0; // the running total
int averageTemp = 0; // the average temp over 100 sample
int Max1 = 0; //Temp max reading
int Min1 = 1023; //temp min reading
int Max2 = 0;
int Min2 = 1023;
int tempReadings2[numReadings]; // the readings from the tmp36
int index2 = 0; // the index of the current reading
long total2 = 0; // the running total
int averageTemp2 = 0; // the average
float temp1 = 0; //temp LM35 in celsius
float temp2 = 0; //temp TMP36 in celsius
float tempMax1 = 0; //Temp max LM35 in celsius
float tempMin1 = 0; //Temp min LM35 in celsius
float tempMax2 = 0; //Temp max TMP36 in celsius
float tempMin2 = 0; //temp min TMP36 in celsius
void setup() {
Serial.begin(9600); //init serial port for printing
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
tempReadings[thisReading] = 0;
tempReadings2[thisReading] = 0;
}
analogReference(EXTERNAL); //setup the ref voltage of the ADC to external
}
void loop() {
readingTemp1 (); //call the function to read sensor LM35
readingTemp2(); //call the function to read sensor TMP36
// Print result on serial port
Serial.print("Sensor1:");
Serial.print (temp1);
Serial.print("Max=");
Serial.print(tempMax1);
Serial.print("Min=");
Serial.println(tempMin1);
Serial.print("Sensor2:");
Serial.print (temp2);
Serial.print("Max=");
Serial.print(tempMax2);
Serial.print("Min=");
Serial.println(tempMin2);
}
int readingTemp1 () {
// subtract the last reading:
total = total - tempReadings[index];
// read from the sensor:
tempReadings[index] = analogRead(tempSensor1);
// add the reading to the total:
total = total + tempReadings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
averageTemp = total / numReadings;
//record the temp max
if (averageTemp > Max1) {
Max1 = averageTemp;
}
//record the temp min
if (averageTemp < Min1 && averageTemp < Max1) {
Min1 = averageTemp;
}
// temp calculation in celsius for LM35
temp1 = ((averageTemp * aref_voltage) / 1024) * 100 ;
tempMax1 = ((Max1 * aref_voltage) / 1024) * 100;
tempMin1 = ((Min1 * aref_voltage) / 1024) * 100;
delay(10);
return temp1;
return tempMax1;
return tempMin1;
}
int readingTemp2 () {
// subtract the last reading:
total2 = total2 - tempReadings2[index2];
// read from the sensor:
tempReadings2[index2] = analogRead(tempSensor2);
// add the reading to the total:
total2 = total2 + tempReadings2[index2];
// advance to the next position in the array:
index2 = index2 + 1;
// if we're at the end of the array...
if (index2 >= numReadings)
// ...wrap around to the beginning:
index2 = 0;
// calculate the average:
averageTemp2 = total2 / numReadings;
//record temp max
if (averageTemp2 > Max2) {
Max2 = averageTemp2;
}
//record temp min
if (averageTemp2 < Min2 && averageTemp2 < Max2) {
Min2 = averageTemp2;
}
//temp calculation for TMP36
temp2 = (((averageTemp2 * aref_voltage) / 1024) - 0.5) * 100 ;
tempMax2 = (((Max2 * aref_voltage) / 1024) - 0.5) * 100;
tempMin2 = (((Min2 * aref_voltage) / 1024) - 0.5) * 100;
delay(10);
return temp2;
return tempMax2;
return tempMin2;
}
Conclusion:
There is a temp difference reading between the two sensors of 0.5 to 1 Celsius with an average of 0.7 Celsius. The LM35 reading is always higer than the TMP36. I have no idea where the difference come from. It can be corrected by adding an offset calibrated from an external temp reading.
The TMP36 is more stable than the LM35 or the LM35 more sensitive.
What did i learn from that sketch:
I have learned to calculate an average temperature from an array of 100 reading. Record the Max an Min temperature. I should delay the moment to start to record of max and min temp to avoid the initial filling time of the array used to average the temperature.