Example Code for Raspberry Pi-Read Voltage Current Power

This tutorial presents a basic usage of the module with Raspberry Pi.

Hardware Preparation

  • Raspberry Pi 3 Model B (or similar) x 1
  • DFRobot Gravity: I2C Digital Wattmeter Sensor x 1
  • Gravity 4P sensor wire (or Dupont wires) x 1
  • High resolution multimeter x1

Software Preparation

Wiring Diagram


Other Preparation Work

Start the I2C interface of the Raspberry Pi. If it is already open, skip this step. Open Terminal, type the following command, and press Enter:

pi@raspberrypi:~ $ sudo raspi-config

Then use the up and down keys to select “5 Interfacing Options” -> “P5 I2C” and press Enter to confirm “YES”. Reboot the Raspberry Pi.

Installing Python libraries and git (networking required). If it is already installed, skip this step. In the Terminal, type the following commands, and press Enter:

pi@raspberrypi:~ $ sudo apt-get update
pi@raspberrypi:~ $ sudo apt-get install build-essential python-dev python-smbus git

Download the driver. In Terminal, type the following commands, and press Enter:

pi@raspberrypi:~ $ git clone https://github.com/DFRobot/DFRobot_INA219.git

Connect the module to the Raspberry Pi according to the connection diagram. The I2C address defaults to 0x45, corresponding to "_INA219_I2C_ADDRESS4" in the code. If the I2C address needs to be modified, configure A0 and A1 to 0 or 1 respectively by the DIP switch on the module, and modify "_INA219_I2C_ADDRESS_x", the second parameter of "ina=INA219(1, _INA219_I2C_ADDRESS4)" in “getVoltageCurrentPower.py”. x can be 1, 2, 3 or 4. The mapping of DIP switch and the I2C address parameters are 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

Install DFRobot_INA219 Raspberry Pi library. In the Terminal, type the following commands and press Enter to run the sample code:

pi@raspberrypi: $ cd ~/DFRobot_INA219/RaspberryPi/Python/example
pi@raspberrypi:~/DFRobot_INA219/RaspberryPi/Python/example $ python getVoltageCurrentPower.py

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 through 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 "BusVoltage" (resolution 4mV) and "Current" (resolution 1mA) are separately read, the power resolution obtained by using the statement "Power = BusVoltage*Current;" can be increased to 4mW (software mode).

  • 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 readings resume.

Was this article helpful?

TOP