Measuring Voltage using arduino

If you want to monitor a voltage level that is higher than the voltage that arduino input can handle, you will just need to use a voltage divider.

About the Circuit

Basically you will only need a voltage divider if you are measuring voltage that is greater than the max input voltage of your arduino.

“Vin” is where you will input the voltage to be measured. R1 and R2 forms a voltage divider to decrease voltage of the Vin. The value of R1 will be based on the max voltage that your Vin can reach.

Let R2 = 100k, you can choose higher value but check the computed value of R1 will be easy to find.

The maximum voltage that this arduino nano can handle is 5V so we will use that to compute for the value of R1. R1 is computed such that Va0 level is 5V at Maximum input voltage.

Computing for R1

For instance you are expecting the maximum input voltage is 15V, add about 10% to that to have a little headroom. So you will use 16.5V for your computation.

Vinmax = 16.5V
Va0max = 5V

R1 = (Vinmax – Va0max)/Iin

Iin = Va0max/R2

R1 = (R2(Vinmax – Va0max))/Va0max

R1 = R2((Vinmax/Va0max)-1)

replacing the value to the equation yields:

R1 = (100k)((16.5/5)-1)

R1 = 230k

Computing on software

Arduino will give you the digital value and not the actual voltage value so you need to compute and consider the voltage divider to be able to get the actual input voltage.

Actual voltage is computed as:

Vadc = (adc_val/adc_res)*adc_vref

where:

adc_val – digital value from the adc module
adc_res – ADC resolution, 1024 in case for arduino nano
adc_ref – ADC reference voltage. it is 5V default on arduino nano. This can be changed via program

And consider the voltage divider:

Va0 = Vin(R2/(R1+R2))

Vin = (Va0(R1+R2))/R2

Vin = (R1Va0 + R2Va0)/R2

Vin = Va0((R1/R2) + 1)

And since Va0 = Vadc then…

Vin = ( adc_vref(adc_val/adc_res))((R1/R2) + 1)

Now let’s put it into practice:

int adc_val;
int adc_ref;
int adc_res;
double Vin;
int R1;
int R2;

adc_ref = 5;
adc_res = 1024;
R1 = 230; //k
R2 = 100; //k

// read analog input

adc_val = analogRead(0);  // 0 = Connected to A0

Vin = (adc_ref(adc_val/adc_res))((R1/R2)+1);

Displaying it into the LCD

// include the library code:
#include <LiquidCrystal.h>// initialize the library with the numbers of the interface pins – The numbers
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

int adc_val;
int adc_ref;
int adc_res;
double Vin;
int R1;
int R2;

void setup() {
adc_ref = 5;
adc_res = 1024;
R1 = 230; //k
R2 = 100; //k

    // set up the LCD’s number of columns and rows:
lcd.begin(16, 2);
   // Print a message to the LCD
lcd.print(“hello, world!”);
}

void loop() {
// read analog input
adc_val = analogRead(0);  // 0 = Connected to A0
Vin = (adc_ref(adc_val/adc_res))((R1/R2)+1);
// set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
    // print the number of seconds since reset:
lcd.print(“Voltage:”);
lcd.setCursor(1, 1);
lcd.print(Vin );

}

Related: CONNECTING ARDUINO NANO TO 16×2 LCD