Example Code for Raspberry Pi – I2C Digital Wattmeter

Last revision 2026/01/20

The article provides a comprehensive guide on using the I2C Digital Wattmeter with Raspberry Pi to measure voltage, current, and power with high precision, covering installation of libraries, configuration of I2C addresses, and Python script execution for real-time monitoring, ideal for solar systems and battery life evaluation.

Introduction

I2C Digital Wattmeter is a high-resolution, high-precision, large-scale measurement module that can measure the voltage, current and power of various electronic modules and electrical equipment within 26V 8A, and the maximum relative error is no more than ±0.2% (A simple manual calibration is required before using). It can be used for power consumption measurement or battery life evaluation of solar energy systems, battery coulombmeters, motors, controllers or electronic modules.

This module adopts TI INA219 zero temperature drift current/power monitoring chip and 2W high power low temperature drift 10mΩ alloy sampling resistor. The voltage and current resolution can reach 4mV and 1mA respectively. Under the full-scale measurement condition, the maximum relative error of voltage and current measurement can be superior to ±0.2%. It also provides four I2C addresses that can be configured via the 2P DIP switch. The module accurately measures bi-directional high-side currents (current flowing through the power supply or battery positive), which is especially useful in solar or battery fuel gauge applications where the battery needs to be charged and discharged. This status can be simply determined by positive or negative current readings. In the motor applications, the current can be monitored in real time by monitoring whether the motor current is too large due to overload. In addition, you can use this module to measure the power consumption of various electronic modules or the entire project to evaluate battery life.

Wiring Diagram

  • Power the Raspberry Pi on and install the Raspberry Pi expansion board correctly
  • Connect the sensor to the IIC interface on the expansion board

  • Configure to enable I2C and restart the Raspberry Pi. If configured, you can skip this step. Configure the Raspberry Pi according to the following procedure and restart it.

Sample Code

  • Install I2C libraries and tools, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’
sudo apt-get install i2c-tools
  • When the I2C device is connected, the I2C address can be checked by the following command. In the terminal, type the following instructions and press ‘Enter’
sudo i2cdetect -y -a 1

  • Read all register data of I2C device. In the terminal, type the following instructions and press ‘Enter’
sudo i2cdump -y 1 0x45

-y means cancelling the user interaction process and directly executing the command
1 is the I2C device number
0×45 is I2C device address

  • Install Python dependency libraries and git, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’

sudo apt-get install build-essential python-dev python-smbus git

  • Install the drive library. In the terminal, type the following instructions in order and press ‘Enter’

cd \~

git clone https://github.com/DFRobotdl/37\_Pcs\_Sensor.git

  • Find get_voltage_current_power.py in 37_Pcs_Sensor\4_I2C_Digital_Wattmeter, open it with Thonny Pyth IDE and run it, then you can see the printed data.

  • Click ‘run’, it will show current, voltage, and other information.

Was this article helpful?

TOP