Some AVR devices have an internal temperature sensor. It can be used to measure the core temperature of the device (not the ambient temperature around the device). The measured voltage has a linear relationship to the temperature. The voltage sensitivity is approximately 1 mV/°C, the accuracy of the temperature measurement is ±10°C.
The temperature measurement is based on the on-chip temperature sensor that is coupled to a single ended ADC channel. Selecting ADC channel 8 by writing '1000' to ADMUX.MUX[3:0] enables the temperature sensor. The internal 1.1 V voltage reference must also be selected for the ADC voltage reference source. When the temperature sensor is enabled, the ADC converter can be used in single conversion mode to measure the voltage over the temperature sensor.
Sample Measurement Data
Calibration
The results from temperature measurements have offset and gain errors. The internal temperature reference can be corrected for these errors by making calibration measurements at one or two known temperatures and adjusting the output values. This can result in very precise temperature measurements, sometimes as accurate as ±2°C.
More detail can be found in this application note.
Configuring the ADC
The internal 1.1 V voltage reference must be selected for the ADC voltage reference source when using the internal temperature sensor. Writing “11” to the REFS1 and REFS0 bits of the ADMUX register selects the internal 1.1 V Voltage Reference.
The ADC has multiple input channels and modes of operation. The Single Conversion mode can be used to convert the temperature sensor signal connected to channel 8. To select channel 8, writing “1000” to the MUX3 thru MUX0 bits selects channel 8 or the temperature sensor.
Once the conversion is complete, the result is stored in two 8-bit ADC data registers ADCH (higher 8-bits) and ADCL (lower 8-bits). The 10-bit result can be either left justified or right justified. If ADLAR bit is set to a "1", then the result is left adjusted to the upper 10-bits of the two registers. If set to "0" then the result occupies the lower 10 bits of the two registers. By default, each bit is cleared and the word is right justified.
This code statement will set the bits as described.
ADMUX = (1«REFS1) | (1«REFS0) | (0«ADLR) | (1«MUX3) | (0«MUX2) | (0«MUX1) | (0«MUX0);
Configuring the ADC Clock and Conversion Timing
The ADC can prescale the system clock to provide an ADC clock that is between 50 kHz and 200 kHz to get maximum resolution. If an ADC resolution of less than 10-bits is required, then the ADC clock frequency can be higher than 200 kHz. At 1 MHz it is possible to achieve up to eight bits of resolution.
The prescaler value is selected with ADPS bits in ADCSRA Register. For example; writing “110” to the ADCSRA register selects the divide by 64 pre-scaler resulting in a 125 KHz ADC clock when an 8 MHz oscillator clock is used.
ADC Control and Status Register A
Starting a Conversion
In single conversion mode the ADSC bit in the ADCSRA register must be set to a logical one state to start the ADC conversion. This bit remains at logic high while the conversion is in progress and is cleared by the hardware, once the conversion is complete.
The first conversion after the ADC is switched on takes 25 ADC clock cycles in order to initialize the analog circuitry. Then, for further conversions, it takes 13 ADC clock cycles (13.5 for Auto-triggered conversions).
Sample Project
A sample project for using the Temperature Sensor is available here.