ATMEGA Core Temperature Sensor

Summary of ATMEGA Core Temperature Sensor


This article details a project using the ATMEGA32u4 microcontroller's internal temperature sensor to estimate ambient room temperature. The author configured the ADC with a 2.56V reference and multiplexer settings specific to the ATMEGA32u4, noting that while Arduino Leonardo and Pro Micro boards work directly, other AVR chips require register adjustments.

Parts used in Einstepper Project:

  • ATMEGA32u4 microcontroller
  • Einstepper Project board
  • Arduino Leonardo
  • Arduino Pro Micro

Which parts have the channel

The ATmega48A/88A/168A/328/328P family lists “Temperature Measurement” in its feature summary, with the mechanism in the ADC chapter: the sensor is wired to a single-ended channel called ADC8, one past the last pin channel. Writing MUX3..0 = 1000 in ADMUX connects it, and the footnote on that table row reads “For Temperature Sensor”. Rows above are pin channels, rows below are reserved: the signature of a spare multiplexer input, not an analogue pin on a lead.

Older parts do not have one. The ATmega16/ATmega32 multiplexer covers ADC0–ADC7 plus differential pairs, with no temperature entry; every “temperature” hit there is an operating-limit table. The ATmega16U4/32U4 has a sensor, described as an “on-chip calibrated temperature sensor”, but the datasheet’s stated use for it is not ambient sensing — it is correcting the internal RC oscillator, in section 24.6.1, “Sensor Calibration”. tinyAVR 0/1-series parts store per-device corrections in the signature row: SIGROW.TEMPSENSE0 for gain, TEMPSENSE1 for offset. Read the multiplexer table, not the feature bullet.

Selecting it through the multiplexer

Two fields must agree. REFS1..0 = 11 selects the internal 1.1 V reference, which the datasheet requires for this measurement, and MUX3..0 = 1000 selects the sensor. Together that is ADMUX = 0xC8. The ADC clock must sit between 50 kHz and 200 kHz for full 10-bit resolution, so at 16 MHz the prescaler divides by 128 to give 125 kHz.

ADMUX  = (1<<REFS1)|(1<<REFS0)|(1<<MUX3);   /* 0xC8: 1.1V ref, ADC8 */
ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
ADCSRA |= (1<<ADSC);
while (ADCSRA & (1<<ADSC)) ;
t = ADC;   /* read ADCL first: it locks ADCH */

A normal conversion takes 13 ADC clock cycles; the first after ADEN is set takes 25. The sensor is connected only while its channel is selected, so it costs nothing in a design that never asks for it.

What the number means, and why the spread is wide

The output is a voltage with a near-linear temperature term. The 2014 revision of the datasheet tabulates 242 mV at −45 °C, 314 mV at +25 °C and 380 mV at +85 °C, a sensitivity near 1 mV/°C. The current revision restates the same curve in ADC counts — 205, 270 and 350 LSB at −40, +25 and +105 °C — because one LSB is about 1.07 mV and the whole operating range spans roughly 140 codes.

The stated accuracy is ±10 °C, and it is not a rounding error. The sensor is an untrimmed junction voltage on the die: offset and slope both move with process, which the datasheet says plainly — “due to the process variation the temperature sensor output voltage varies from one chip to another”. Two further errors sit outside its model. The sensor measures the die, not the board, and the die runs above ambient by an amount set by dissipation that the datasheet does not characterise. The 1.1 V reference it is measured against has its own tolerance, which scales every reading.

Calibration against a known temperature

One reading at a known temperature gives the offset. The datasheet’s correction is T = (ADC − T_OS) / k, where k is a fixed coefficient and T_OS the offset determined at production test. One point fixes T_OS and leaves k nominal; two points fix both, and the current revision notes that two temperature points give better accuracy than one. Store the result per board in EEPROM — the offset belongs to the chip, not to the firmware image.

What calibration cannot fix is resolution. Averaging a thousand samples removes random noise and leaves offset, slope and self-heating untouched.

Reading it without disturbing the application

The ADC shares supply and ground with the core, so a conversion taken while the CPU and I/O switch measures their noise too. ADC Noise Reduction mode is the mechanism against that: writing SM2..0 = 001 and executing SLEEP halts clkI/O, clkCPU and clkFLASH while the ADC keeps running, and if the ADC is enabled a conversion starts on entry. It wakes on the conversion-complete interrupt: one conversion time lost, no busy-wait.

Two details cost readings if skipped. The first conversion after enabling the ADC is a throwaway: 25 clock cycles, and the datasheet warns that with the bandgap reference “it will take a certain time for the voltage to stabilize”, so the first value may be wrong. And the ADC should not be left enabled between measurements — ADEN keeps the prescaler and analogue front end clocked.

What it is actually good for

Overtemperature protection, and anything else where the absolute number does not matter. A board that folds back a motor driver at 85 °C die temperature, or logs a thermal trend, needs a repeatable relative measurement with a fixed threshold — exactly what a calibrated offset gives. The 32U4 case is the strongest: the RC oscillator that clocks USB drifts with temperature, and the sensor sits on the same die, so it tracks the thing that is drifting.

Ambient measurement is the case it fails. The die is warmer than the air, the offset is uncalibrated on a bare board, and ±10 °C is optimistic once the part is driving loads. A sensor on a lead, or a digital part on the I²C bus, costs less than the calibration effort and measures the right object.

Everything above is from the documents listed below; nothing was measured here. A builder should read offset and slope from their own part before trusting a threshold.

References

  • Microchip, ATmega48A/PA/88A/PA/168A/PA/328/P Datasheet, Atmel-8271I — §24.8, §24.9.1, §10.4.
  • Microchip, ATmega328/P Datasheet, DS40001984A — §28.8, temperature in LSB.
  • Microchip, ATmega16U4/32U4 Datasheet, Atmel-7766 — §24.6.1 Sensor Calibration.
  • Microchip, megaAVR 0-series / tinyAVR 1-series Datasheet — §30.3.2.6, SIGROW.TEMPSENSE0/1.

Quick Solutions to Questions related to Einstepper Project:

  • Can the ATMEGA32u4 internal temperature sensor be used to detect room temperature?
    Yes, by using a known temperature differential between the ambient air and the core, it is possible to calculate the ambient temperature.
  • How do you set up the ADC to read the internal temperature sensor?
    You must set the ADC reference to the 2.56V internal reference, set the multiplexer to the temperature sensor, and enable the ADC.
  • Which Arduino boards use the ATMEGA32u4 chip?
    The Arduino Leonardo and Arduino Pro Micro both use the ATMEGA32u4, allowing the provided code to work without modification.
  • Do all megaAVR devices contain an internal temperature sensor?
    No, only specific devices listed in Atmel’s product finder contain this feature, including various ATmega and AT90 models.
  • What happens if I use a different chipset on an AVR board?
    Minor modifications to the registers will be necessary because the configuration code is specifically for the ATMEGA32u4.
  • Does the package type affect the results of this project?
    Yes, there may be varying thermal characteristics between different packages such as TQFP versus QFN.
  • Where can I find the list of supported microcontrollers?
    The list of supported devices can be found according to Atmel’s product finder.
  • Is the provided code applicable to all AVR boards?
    No, the code is specifically for the ATMEGA32u4, and users should check the datasheet for correct register values on different chips.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top