Example Code for Arduino-Read Voltage Current Power
This tutorial presents a basic usage of the module with Arduino UNO.
Hardware Preparation
- DFRduino UNO R3 (or similar) x 1
- DFRobot Gravity: I2C Digital Wattmeter x 1
- Gravity 4P sensor wire (or Dupont wires) x 1
- High resolution multimeter x1
Software Preparation
- Arduino IDE
- Download and install the DFRobot_INA219 Library. (About how to install the library?)
Wiring Diagram
Other Preparation Work
Calibration
In the actual measurement environment, measurement errors come from many sources. However, for the Gravity: I2C Digital Wattmeter, the voltage measurement does not need to be calibrated, and the current measurement error mainly comes from the error of the resistance of the sampling resistor, which will have a significant impact on the current measurement. If calibration is not performed, the relative error of the maximum current measurement is about 3%. If a single-point linear calibration is performed using a high-precision multimeter or an electronic load, the linearity error of the system can be effectively eliminated, and the maximum relative error can be up to ±0.2%.
- If you already have a regulated power supply and a DC electronic load on hand, follow the steps below to calibrate the current measurement:
- Connect the Arduino UNO, digital wattmeter module, regulated power supply and DC electronic load as shown below.
- Adjust the output voltage of the power supply to 12V
- Select the constant current mode CC for the electronic load, and set the current to 1.000A.
- Upload the sample code to Arduino UNO.
- Modify the value of the variable "float ina219Reading_mA = 1000;" in the code according to the readings of "Current".
- Upload the sample code to Arduino UNO again.
- Calibration finished.
- If you don't have a regulated power supply nor a DC electronic load on the hand, follow the steps below to calibrate the current measurement:
- Connect the Arduino UNO, multimeter (switch to amperemeter) and load (gas sensor, motor or LCD screen etc. ) as shown below. It is recommended that the load power consumption should no less than 100mA.
- Upload the following sample code to Arduino UNO.
- Modify the value of the variable "float ina219Reading_mA = 1000;" according to the readings of the serial port print "Current" and "float extMeterReading_mA = 1000;" according to the current readings of the multimeter.
- Upload the sample code to Arduino UNO again.
- Calibration finished.
- The calibrated parameters "ina219Reading_mA" and "extMeterReading_mA" can now be used in this specific wattmeter module without recalibration for each measurement.
Sample Code
/*!
file getVoltageCurrentPower.ino
SEN0291 Gravity: I2C Digital Wattmeter
The module is connected in series between the power supply and the load to read the voltage, current and power
The module has four I2C, these addresses are:
INA219_I2C_ADDRESS1 0x40 A0 = 0 A1 = 0
INA219_I2C_ADDRESS2 0x41 A0 = 1 A1 = 0
INA219_I2C_ADDRESS3 0x44 A0 = 0 A1 = 1
INA219_I2C_ADDRESS4 0x45 A0 = 1 A1 = 1
Copyright [DFRobot](https://www.dfrobot.com), 2016
Copyright GNU Lesser General Public License
version V0.1
date 2019-2-27
*/
#include <Wire.h>
#include "DFRobot_INA219.h"
DFRobot_INA219_IIC ina219(&Wire, INA219_I2C_ADDRESS4);
// Revise the following two paramters according to actula reading of the INA219 and the multimeter
// for linearly calibration
float ina219Reading_mA = 1000;
float extMeterReading_mA = 1000;
void setup(void)
{
Serial.begin(115200);
while(!Serial);
Serial.println();
while(ina219.begin() != true) {
Serial.println("INA219 begin faild");
delay(2000);
}
ina219.linearCalibrate(ina219Reading_mA, extMeterReading_mA);
Serial.println();
}
void loop(void)
{
Serial.print("BusVoltage: ");
Serial.print(ina219.getBusVoltage_V(), 2);
Serial.println("V");
Serial.print("ShuntVoltage: ");
Serial.print(ina219.getShuntVoltage_mV(), 3);
Serial.println("mV");
Serial.print("Current: ");
Serial.print(ina219.getCurrent_mA(), 1);
Serial.println("mA");
Serial.print("Power: ");
Serial.print(ina219.getPower_mW(), 1);
Serial.println("mW");
Serial.println("");
delay(1000);
}
Result
- The module prints four parameters every 1s:
- ShuntVoltage: Voltage of the sampling resistor, IN+ to NI-.
- BusVoltage: Voltage of IN- to GND.
- Current: Current flows across IN+ and IN-. If the current flows from IN+ to IN-, the reading is positive. If the current flows from IN- to IN+, the reading is negative.
- Power: The product of "BusVoltage" and "Current", that is, power. The power resolution read directly from the module is 20mW (hardware mode). If the power is obtained by using the statement "Power = BusVoltage*Current;", the resolution can be increased to 4mW (software mode).
-
The module can be configured as one of four different I2C addresses by DIP switch. Accordingly, change the parameter "INA219_I2C_ADDRESSx" of "ina219.setI2cAddr(INA219_I2C_ADDRESS4);", where x can be 1, 2, 3 or 4. The mapping of DIP switch and the I2C address parameter is as follow:
- INA219_I2C_ADDRESS1: 0x40, A0=0, A1=0
- INA219_I2C_ADDRESS2: 0x41, A0=1, A1=0
- INA219_I2C_ADDRESS3: 0x44, A0=0, A1=1
- INA219_I2C_ADDRESS4: 0x45, A0=1, A1=1
-
If the I2C fails due to the following reasons, "I2C init fail" is printed.
- The I2C wiring SCL and SDA are not properly connected.
- The I2C address (DIP switch) does not match the I2C address parameter.
-
When the DIP switch is properly configured, the "Measuring voltage and current with INA219 ..." is printed and the readings resume.
Was this article helpful?
