Skip to content

Calculating the battery voltage reading from Arduino's ADC

Alexandre Devienne edited this page Nov 1, 2016 · 1 revision

To get the battery voltage, we are reading from analog pin A0. The pin can only support up to 5V, so we are using a voltage divider:

  • R1 = 24kOhm, R2 = 12kOhm
  • Input voltage = Battery Voltage * R2/(R1+R2)

The ADC gives a analog readout AR from 0 to 1023, which corresponds to a voltage from 0 to 5V, then:

  • Battery voltage = 5 * AR/1024 * [(R1+R2)/R2]

To account for an offset and gain error of the ADC (see section 26.6.4 ADC Accuracy Definitions in the ATMEL datasheet), we measured the AR value in function of the applied voltage, and then did a linear regression. Then:

  • Battery voltage = AR * BATTERY_ADC_SLOPE + BATTERY_ADC_OFFSET

Note: The code used to initiate the ADC conversion was copied from the Wiring Arduino library.