# Measure ATmega168PA internal temperature while using other ADC channels at 5V reference

I have a device that continuously measures the value of 3 sensors connected to 3 ADC pins and working at 5V reference, so I have two options for voltage reference selection:

1. Connecting AREF pin to 5V supply (REFS=00)
2. Connecting AREF to a 100nF capacitor and choose AVcc as reference (REFS=01)

Till here everything works fine. I also want to measure chip temperature using the internal temperature sensor (ADC8), and as the datasheet says I must select the 1.1V internal voltage reference, so the first above options can not be used, If I choose the second option selecting the 1.1V reference while the capacitor is charged with 5V (due to other pins measurements) and again selecting back AVcc reference doesn't cause error in ADC measurement? I decided to read the internal temperature sensor without changing the reference voltage to 1.1V. So I read 50 samples and divided the sum to 11 (because I used 5V reference instead of 1.1V), but the result is about 0x170 (~76 deg C) that is incorrect.
Anyone can solve my problem?

As I described in the question changing the ADC reference voltage can cause other problems. Is it really necessary to change it? What happens if I read the internal temperature sensor with a 5V reference instead of 1.1V? I tested it by taking 50 samples at 1.1V reference converting it to millivolts (SamplesSum * 1100 / 50 / 1023) one time and taking 50 samples at 5V reference converting it to millivolts (SamplesSum * 5000 / 50 / 1023) another time. The result is the same and there has been just a little error because measuring with 1.1V reference is more accurate than 5V reference that it can be easily corrected by the calibration value.
And finally, to make the code simpler and using only a 16-bit integer to store values, I took 44 samples and divided the sum of all samples to 9 (SamplesSum * 5000 / 44 / 1023 = SamplesSum / 9). The result for my MCU is the temperature in degrees Celcius + 380 (My MCU calibration value is 380 - 273 = 107). I took 44 samples because the mentioned calculations have the lowest integer rounding error.