# Arduino Internal Temperature Measuring

Send article as PDF

The code below, somewhere, shows how to measure the temperature of the ATmega328 microcontroller built in to numerous Arduino boards. You can find all the gory details in my new book Arduino Software Internals available from Apress, Amazon and good bookshops everywhere.

Apress.com: https://www.apress.com/gb/book/9781484257890

### The Warning

Do not use this code if your Arduino has the AREF pin connected to any voltage source. When you enable the 1.1v bandgap voltage as the ADC reference, it goes Bzzzt! And all the magic blue smoke gets out! The only thing connected to AREF should be a 100nF capacitor, to ground. (Already built in on Arduino boards.)

### The Listing

``````/*
* ARDIUNO - measure the internal temperature of the
* AVR ATmega328P using the ADC internal temperature
* input. See the data sheet for details.
*
* (C) Norman Dunbar, July 21 2018.
*
* This code is in the public domain - use it and abuse
* it as you wish! It is based, but not copied, from
* an Atmel example found at:
*
*
*/

void setup() {
// Initialise the ADC to use the
// internal 1.1V reference voltage.
ADMUX = (1 << REFS0) | (1 << REFS1);

// Use the ADC multiplexer input
// number 8, the temperature sensor.

// Slow the ADC clock down to 125 KHz
// by dividing by 128. Assumes that the
// standard Arduino 16 MHz clock is in use.

// Non-standard 8MHz clock in use.

// it is always 351 on my device.

// Use the Serial monitor for output.
Serial.begin(9600);
Serial.println("Arduino Internal Temperature");
}

// start another before ruturning the current reading.
; // Just wait for ADC to finish.
}

return result;
}

//-----------------------------------------------------------------
// There are many ways, it seems, to calculate the degrees C from
// from. Here are some:
//
// ADC - some random offset;
// (((ADC - (273 - 100 - TS_OFFSET)) * 128) / TS_GAIN) + 25.
// (ADC - 324.31) / 1.22
//
// I'm using the last one, as it's the one closest to my actual
// temperature measurements.
//-----------------------------------------------------------------

void loop() {

// better accuracy.

for (uint8_t x = 1; x < 101; x++) {

// Uncomment if you want a running commentary!
/*
Serial.print(" ");
Serial.print(" ");
*/

}

float degreesC = (ADCAverage - 324.31) / 1.22;
Serial.print(degreesC);
Serial.print("C, ");

// Convert to Fahrenheit. C * 1.8 + 32.
Serial.print(degreesC * 1.8 + 32);
Serial.println("F.");

// Delay a second more between readings.
delay(1000);
}
``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.