Use the Arduino internal voltage reference to measure battery level.
Step 1: How to Use the Arduino Internal Voltage Generator.
Arduino boards have an internal voltage generator. With it we can measure the battery level on a device.
- We know the maximum voltage of the battery we want to measure.
- With a voltage divider we lower the maximum voltage to a reference voltage. A voltage divider is a circuit that scales voltage through a pair of resistors in series. When we take the output voltage (v_out) between the resistors we obtain a fraction of the input voltage (v_in).
- This voltage should not change and in this case the reference will be the 1.1 V that the internal generator of Arduino UNO can give.
- The Arduino Mega has a 1.1 V internal generator and another of 2.56 V.
- Once we have lowered the maximum voltage to the reference voltage we configure the analog-to-digital converter (ADC) to use the internal voltage generator as reference. We do this with the instruction analogReference():
If we write INTERNAL as the argument of analogReference() the ADC takes the 1.1 V of the internal generator of the Arduino UNO as reference. Other instructions to analogReference() are:
DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards).
INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 or ATmega34U4 (not available on the Arduino Mega).
INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only).
INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only).
EXTERNAL: the voltage applied to the AREF (analog reference) (a pin (0 to 5V only) is used as the reference.
Done! Comparing the device battery voltage with the reference voltage we can measure the battery level.