Measure a battery or supply with the Arduino voltage sensor module
Beginner
Or see every lesson.
- 465parts on the bench
- 43boards running now
- 1.00xreal time, on every board
The voltage sensor module is the small red board with a green screw terminal in most Arduino sensor kits. It lets an Arduino measure a voltage higher than its own 5 V, like a 12 V battery or a bench supply, so a project can warn you when a battery is getting flat.
The circuit above is an Uno with the module across a bench supply set to 12 V, and a 16x2 I2C LCD. Press Run and the screen shows the voltage. Press the supply’s VOLTAGE buttons and the reading follows. It runs in your browser on the sketch below.
What you need
- An Arduino Uno
- A voltage sensor module (0 to 25 V)
- A battery or a bench supply to measure
- A 16x2 LCD with an I2C backpack
- A half-size breadboard and jumper wires
Wiring
| Part and pin | Goes to | Note |
|---|---|---|
| Module VCC (screw terminal) | Plus of the voltage to measure | Up to 25 V on a 5 V Arduino |
| Module GND (screw terminal) | Minus of the voltage to measure | |
| Module S | A0 | A fifth of the input |
| Module - | GND | The same trace as the terminal GND |
| Module + | Nothing | Not connected on the board |
| LCD SDA and SCL | A4 and A5 | Backpack address 0x27 |
It is just two resistors
There is no chip on the board. A 30 kohm resistor runs from the terminal’s VCC to S, and a 7.5 kohm resistor from S to GND. That is a voltage divider, and it makes S one fifth of the input: 7.5 / (30 + 7.5) = 0.2. So the sketch reads S and multiplies by 5.
In the circuit above, 12 V on the terminal is 2.4 V on S, and A0 reads 491. The sketch turns that into 491 x 5 / 1023 x 5 and the screen shows 12.00 V.
The board loads whatever it measures with its 37.5 kohm, about a third of a milliamp at 12 V. That is nothing for a battery, and Mokxi puts the two real resistors in the circuit, so it is there in the simulation too.
Where 25 V comes from, and why it is 16.5 V on a 3.3 V board
The limit is the Arduino, not the board. An Uno can read up to 5 V, and 5 V on S is 25 V on the terminal: step the supply to 25 V and A0 reads 1023, which the sketch shows as 25.00 V. Anything higher reads 1023 too, and pushes S above the Uno’s supply.
On a 3.3 V board like an ESP32, a Pico or an Arduino Due, the top of the ADC is 3.3 V, so the same module reads at most 3.3 x 5 = 16.5 V. Change the 5.0 in the sketch to 3.3 as well.
One step of the Uno’s ADC is 4.9 mV at S, so 24.4 mV on the terminal. Set the supply to 9 V and A0 reads 368, which shows as 8.99 V. That last digit is the converter’s step, not a fault.
void loop() {
long sum = 0;
for (int i = 0; i < 16; i++) sum += analogRead(SENSE);
int raw = (sum + 8) / 16;
float atS = raw * 5.0 / 1023.0; // the voltage at S
float volts = atS * 5.0; // the divider is 5 to 1
Serial.print("ADC: ");
Serial.print(raw);
Serial.print(" volts: ");
Serial.println(volts, 2);
delay(250);
}The shared ground
The terminal’s GND and the header’s minus are the same trace on the board. So when you wire minus to the Arduino’s GND, the minus of whatever you are measuring is now on the Arduino’s ground. That is how it measures at all, and it is why this module must never go near mains or anything that cannot share a ground with your Arduino.
Try it in the editor
Add an LED that lights when the voltage drops under 11.8 V, the point where a 12 V lead-acid battery is nearly flat. Or swap the bench supply for a 9 V battery part and watch its voltage sag when you add a load.
Common mistakes
Leaving minus unconnected. Without a shared ground the Arduino has nothing to measure S against, and the reading wanders.
Putting the voltage on the header’s plus pin. It goes nowhere on the board. The voltage goes into the screw terminal.
Measuring more than 16.5 V with a 3.3 V board. The 25 V on the listing assumes a 5 V Arduino.
Forgetting the factor of 5. Without it, a 12 V battery reads 2.4 V.
Questions
How do I connect a voltage sensor module to an Arduino?
The voltage to measure goes into the screw terminal, plus to VCC and minus to GND. On the header, S goes to an analog pin such as A0 and minus goes to the Arduino’s GND. The plus pin is not connected.
What is the formula for the Arduino voltage sensor?
The board is a 30 k and 7.5 k divider, so the input is five times the voltage on S: volts = analogRead(A0) x 5.0 / 1023 x 5 on a 5 V Arduino.
Can the voltage sensor module measure 25 V on an ESP32?
No. On a 3.3 V board the most it can read is 3.3 x 5 = 16.5 V. The 25 V figure is for a 5 V Arduino.
What does Mokxi not model on the voltage sensor?
The resistors are exact here, so the ratio is exactly 5; a real board’s 1% or 5% resistors can need a small correction. What happens to the Arduino’s input above 25 V is not modeled either.
Keep going
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.