Sunday, February 19, 2017

AVR internal temperature sensor interface library | ATmega328P

A few AVR microcontrollers have internal on-chip temperature sensors. The sensor is a diode that produces a temperature dependent voltage. This voltage is measured with the ADC. According to Atmel the typical accuracy of temperature measurements over the full temperature range of the AVR is ±10°C but after calibration the accuracy can be ±1°C over 20°C to 55°C using one-point calibration.


The calibration can be done using one-point calibration and compensation or two-point calibration and compensation. One-point calibration is the easiest - subtract the ambient

temperature from the AVR sensor temperature. In the case of two-point calibration you need to have a controlled temperature environment and use a special equation. For more details visit the Atmel's application note AVR122: Calibration of the AVR's Internal Temperature Reference.

When calibrating the temperature, find the offset error using the MCU value when you first start it because after that it will increase (by 2°C in my case), depending on how much work the MCU is doing. If the CPU clock is more than 1MHz the ADC Noise Reduction Mode should be used for more accurate results.


The internal chip temperature sensor is not made for measuring ambient temperature because the sensor is not very accurate and the temperature inside it increases when the MCU is operating. The on-chip temperature sensor can be used as a safety measure to monitor the chip temperature and if it's increasing to much turn off the MCU or some outputs, or monitor the temperature inside of the project enclosure.

innerAVRtemperature - ATmega328P temperature sensor library

The innerAVRtemperature library is written for ATmega328P microcontroller but can be easily modified for use with other AVR MCU's.

For better results this library takes n number of samples and averages the results. n is defined by CHIP_TEMP_NR_OF_SAMPLES.

To reduce the noise from the CPU affecting the ADC the ADC Noise Reduction Sleep Mode is implemented. During the ADC measurement the CPU is halted and when the conversion is done the CPU is woke up and the ISR is executed. The ISR is empty and the function remains in a while loop until the conversion is done for easier interface of the library with the user's code.

The CPU frequencies supported are 1MHz, 8MHz and 16MHz but others can be easily added.

To set the offset gain calibration in degrees Celsius use CHIP_TEMP_ERROR_OFFSET. By default the value is -52°C.

The global variable ADCisReadingTemp indicates an active reading. The function  ReadMCUTemp is looping 100 times to read the ADC and if in that time you have an interrupt that changes the ADC registers you should check this variable and if it's 1 wait until it reads 0 to prevent interfering with the function. The ADC registers are setup every time the function is used but not during the loop.

int8_t ReadMCUTemp(void)

This function returns the microcontroller's average temperature in degrees celsius. The number of samples are defined by CHIP_TEMP_NR_OF_SAMPLES.

Example code:
#include <avr/io.h>
#include <util/delay.h>
#include "OnLCDLib v1.3.h"
#include "innerAVRtemperature v1.0.h"


int main(void){
    // Initialise the LCD

        LCDWriteInt(ReadMCUTemp(), 2);


innerAVRtemperature v1.0.h

No comments:

Post a Comment