Arduino Forum

Using Arduino => Microcontrollers => Topic started by: Nico_TheDecoder on Sep 13, 2020, 03:59 pm

Title: ATMEGA328P U internal temperature ?
Post by: Nico_TheDecoder on Sep 13, 2020, 03:59 pm
I recently bought 2 pcs. Arduino UNO (clone model) with this type of IC (ATmega328P-U).

And have discovered that both clone models (ATmega328P-U), can not read out internal temperature sensor correctly.

I used this code:
Code: [Select]

// Internal Temperature Sensor
// Example sketch for ATmega328 types.
//
// https://playground.arduino.cc/Main/InternalTemperatureSensor/
//
// April 2012, Arduino 1.0

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

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

void loop()
{
  // Show the temperature in degrees Celsius.
  Serial.println(GetTemp(),1);
  delay(1000);
}

double GetTemp(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;

  // The offset of 324.31 could be wrong. It is just an indication.
  t = (wADC - 324.31 ) / 1.22;

  // The returned temperature is in degrees Celsius.
  return (t);
}


My output from ATmega328P-U (Temp in °C):
Code: [Select]

15:51:22.045 -> 9.6
15:51:23.063 -> 9.6
15:51:24.081 -> 9.6
15:51:25.096 -> 9.6
15:51:26.113 -> 8.8
15:51:27.133 -> 9.6
15:51:28.185 -> 9.6
15:51:29.173 -> 9.6
15:51:30.222 -> 10.4
15:51:31.240 -> 10.4
15:51:32.257 -> 10.4
15:51:33.277 -> 10.4
15:51:34.297 -> 11.2
15:51:35.316 -> 11.2
15:51:36.332 -> 11.2
15:51:37.354 -> 11.2
15:51:38.372 -> 11.2
15:51:39.391 -> 11.2
15:51:40.412 -> 11.2
15:51:41.430 -> 11.2
15:51:42.450 -> 11.2
15:51:43.468 -> 14.5
15:51:44.487 -> 18.6
15:51:45.508 -> 23.5
15:51:46.527 -> 24.3
15:51:47.546 -> 23.5
15:51:48.564 -> 22.7
15:51:49.584 -> 21.9
15:51:50.603 -> 21.1
15:51:51.655 -> 21.1
15:51:52.672 -> 19.4
15:51:53.692 -> 19.4
15:51:54.712 -> 18.6
15:51:55.730 -> 17.8
15:51:56.724 -> 17.8
15:51:57.775 -> 17.0
15:51:58.794 -> 17.0
15:51:59.812 -> 16.1
15:52:00.832 -> 16.1
15:52:01.851 -> 16.1
15:52:02.871 -> 16.1
15:52:03.897 -> 15.3
15:52:04.915 -> 15.3
15:52:05.934 -> 15.3
15:52:06.953 -> 15.3
15:52:07.971 -> 14.5
15:52:08.992 -> 14.5
15:52:10.010 -> 12.9
15:52:11.030 -> 12.9
15:52:12.048 -> 12.9
15:52:13.068 -> 12.0
15:52:14.088 -> 12.0
15:52:15.104 -> 12.0
15:52:16.124 -> 11.2
15:52:17.141 -> 11.2
15:52:18.162 -> 11.2
15:52:19.181 -> 10.4
15:52:20.201 -> 10.4
15:52:21.222 -> 10.4
15:52:22.240 -> 10.4
15:52:23.260 -> 10.4
15:52:24.280 -> 10.4
15:52:25.329 -> 10.4
15:52:26.349 -> 10.4
15:52:27.368 -> 9.6
15:52:28.386 -> 10.4
15:52:29.406 -> 9.6
15:52:30.424 -> 9.6
15:52:31.441 -> 9.6
15:52:32.460 -> 9.6
15:52:33.478 -> 9.6
15:52:34.498 -> 9.6
15:52:35.516 -> 9.6
15:52:36.535 -> 9.6
15:52:37.555 -> 9.6
15:52:38.576 -> 9.6
15:52:39.597 -> 9.6
15:52:40.615 -> 9.6
15:52:41.635 -> 8.8
15:52:42.651 -> 9.6
15:52:43.676 -> 9.6
15:52:44.694 -> 9.6
15:52:45.712 -> 9.6
15:52:46.733 -> 9.6
15:52:47.748 -> 9.6
15:52:48.766 -> 9.6


The temperature fluctuation is made by heating the IC with a lighter for a few seconds  :smiley-twist:



However, if I use ATmega328P-PU, then the reading of internal temperature sensor works perfectly.

It seems to me strange that ATmega328P-U, can not read out internal temperature sensor correctly.

Do others have this experience?
Title: Re: ATMEGA328P U internal temperature ?
Post by: UKHeliBob on Sep 13, 2020, 05:19 pm
Split from an older topic
Title: Re: ATMEGA328P U internal temperature ?
Post by: jremington on Sep 17, 2020, 03:21 am
Needs calibration. The error spec is +/- 10 degrees C, I think. 
Title: Re: ATMEGA328P U internal temperature ?
Post by: srnet on Sep 17, 2020, 08:41 am
You have not actually told us what you mean by an 'incorrect' or a 'perfect' reading.

However as already mentioned, the ATmega datasheet specifies an accuracy of +/- 10C.