Raspberry Pi Power Monitor


Power Consumption Monitor using Raspberry Pi


Sometimes I get tired of using test equipment to monitor the current and/or power consumption of the system, especially when I want to quickly test it and log the data.
For some of my projects, I use INA219 populated on my board to monitor the current.
Yes, it is very helpful and I can manage the power status through firmware.
Also, having test equipment is not practical for the hobby projects like I do...
So here, I decided to use off-the-shelf INA219 board with Raspberry Pi to create easy and cheap current and/or power monitoring tool.
I bought Adafruit INA219 board and this is just less than $10. 
Here is the features.
  • 0.1 ohm 1% 2W current sense resistor
  • Up to +26V target voltage
  • Up to ±3.2A current measurement, with ±0.8mA resolution
If you want even cheaper INA219 board, there are some others and I think those work very similar to Adafruit one.
Less than $5:


Setup
For this setup, I am using Raspberry Pi 3 Model B.
Thankfully, there are many tutorials and information already.
You can refer to the Adafruit site for the hardware and software instructions.

1. Connect INA219 board to Raspberry Pi
I used jumper wires to connect 2 boards.
You just need 4 wires and check the diagram here for the connection.
  • RPI pin1(3V3) to INA219 Vcc pin 
  • RPI pin3(SDA) to INA219 Sda pin
  • RPI pin5(SCL) to INA219 Scl pin
  • RPI pin9(GND) to INA219 Gnd pin

2. Install Python module
For those people using Python v3, you can use CircuitPython module.
You can follow the same Adafruit page above and there are sample codes.
I am using Python v2, so I used the library called pi-ina219.
Simple command. Just run below to install the library.

$ sudo pip install pi-ina219
3. Sample code test
pi-ina219 library page has sample code.
I simply tried the first sample and it works.
You will see the 4 printed values in your terminal.
If you see errors, make sure wires are connected properly.

Here is 2 more test codes that I basically added to the above sample code.

Reading continuously:

#!/usr/bin/env python
from ina219 import INA219
from ina219 import DeviceRangeError

SHUNT_OHMS = 0.1
ina = INA219(SHUNT_OHMS)
ina.configure()

def read():

    BV = ina.voltage()
    BC = ina.current()
    PW = ina.power()
    SV = ina.shunt_voltage()
    print("Bus Voltage: %.3f V" % BV)
    try:
       print("Bus Voltage: %.3f V" % BV)
        print("Bus Current: %.3f mA" % BC)
        print("Power: %.3f mW" % PW)
        print("Shunt voltage: %.3f mV" % SV)
    ina.sleep()
    time.sleep(3)
    ina.wake()
    except DeviceRangeError as e:
        # Current out of device range with specified shunt resister
        print(e)


if __name__ == "__main__":
    while True:
        read()

Logging data:

#!/usr/bin/env python
from ina219 import INA219
from ina219 import DeviceRangeError
import time
import csv
import os

SHUNT_OHMS = 0.1
ina = INA219(SHUNT_OHMS)
ina.configure()

file = open("log.csv", "a")

def read():

    IV = ina.supply_voltage()
    BV = ina.voltage()
    BC = ina.current()
    PW = ina.power()
    SV = ina.shunt_voltage()
    print("Supply Voltage: %.3f V" % IV)
    try:
       print("Bus Voltage: %.3f V" % BV)
        print("Bus Current: %.3f mA" % BC)
        print("Power: %.3f mW" % PW)
        print("Shunt voltage: %.3f mV" % SV)
        with open("log.csv", 'a') as log:
        writer = csv.writer(log, delimiter=",")
        writer.writerow([time.time(), IV, BV, BC, PW, SV])
    ina.sleep()
    time.sleep(3)
    ina.wake()
    except DeviceRangeError as e:
        # Current out of device range with specified shunt resister
        print(e)


if __name__ == "__main__":
    if os.stat("log.csv").st_size == 0:
        file.write("Time, Supply Voltage(V), Bus Voltage(V), Bus Current(mA), Power(mW), Shunt Voltage(mV)\n")
    file.close()
    while True:
        read()


Raspberry Pi Power Monitor

Power Consumption Monitor using Raspberry Pi Sometimes I get tired of using test equipment to monitor the current and/or power...