Hidden Response Accuracy in the ATMEGA328P
UPDATED – See Below
Some people may have known already about the temperature sensor built into the ATMEGA8, 168, 328 series microcontrollers, but the most serious enthusiast would write off the 1°C resolution as just a coarse sensor of no practical use. I would have to say I was part of that group, until I compared the response between the two sensors.
The TI LM35DZ is a simple temperature sensor that has an accuracy of +/-0.5°C at 25°C and with 0.08°C of self heating, this should be sufficient to demonstrate the rough nature of the ATMEGA328’s internal temperature when compared to dedicated external sensor. The TO-92’s have the best Thermal Reponses on the datasheets and are the cheapest. Connection is straight-forward, +5VDC, Vout and Gnd with Vout to the ATMEGA328’s PC0 (Arduino pin A0).
For the ATMEGA328 internal sensor, adjustment of the voltage offset can be used to calibrate to another sensor.
The Arduino IDE sketch is as follows:
//Internal and External Temperature Sensor Sketch
Serial.println(F(“Internal Temperature Sensor”));
// Show the temperature in degrees Celcius.
unsigned int wADC;
// The internal temperature has to be used
// with the internal reference of 1.1V.
// Channel 8 can not be selected with
// the analogRead function yet.
// Set the internal reference and mux.
ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
ADCSRA |= _BV(ADEN); // enable the ADC
delay(20); // wait for voltages to become stable.
ADCSRA |= _BV(ADSC); // Start the ADC
// Detect end-of-conversion
// Reading register “ADCW” takes care of how to read ADCL and ADCH.
wADC = ADCW;
// The offset of 324.31 could be wrong. It is just an indication.
t = (wADC – 326.31 ) / 1.22;
// The returned temperature is in degrees Celcius.
val = analogRead(0);
sample_avg = sample_avg + (val – sample_avg) / sample_count;
temp=(1.1 * sample_avg * 100.0) / 1024.0;
Both devices were within an inch or two and subjected to a 40 watt clip lamp for heating and a small space fan for cooling. The response curve is as follows (one sample per second):
While the precision of the ATMEGA328 internal sensor is to be desired, the temperature response is quite dramatic.
The LM35DZ’s cooling response of 1°C per 10 minutes could be an issue with some projects.
Update 21 Jan 14:
Scott Teal brought up an issue concerning the smoothing algorithm. I retested including a no smoothing sample and the results for the LM35DZ are much better. With a slope correction factor of the ATmega328P I believe the LM35DZ will correlate very well.
Update 2 21Jan14:
The ATmega328 correlation test is as follows:
Update 3 – 22Jan14:
Little more tuning and the Ring Buffer Included…
Pretty good match!