Skip to content

Instantly share code, notes, and snippets.

@mybigman
Forked from AndiSHFR/GetOnboardTemperature.c
Created May 3, 2019 13:16
Show Gist options
  • Save mybigman/806158df0abc90faed212a687509b32e to your computer and use it in GitHub Desktop.
Save mybigman/806158df0abc90faed212a687509b32e to your computer and use it in GitHub Desktop.
Read internal ATmega cpu temperature sensor.
/**
* GetOnboardTemperature
*
* Read temperature from the onbaord sensor. Check specs if sensor exist for your cpu. ;-)
*
* Example:
* char buff[10];
* Serial.print("CPU temp.: " );
* Serial.print(dtostrf(GetOnboardTemperature(), 5, 2, buff));
* Serial.print(" °C\r\n" );
*
*/
double GetOnboardTemperature(void) {
unsigned int wADC;
double t;
// 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
while (bit_is_set(ADCSRA,ADSC));
// Reading register "ADCW" takes care of how to read ADCL and ADCH.
wADC = ADCW;
// You may need to adjust the offset 324.31 acording to your system.
t = (wADC - 324.31 ) / 1.22;
// The returned temperature is in degrees Celcius.
return (t);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment