Measure DC Voltage and Current with Arduino: A Step-by-Step Guide with Simple Code and LCD Display

Uche EmmanuelUche Emmanuel
6 min read

This article will teach you how to use the Arduino IDE to measure DC voltage and current with a simple code and a single connection on one LCD display, providing a schematic to follow and step-by-step lines of codes.

Arduino

Arduino is an open-source platform for electronics projects that uses a microcontroller to control various components. It is user-friendly, with an accessible IDE and a large, supportive community.

Its versatility allows for a range of projects, from simple LED control to robotics and automation. Arduino is open-source, cost-effective, and suitable for both beginners and professionals.

This usually needs some or a lot of lines of code for it to function. Arduino has no specific language it is written in. Yes, it is written with a combination of languages, for example, C/C++, Assemble Language, Python, Java, and JavaScript.

Please note that the Arduino platform itself is built on a foundation of C/CC++ and assembly language.

The Arduino IDE (Integrated Development Environment) is like a special notebook for writing instructions (code) that tell Arduino boards what to do. This code, called sketches, is what makes Arduino projects come alive. You can get more basic knowledge on Arduino Here

DC Voltage And Current Measurement

Direct current (DC) voltage and current are two of the basic electrical quantities that define the operation of an electronic circuit.

You can say a voltage is like the force that pushes the current through the circuit, while the current is the flow of electrical charge.

The Arduino's built-in analog-to-digital converter (ADC) can measure voltage up to its operating voltage (usually 5 volts).

Components

    1. Arduino board (UNO)

      1. LCD display

      2. Voltage Sensor (Array Module)

      3. Current (ACS712)

      4. Bread-Board

      5. Jumper wires

      6. DC voltage source X2 (battery, power supply)

      7. Dc Load (Bulb)

      8. Switch

Schematic

All components listed above are listed in this diagram. Do well to carefully follow through with the diagram. You can decide to use an adaptable box and then make the necessary spaces for the outside connections as shown in the diagram.

Code/Sketch

Analog Input Pin

This line of code defines a constant called ANALOG_IN_PIN and assigns a value to A0 and A1.

These A0 and A1 are the first analog input pins on the Arduino board. They are both from different sensors.

    • A0, this analog input pin is used to read analog current flowing in the load in use.

      • A1: These analog input pins are used to read analog voltage signals, which can have a range of values between 0 and 5 volts.

 //For voltage
#define ANALOG_IN_PIN A1
//for current
#define ANALOG_IN_PIN A0

LiquidCrystal_12C Library

Before you can run this line of code, you will definitely need to download the LiquidCrystal_I2C library. You can download it here.

The LiquidCrystal_I2C is made to work with 12C (Inter-Integrated Circuit) LCD displays. By including this library at the beginning of the sketch, we are making all the functions and features of the C library available for use in our code.

  • The #include <Wire.h> line includes the Wire library, which is necessary for I2C communication on Arduino.

  • LiquidCrystal_I2C lcd(0x27, 16, 2); initializes an object named lcd of type LiquidCrystal_I2C.

#include <LiquidCrystal_I2C.h>

#include <wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);

Variables

This is when you declare and initialize variables for measuring the voltage and current. Now these constants will be used throughout the measurement.



// For Voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;

float R1 = 30000.0;
float R2 = 7500.0; 

float ref_voltage = 5.0;
int adc_value = 0;

//For Current
double Vout = 0;
double Current = 0;
const double scale_factor = 0.066; // 30A
const double vRef = 5.00;
const double resConvert = 1024;
double resADC = vRef/resConvert;
double zeroPoint = vRef/2;

Setup()

This is where you call your program to run, like initializing the serial communication at a baud rate of 9600, the pin mode and the LCD display.

The void setup() function in Arduino is a mandatory function that is called once at the beginning of the program.


void setup() {

  Serial.begin(9600);


//For Voltage
  pinMode(A1,INPUT);
//For Current  
  pinMode(A0,INPUT);


  lcd.init();
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("DC Voltage And");
  lcd.setCursor(0,1);
  lcd.print("DC Current");
  delay(2000);
  lcd.clear();

}

Loop

This code runs the necessary computations, records the results in the appropriate variables (in_voltage and current), and continually monitors the DC voltage and current. Whereas the current measurement averages 1000 measurements for accuracy, the voltage measurement just employs one analog read.

lcd.init() initializes or awakens the LCD display, creating a connection between your lines of codes and the LCD display.

void loop() {

// For Voltage
   adc_value = analogRead(A1);
   adc_voltage  = (adc_value * ref_voltage) / 1024.0; 
   in_voltage = adc_voltage / (R2/(R1+R2)); 

//For Current
   for(int i = 0; i < 1000; i++) {
    Vout = (Vout + (resADC * analogRead(A0)));

   }
Vout = Vout /1000;
 Current = (Vout - zeroPoint) / scale_factor;

   if (Current < 0.10) {
    Current = 0;
   }

Serial Print

This is where you display or print your calculated input voltage and current values on the serial monitor in the Arduino IDE (this is like your display terminal).

 Serial.print("Input Voltage = ");
  Serial.print(in_voltage, 2);
  Serial.println(" Volts");
  Serial.print("\t Current = ");                  
  Serial.print(Current, 2);
  Serial.println(" Amps");
  delay(500);

LCD display

Finally, this code segment is crucial for displaying the measured input voltage and current values in a readable format on the LCD screen, providing real-time feedback on the voltage and current measurements being taken by the Arduino.

//For Voltage

  lcd.setCursor(0, 0);
  lcd.print(in_voltage);
  lcd.print(" Volts");


//For Current
  lcd.setCursor(0, 1); 
  lcd.print(Current, 2);
  lcd.print(" Amps"); 

  delay(500);



}

Furthermore, when you have the connections set and your code set, you can upload your code to the Arduino board by using your Arduino cable. After uploading, your DC voltage and current are ready to be measured.

You can place a DC voltage to calculate the voltage and also place any DC load to calculate the current flowing through it. Please do well to adhere strictly to the diagram above.

Conclusion

In this article, we have learned to measure and display DC voltage and current with Arduino with simple code, using the components provided in this article.

The key steps we covered were:

  1. Connecting the Arduino, LCD display, voltage sensor, and current sensor using jumper wires

  2. Writing Arduino code to read the analog input pins, perform calculations to determine the voltage and current values, and format the results for display

  3. Displaying the real-time voltage and current measurements on the LCD screen

This project introduces working with Arduino, sensors, and displays. It teaches interfacing components, creating interactive systems, expanding code, and gaining practical experience in embedded systems and electronics.

I hope you found this tutorial helpful and inspiring! Let me know if you have any other questions or ideas for projects you'd like to explore with Arduino.

10
Subscribe to my newsletter

Read articles from Uche Emmanuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Uche Emmanuel
Uche Emmanuel

I am an eager, self-motivated, reliable, responsible, and industrious Technical writer and Graphic designer. I have good time management abilities, and I can work well under pressure and meet tight deadlines. I enjoy working practically with good life ideals to ensure that everything runs smoothly. I am looking for positions with forward-thinking firms that will allow me to adopt creative methods while increasing revenue and brand growth and to use my abilities and creativity to participate, manage, and carry out complicated tasks in a team setting.