Sunday, May 17, 2009

Arduino test: voltmeter

First experiment with Arduino. Trying to implement a voltmeter.

Each analog input of Arduino has 1024 steps. From 0 to 5 volt. This experiment used a simple voltage divider to measure roughly 0 to 12 volt (using 50k as R1 and 4k3 as R2).

By varying the values of R1 and R2, the precision and range of the measurement can be changed.

Here is the Arduino code:

// variables for input pin and control LED
  int analogInput = 1;
  int LEDpin = 13;
  int prev = LOW;
  int refresh = 1000;
  float vout = 0.0;
  float vin = 0.0;
  float R1 = 50000.0;    // !! resistance of R1 !!
  float R2 = 4300.0;     // !! resistance of R2 !!

// variable to store the value
  int value = 0;

void setup(){

  // declaration of pin modes
  pinMode(analogInput, INPUT);
  pinMode(LEDpin, OUTPUT);

  // begin sending over serial port

void loop(){
  // read the value on analog input
  value = analogRead(analogInput);

  if (value >= 1023) {
  else if (value <= 0) {

  // blink the LED
  if (prev == LOW) {
    prev = HIGH;
  } else {
    prev = LOW;
  digitalWrite(LEDpin, prev);

  // print result over the serial port
  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));


  Serial.println(" volt");

  // sleep...


cc said...


Colin said...


I'm trying to read battery voltages in real time from a car I recently converted from ICE to all electric. I have 11 12V lead acid batteries in series as my battery pack and I am researching how best to cycle through the battery pack and read the voltage of each of the 11 in turn . I need to read the range of 10V to 15 v.

I know the arduino has only 6 analogue inputs, but for a start, how do I calculate the values of r1 and r2 in your example above to read that V range at just one input?

Thanks for any help,

Colin McC

clarence said...

@colin: R1 and R2 act together as a voltage divider. If Vin is the voltage across the voltage divider, the voltage applied to Arduino input will be Vout = (Vin * R2) / (R1 + R2).

Choose your combination of R1 and R2 based on: (1) R1 + R2 will affect the current drawn from the source. Keep the current small to avoid overheating and increase accuracy; (2) Arduino input can read 0V to5V. Values returned range from 0 to 1023.

(now you asked... i am wondering if i actually used 4k3 and 50k resistors in my experiment... anyway...)

Luis said...

Works,its a good sample.

Luis said...

this site help you to caculate:

extremesanity said...

Thanks for the info!

daniel said...

is there any chance of making this control a LED bar according to volts measured in the range of 10 - 15v without frying the arduino ?

thanks in advance

Ricki said...

yeah im trying to use this code and my measurements are not matching with what should be getting

quadboy said...

where did you get that arduino clone?

Rico said...

For the life of me I can't get this to work.

B. Fischer said...

Hello to all,

thanks for the great work. I'm using the voltmeter to check my 4S LiFePo4 Bat realtime. Its better to work with "E24" resistors.
Voltage:0-20V: R1=13KOhm, R2=4.3KOhm.
It works great. Thanks a lot.

Benjamin from Germany

matii said...

good stuff but have anybody code whit LCD screen, better in car have volt meter at lcd and see numbers, and many other place same,change only volt divicer what need.
i try looking at net but no found good volt meter vhit lcd i2c. arduino uno.

Boris said...

Hi, when 12V is input, the output is 0.957V. Is that correct? Why so little coverage? Arduino allows 5V input. So it is possible to use R1=1.1M and R2=270R. The input can be 20V and the output is 4.9V, it is OK.
Have I a mistake somewhere?

David said...

if tuss calculations are correct for 20 V but 12 salelo me 0 same as you .....
remember that arduinonoson 5v are 4.9V for load ami 12 v gives me 6.7khm r1 = r2 = 4.7 / 12v = 4.947 elo I think we can give exact ma 12v pariendode also to estono varie muchoresistencias 1% tolerance corregirmesime wrong