How to Measure Voltage with Arduino: Voltage Divider

Projects Beginner 8 min read Published:
Cover image for measuring voltage with Arduino and calculating a voltage divider, showing an R1 R2 divider and the A0 input

To measure a voltage higher than 5 V with an Arduino, use a two-resistor voltage divider: the voltage to be measured is divided down at the junction of the upper resistor R1 and the lower resistor R2, and that junction is connected to an analog input (e.g., A0). The input voltage is found with the formula Vin = ADC × VREF / 1024 × (R1 + R2) / R2. In this article’s example, we build a circuit with 21.5 kΩ and 10 kΩ resistors that measures a 12 V battery up to 15.75 V using a 5 V reference.

The example circuit is described on an Arduino Nano, but the same calculation applies to any board using the ATmega328P. All ADC limit values are taken from Microchip’s ATmega48A/PA/88A/PA/168A/PA/328/P datasheet, and R1’s power, voltage and tolerance values from the Vishay Dale CMF (RN/RL) datasheet. If you only need the ratio, our voltage divider calculator gives the result directly; this article covers building the circuit, writing the code and getting an accurate measurement.

What is a voltage divider and why do you need one?

A voltage divider is a circuit of two series resistors that delivers a fixed fraction of the input voltage to its output. The output is taken from the point where the two resistors meet:

Vout = Vin × R2 / (R1 + R2)

The analog input of the ATmega328P on the Arduino Nano cannot measure a value above the selected reference voltage (VREF). The ADC table in the datasheet gives the input voltage range as GND to VREF. More importantly, according to the absolute maximum ratings table, no pin other than RESET should see more than VCC + 0.5 V. On a board running at 5 V, that limit is 5.5 V. A 12 V battery is more than twice this limit; connecting the battery directly to A0 can permanently damage the input. The divider brings the battery voltage down into this safe range.

Parts list

Part Value / part number Function
Microcontroller board Arduino Nano 328 Reads the divider output with its 10-bit ADC
R1 (upper resistor) RN55C2152BR36, 21.5 kΩ, ±0.1% Drops most of the battery voltage
R2 (lower resistor) MFR100FRF52-10K, 10 kΩ Resistor between A0 and GND
C (optional) 100 nF ceramic capacitor Filters noise at A0

The code of the Vishay part we chose for R1 is decoded with the table in the datasheet: RN55 indicates the military-style body, the letter C a 50 ppm/°C temperature coefficient, 2152 the value 21.5 kΩ (215 × 10²), the letter B a ±0.1% tolerance, and R36 tin/lead-plated leads in full tape and reel (T/R) packaging. According to the datasheet, the rated power of an RN55 with characteristic C is 0.10 W at 125 °C (the 0.125 W value at 70 °C applies to characteristic D); the maximum working voltage is 200 V. In this project the resistor dissipates only a few milliwatts, far below these limits.

Wiring diagram

The circuit has only four connections: the battery positive terminal goes to one end of R1, the other end of R1 goes to both R2 and A0, the other end of R2 goes to GND, and the battery negative terminal goes to the Arduino’s GND. If this last connection is forgotten, the measurement is meaningless, because the ADC measures voltage relative to the board’s own GND.

12 V battery measurement circuit with a 21.5 kΩ and 10 kΩ voltage divider connected to the A0 input of an Arduino Nano
Figure 1: Divider measuring a 12 V battery voltage. R1 = 21.5 kΩ, R2 = 10 kΩ; the 100 nF capacitor is optional. The battery negative terminal must be connected to the Arduino GND.

How to choose the resistor values

The selection takes three steps.

  1. Determine the highest voltage to be measured. A 12 V lead-acid battery can rise above 14 V while charging. Leaving some margin, it makes sense to keep the upper limit around 15–16 V.
  2. Calculate the ratio. The ratio R2 / (R1 + R2) must keep A0 below VREF at the highest input voltage. You can pick R2 and find R1 with this formula: R1 = R2 × (Vmax / VREF − 1).
  3. Check the source impedance. The datasheet states that the ADC is optimized for sources with an output impedance of approximately 10 kΩ or less. The divider’s impedance as seen from A0 is the parallel equivalent of R1 and R2: R1 × R2 / (R1 + R2).

With our values, the calculation is as follows:

Quantity Calculation Result
Divider ratio 10 / (21.5 + 10) 0.3175
Highest measurable voltage (VREF = 5 V) 5 / 0.3175 15.75 V
Resolution referred to the input (1 LSB) (5 / 1024) / 0.3175 about 15.4 mV
Source impedance seen from A0 21.5 × 10 / 31.5 about 6.8 kΩ
Current through the divider (at 12.5 V) 12.5 V / 31.5 kΩ about 0.40 mA
Power in R1 (at 15.75 V) (0.5 mA)² × 21.5 kΩ about 5.4 mW
Input voltage at which A0 reaches 5.5 V 5.5 / 0.3175 about 17.3 V

At 6.8 kΩ, the source impedance stays below the 10 kΩ limit recommended by the datasheet. The 5.4 mW dissipated in R1 is very small next to the RN55C’s 0.10 W power rating. The last row of the table is a warning: if the battery voltage exceeds 17.3 V, A0 exceeds its absolute maximum rating. If your vehicle or charging circuit can produce such peaks, reduce the ratio. For example, to measure up to 30 V in a 24 V system, the formula gives R1 = 50 kΩ with R2 = 10 kΩ; with the nearest standard value of 51 kΩ, the highest measurable voltage becomes about 30.5 V. To try different values quickly, use the voltage divider calculator, and for current and power, the Ohm’s law calculator.

Graph of the A0 pin voltage versus battery voltage, with the 15.75 V measurement limit and the 17.3 V absolute maximum limit marked
Figure 2: Linear relationship between the input voltage and the A0 voltage. At about 15.7 V the ADC reaches full scale (1023); above 17.3 V, A0 exceeds the VCC + 0.5 V limit.

How to convert the ADC reading to voltage

The datasheet defines the single-ended conversion result as ADC = VIN × 1024 / VREF. 0x000 represents analog ground, and 0x3FF (1023) represents the reference voltage minus one LSB. Inverting this formula and adding the divider ratio gives the battery voltage:

Vin = ADC × VREF / 1024 × (R1 + R2) / R2

Example calculation: analogRead(A0) returned 812 and VREF = 5.00 V.

  1. Voltage at A0: 812 × 5.00 / 1024 = 3.965 V
  2. Battery voltage: 3.965 × 31.5 / 10 = 12.49 V

Arduino code

The code below takes 16 samples from A0, averages them and prints the result in volts to the serial monitor. Averaging reduces the few-LSB jitter of a single reading. Since 16 × 1023 = 16368, the sum easily fits in a long variable.

const float R1 = 21500.0;   // upper resistor (ohm)
const float R2 = 10000.0;   // lower resistor (ohm)
const float VREF = 5.00;    // value measured on the 5V pin with a multimeter

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

void loop() {
  long toplam = 0;
  for (int i = 0; i < 16; i++) {
    toplam += analogRead(A0);
    delay(2);
  }
  float adc = toplam / 16.0;
  float vPin = adc * VREF / 1024.0;
  float vGiris = vPin * (R1 + R2) / R2;

  Serial.print("Battery voltage: ");
  Serial.print(vGiris, 2);
  Serial.println(" V");
  delay(1000);
}

Calibration for an accurate measurement

The weakest link in the formula is VREF. By default, the Arduino uses the supply voltage (AVCC) as the reference, and the board’s 5 V rail may not be exactly 5.00 V. The example reading of 812 corresponds to 12.24 V with a reference that is actually 4.90 V; if the code keeps 5.00, the measurement reads about 0.25 V high. You can correct the measurement in three steps:

  1. While the board is powered from the source you will use during measurement, measure the voltage between the 5V and GND pins with a multimeter and enter it in the VREF constant.
  2. Connect the battery, measure the voltage across the battery terminals with a multimeter and compare it with the value on the serial monitor.
  3. If a difference remains, calculate a correction factor: k = Vmultimeter / Vread. Multiply the result in the code by this factor (vGiris = vGiris * k;). Checking the factor at several different battery voltages makes the measurement more reliable.

The effect of resistor tolerance can also be calculated. The relative error of the divider ratio equals the difference between the two resistors’ relative errors multiplied by R1 / (R1 + R2); in our circuit, this factor is 0.68. In addition to R1’s ±0.1% tolerance, if we assume the 10 kΩ resistor has a ±1% tolerance, the ratio deviates by about ±0.75% in the worst case. At 12.5 V, that is about ±0.09 V. The correction factor calculated in the third step above largely removes this error as well.

Noise filter: 100 nF capacitor

The datasheet recommends removing signal components above the Nyquist frequency from the input with a low-pass filter. A 100 nF capacitor placed between A0 and GND forms, together with the divider’s 6.8 kΩ equivalent resistance, an RC filter with a cutoff frequency of about 233 Hz (f = 1 / (2π × 6825 Ω × 100 nF)). Since the battery voltage changes slowly, this filter does not delay the measurement; it suppresses sudden noise from a motor, relay or charging circuit. The capacitor also provides a local charge reservoir for the input while the ADC’s internal 14 pF sample-and-hold (S/H) capacitor charges.

Common mistakes

  • Not connecting a common GND. If the battery negative terminal is not connected to the Arduino GND, A0 floats and random values are read.
  • Swapping R1 and R2. If the 21.5 kΩ resistor is placed on the GND side, the ratio becomes 0.68 and about 8.2 V appears at A0 at 12 V. This value is far above the 5.5 V absolute maximum rating.
  • Choosing very large resistors. Using megaohm-range resistors to reduce current can be tempting, but when the source impedance is far above 10 kΩ, the sample-and-hold (S/H) capacitor does not charge fully and readings come out low or unstable. In that case, add a capacitor at A0 or wait between samples.
  • Dividing by 1023 in the code. The datasheet formula uses 1024. Dividing by 1023 makes the reading about 0.1% larger; even though calibration corrects the result anyway, it is better to write the formula as the datasheet gives it.
  • Forgetting that the divider continuously drains the battery. 0.40 mA is a small current, but it adds up to about 9.5 mAh per day. Take this into account in a system that stays connected for months.

Frequently asked questions

What is the maximum voltage an Arduino analog input can take?

For a measurement, the input must be between GND and VREF; in the default setting on 5 V boards, this is the supply voltage. According to the ATmega328P datasheet, the absolute upper limit that can be applied to the pins is VCC + 0.5 V. This limit is a damage limit, not a measurement range.

Can a voltage divider measure 220 V?

The circuit in this article is only for low-voltage DC sources. Mains voltage is AC, and a divider would connect both the Arduino and the user directly to the mains. Mains-related measurements must use circuits that provide isolation; for an isolated approach, see our article on mains detection with the SFH620A.

Is the internal 1.1 V reference better?

The internal reference is largely independent of supply voltage fluctuations, but the datasheet specifies it between 1.0 V and 1.2 V. This means it requires per-board calibration, and the divider ratio must be recalculated for 1.1 V. If an external voltage is applied to the AREF pin, the internal references must not be used.

How precise do the divider resistors need to be?

If you calibrate, ±1% metal film resistors are sufficient. If you want to measure without calibration, tight-tolerance resistors such as ±0.1% with a low temperature coefficient reduce the error. To read a resistor’s value from its color bands, use the resistor color code calculator.

Conclusion

Measuring voltages above 5 V with an Arduino takes two resistors. Choose the ratio for the highest voltage to be measured, keep the source impedance below 10 kΩ, connect a common GND, and measure VREF with a multimeter and enter it in the code. The 21.5 kΩ and 10 kΩ pair provides a measurement range up to 15.75 V for 12 V batteries and a resolution of about 15 mV at the input. The Arduino Nano 328, RN55C2152BR36 and MFR100FRF52-10K used in the circuit are on their product pages; resistors with other values are in the through-hole resistors category, and other boards in the microcontroller development boards category. For choosing a voltage regulator to power the Arduino from a battery, see our article on the difference between the 7805 and AMS1117.

RN55C2152BR36Vishay · EE01137-3$0.6414 In StockView

Sources

Ask Claude or ChatGPT about the parts in this article and their alternatives: your assistant brings them from our catalog with stock and prices.

How do I connect?

Products in this article

More from Projects

For AI assistants

Search our catalog with Claude or ChatGPT

You came here from an AI assistant. Connect Claude or ChatGPT to Entegreci once, and it searches our catalog for the parts you ask about and brings back products with their availability, current price and a link.

“Is the 1N5819 in stock, and what could I use instead?”

Free for members. The assistant only reads the catalog; it can't place orders or see your account details.