Language: EN

usar-el-sensor-de-temperatura-interno-de-arduino

Using the Internal Temperature Sensor of Arduino

A little-known fact about Arduino is that most models have an internal temperature sensor in the processor that we can use to make measurements without the need for external sensors.

The processors that incorporate this sensor are the ATmega 168A, 168P, 328, and 32P (Arduino Uno and Nano), and the Atmega32U4 (Arduino Leonardo). On the other hand, this sensor is not available in ATMega2560 (Arduino Mega).

The accuracy of this sensor is limited, as it is located inside the processor and is affected by its heat. This temperature increase will be greater the higher the processor load and the current flowing through the outputs.

With some calibration, the internal Arduino sensor can provide an approximate measurement of the ambient temperature with an accuracy of 2ºC, provided that the processor has been off for at least 10 minutes.

Despite its limitations, the temperature sensor is interesting because it allows measurements to be made without using external devices. For example, it is useful for determining the state of the processor, in quick tests on a board, or even in temperature measurement projects that require low precision and long periods between measurements.

The code we need to make the measurement is as follows:

void setup()
{
  Serial.begin(9600);

  Serial.println(F("Internal Temperature Sensor"));
}

void loop()
{
  Serial.println(GetTemp(), 1);
  delay(1000);
}

double GetTemp(void)
{
  unsigned int wADC;
  double t;

  ADMUX = (_BV(REFS1) | _BV(REFS0) | _BV(MUX3));
  ADCSRA |= _BV(ADEN);
  delay(20);
  ADCSRA |= _BV(ADSC);
  while (bit_is_set(ADCSRA, ADSC));
  wADC = ADCW;

  // This is the function to be calibrated.
  t = (wADC - 324.31) / 1.22;
  return (t);
}

If you want to calibrate the sensor, you have to take several measurements, leave the sensor off for 30 minutes, compare the measurements with those of a calibrated sensor, and adjust the values 324.31 and 1.22 in the expression (but don’t expect miracles, as we said, the accuracy will always be limited).

Download the code

All the code from this post is available for download on Github. github-full