Making accurate analog measurements and detecting power fails

This tutorial covers:

  • measuring the internal 1.1V reference voltage
  • measuring the microcontrollers 5-volt power supply
  • demonstrating a method of reacting to a power failure
  • making accurate analog input measurements

We can calculate the voltage of an analog input with:
Vinput = reading * Vcc / 1024
Let’s say our analog reading was 512 and Vcc was 5000mv
Vinput = 512 * 5000mv / 1024 = 2500mv

The accuracy of analog i/p readings is dependent on the correct value for Vcc.
The problem is, Vcc may change slightly.
If we measure Vcc prior to calculating our analog input voltage, we can be more accurate in our calculations.
We can use the controller’s internal 1.1V voltage reference to calculate Vcc.
However, we must first know the true value of the 1.1V reference.

Attached are: 
A2DRead_1.1VBG.ino      (calculate the actual value for the 1.1V reference)
A2DSecretvoltmeter.ino  (measure the UNO’s Vcc)
Power_fail.ino          (Vcc is monitored to see if there is a power fail, if so, save some stuff to EEPROM)
A2DaccurateReadings.ino (Make an accurate calculation of the voltage on the A0 pin)

A2DRead_1.1VBG.ino (3.88 KB)

A2DSecretVoltmeter.ino (3.62 KB)

Power_fail.ino (5.51 KB)

A2DacuracteReadings.ino (4.97 KB)

In case I need this.

larryd:
However, we must first know the true value of the 1.1V reference.

Some processors under some conditions have the 1.1 V reference present on the AREF pin where it can be directly measured.

As in your sketch:
https://forum.arduino.cc/index.php?topic=38119.msg282514#msg282514

Exactly!

Another hint: only a single byte of EEPROM is needed to store a value for the internal reference voltage (0 to 200).