Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

How to measure DC Voltage with Arduino ?

Arduino has the ability to measure voltage using analog input pin. For Arduino UNO, there are 6 analog input pins (A0-A5) where you can use one of the pins to measure DC voltage. Arduino NANO has 8 pins while Arduino MEGA has 16 input pins. The analog input pins will map input voltages between 0 and 5V into integer values between 0 and 1023 with resolution of 4.9mV per unit (5.00V / 1024 units). Do not reverse the voltage polarity which may damage the pins.

 

If your measure DC voltage value which is not more than 5.0V, you can directly connect to the analog pin without any modification. In order to measure higher voltage such as 18Vdc, 48Vdc and 100Vdc, voltage divider method is used to split and reduce the measurement voltage into a 5V range. The voltage divider consists of two resistors connected in series as shown in the diagram below. All you need is just 2 resistors with different resistance values.

When the voltage is applied across the pair resistor of different resistance, it creates a voltage drop based on each resistor and it can be used as reference value and it is directly proportional to the total voltage value. Higher resistance R1 tends to have larger voltage drop while smaller resistance R2 will have smaller voltage value which is within acceptable range of Arduino.

 

Arduino UNO (compatible board)

If you still not yet own an Arduino Micro-controller Board, you can get it cheap at our affiliate link here !!!

For easy calculation, below we have attached an excel sheet that can automatically calculate the value of the resistors. First, key in the Monitoring Voltage. It is the maximum value of your DC voltage monitoring range. The next step is to key in the Total Resistance value. It is recommend that the total resistance ranges from 50,000 ohm to 300,000 ohm. 

Total Resistance is flexible. However, small total resistance will have large consumption and heat dissipation for high voltage measurements which end up you have to buy larger wattage resistor. In the contrary, too large total resistance may end up voltage measurement processing too slow or not accurate in low voltage measurements. 

As a guide, 100Vdc and below use Total Resistance value of 50,000 ohm, 200Vdc and below key in 100,000 ohm, and voltage less than 500Vdc key in 300,000 ohm. Once you keyed in Monitoring Voltage and Total Resistance values, the Excel sheet automatic calculate all the relevant specs for the two resistors.

There are 3 important information that need to be specified during the resistor purchase. You may purchase the resistor at our affiliate link here !!

1) Resistance value 

You will end up getting 2 resistors of different resistance value. You might get weird resistance value in the calculation sheet, for example 44,565 ohm and 5,325 ohm. It is just a guideline and all you need to do is to get a round figure which is closer but larger value for resistor R1 and smaller value for resistor R2. Example: 45,000 ohm and 5,300 ohm. This is to ensure monitoring voltage can be measured within 0-5V range.

2) Resistor Wattage

Similar to other products, resistor itself has its withstand’s wattage rating. Oversize wattage rating is always better than using small wattage rating on large consumption which may lead to resistor burnt. Resistor R1 always has more heat dissipation than resistor R2 thus it is not strange that both resistors are not with same wattage requirement. Just get a value larger than the wattage rating as specified in the excel sheet. The standard wattage of a resistor is: 1/8 Watt (0.125W), 1/4 Watt (0.25W), 1/2 Watt (0.5W), 1 Watt, 2 Watt and 5 Watt.

3) The resistance tolerance in %.

Resistor Tolerance is the percentage of deviation from the stated resistance value. If you do not have a Multimeter to measure the actual resistance value, you will need to purchase a better accuracy resistor. Resistors are very small and cheap component; if possible get most accurate ones. 1% is widely use and suitable for monitoring and measuring purposes. 

You may purchase the resistor at our affiliate link here !!

 

Votlage Sensor Module (0-25Vdc)

This is a cheap and reliable voltage sensor module available in the market using the same voltage division method. The voltage sensor module is designed to measure voltage up to 25Vdc with its resistors tolerance of 1%. The R1 is 30k ohm while R2 is 7.5k ohm. If your monitoring range is within 25Vdc, you may consider getting this module which is available via our affiliate link here !!!

 

Hardware Connection

Once you get your voltage sensor module or resistor ready, you may start to do hardware wiring. Below is the schematic of the whole wiring. If you purchase resistors, I recommend you to get a mini breadboard to put next to the Arduino for wiring which is available here !!!

Arduino with Voltage Sensor Module Wiring Diagram

 

Arduino with Resistors on Breadboard Wiring Diagram

Dupont Line Wires

You may need Dupont Line Wires (male to female) to connect Arduino board and Voltage Sensor. It is available at our affiliate link here !!! 

Software Codes

The final step would be adding source code onto Arduino board. I assume you have installed the Arduino Software. If you still have not installed the software, the link here can bring you to the official download site. Once you have downloaded the software, you may download the code file (.ino) for this application below (right click save link). 

There are two source code files attached, the first source code is normal DC voltage source code for people out there without the LCD Display. The measured voltage value can be shown in Serial Monitor using Arduino Software. The second one is the DC voltage source code with LCD display shield function. Once the code is uploaded to the Arduino board, the voltage value will be shown on the LCD Display. 

Arduino UNO LCD Display Shield

Values showing in Serial Monitor is not convenient as you need to turn on the PC all the time. There are a lot of LCD display modules out there but I prefer this shield which can be mounted directly onto existing board without adding more cables for the display itself. The LCD is a 16×2 characters which is sufficient for most use and it also comes with button function for other purposes. You can have this at our affiliate link here !!!! I have also added the voltage code with LCD Display Shield below (right click save link)

Screw Shield / Expansion Shield

When there are a lot of wiring around especially more than 1 sensor, sharing pins will be difficult as existing pins (ground and 5V) are limited. This shield provides a lot of convenient terminals for each of the input and output pins. The shield can be mounted directly on top of the Arduino Uno board or in between the shields which made it very convenient to use. You can get it at our affiliate link here !!!

Datalogger Shield

If you plan to record the data in a proper way, you may consider this Datalogger Shield. It allows your arduino to record your data in SD Card. Datalogger shield is often installed together with LCD Display shield. Please find it at our affiliate link here !!! For more about this Datalogger Shield, kindly visit our post here.

Codes for DC Voltmeter with LCD Display Shield. Note: the codes shown here may not be 100% correct due to translation error. For accurate code, kindly download the .ino file. 

// DC Voltage Module with LCD Display Shield By Solarduino

// 1 - DC Voltage Measurement

int analogInputPin = A2;
float vArduino = 0.0;
float vActual = 0.0;
float R1 = 30000.0;
float R2 = 7500.0;
int rawValueRead= 0;

// 2 - LCD Display

#includeLiquidCrystal LCD(8,9,4,5,6,7);
unsigned long startMillisLCD;
unsigned long currentMillisLCD;
const unsigned long periodLCD = 1000; /

void setup()
{

// 1 - DC Voltage Measurement

pinMode(analogInputPin, INPUT);
Serial.begin(9600);
Serial.println("DC VOLTMETER");

// 2 - LCD Display

LCD.begin(16,2);
LCD.setCursor(0,0);
startMillisLCD = millis();

}

void loop()

{
// 1 - DC Voltage Measurement

rawValueRead = analogRead(analogInputPin);
vArduino = (rawValueRead * 5.0) / 1024.0;
vActual = vArduino / (R2/(R1+R2));

Serial.print("Vdc = ");
Serial.println(vActual,2);
delay(1000);

// 2 - LCD Display

currentMillisLCD = millis();
if (currentMillisLCD - startMillisLCD >= periodLCD)
{
LCD.setCursor(0,0);
LCD.print(vActual,2);
LCD.print(" Vdc ");
startMillisLCD = currentMillisLCD ;
}

}

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

Result – in Serial Monitor

Result – in LCD Display Shield

For Arduino Code Files, Remember to Right Click > Save Link As … You may alter the internal code as you wish. Happy coding !! 

Voltage Divider.xlsx
DC Voltage Module.ino
DC Voltage Module with LCD Display.ino