Hello everyone, I am playing with the TMP36 that I received today and I can happily get temp readings from my Duemilanove to 2 dec. places. Individual readings are very variable though. Like, +/-2 degrees! The sensor is just sat on a table or, now, on the duvet and so I find this most unsatisfactory.
I dug around for some other sketches and found some code where the guy polls 8 readings and takes the mean, which sounds like it should improve matters. However, his code only uses integers, which kind of defeats the objective of improving precision. I bastardised his sketch with a few others (LadyAda's, PlanetArduino, this) and thought I'd managed to calculate a mean from multiple floating points but what I've got don't seem much of an improvement. I get numbers with decimal places but little variation, like these:
14.45 , 14.45 , 14.94 , 14.45 , 14.45 , 14.45 , 15.92 , 15.43 , 15.43 , 14.94 , 14.45 , 15.43 , 14.94 , 14.45 , 14.45 , 14.94 , 13.96 , 13.96 , 14.94 , 14.45 , 14.94 , 14.45 , 14.94 , 14.45 , 14.94 , 14.94 , 15.43 , 14.94 , 14.45 , 14.94 , 14.45 , 14.94 , 14.94 , 14.45 , 14.94 , 14.45 , 14.45 , 14.94
Here's the code:
/*
An open-source LM36 Temperature Sensor for Arduino. This sketch was ninja'd on the 26-08-11 by Ninja Chris. Fear My Badger.
http://creativecommons.org/license/cc-gpl
*/
int sensorPin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade with a
//500 mV offset to allow for negative temperatures
int mean;
int tempc = 0; // temperature variables
int samples[8]; // variables to make a better precision
float maxi = -100,mini = 100; // to start max/min temperature
int i;
void setup()
{
Serial.begin(9600); // start serial communication
}
void loop()
{
for(i=0; i<=7; i++){ // gets 8 samples of temperature
//getting the voltage reading from the temperature sensor
int reading = analogRead(sensorPin);
samples[i] = reading;
mean = mean + samples[i];
delay(100);
}
mean = mean/8.0; // better precision
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = mean * 5.0;
voltage /= 1024.0;
// now print out the temperature
float tempc = (voltage - 0.5) * 100.0 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
Serial.print(tempc,2);
Serial.print(" Celsius, ");
Serial.print(maxi,2);
Serial.print(" Max, ");
Serial.print(mini,2);
Serial.println(" Min");
mean = 0;
delay(800); // delay before loop
}
If anyone can help me out with getting nice, accurate numbers out of this widget I would be rather grateful.
As an aside, can anyone advise how to add a time stamp to each value in the serial monitor or to set the sample rate to exactly 1Hz so that I can produce accurate time-series by simply recording the start time? I do have a sensor shield with a RTC in the post but I was wondering if this isn't possible with just the basic setup?
Cheers,
NC