I needed to monitor the state of the battery powering a Raspberry Pi system, to optimize its power consumption and shut it down before the battery expired. The existing Python libraries did not make it easy to leverage the quite complex functionality of the Texas Instruments INA219 sensor, so I decided to write my own.
The ScriptThe following Python script demonstrates basic usage of this library with a 0.1Ω shunt resistor, a range of 16 volts and a maximum expected current of 200mA.
#!/usr/bin/env python from ina219
import INA219
SHUNT_OHMS = 0.1
MAX_EXPECTED_AMPS = 0.2
def read():
ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)
ina.configure(ina.RANGE_16V)
print "Bus Voltage: %.3f V" % ina.voltage()
print "Bus Current: %.3f mA" % ina.current()
print "Power: %.3f mW" % ina.power()
print "Shunt voltage: %.3f mV" % ina.shunt_voltage()
if __name__ == "__main__":
read()
A detailed description of the functionality of the library, installation instructions and more code examples can be found in the project README.
Although I have listed the Raspberry Pi 3 and Adafruit INA219 breakout in the hardware section, these are just examples, the library will work with any Raspberry Pi or INA219 sensor.
The library has a set of unit tests, so extending its functionality should be easy. I am happy to accept contributions in the form of pull requests.
I have added integration with Travis CI so the unit tests are now being run automatically using Python 2 and 3 after each change to master, see https://travis-ci.org/chrisb2/pi_ina219.
DemoA really great You Tube video by rdagger68 which describes using this library in a more complex project is;
If you are currently using this library I recommend you upgrade to at least v1.0.3 to fix an issue with invalid current and power readings if low values of max expected amps are specified (Issue #4). To upgrade follow the Installation and Upgrade instructions. To see what version you currently have execute:
pip show pi-ina219
Version 1.1.0 of the INA219 library is now available with three major enhancements:
- Support for automatic gain adjustment, this makes getting valid readings over the full current/power range of the device very easy. Automatic gain is the default mode.
- The calibration calculation has been altered so that current overflow conditions are always detected so its no longer possible to obtain invalid current, power or shunt voltage readings.
- This version (v1.1.0) of the library is available in the PyPI Python Package Index (https://pypi.python.org/pypi/pi-ina219), which allows a more standard installation process.
At its simplest the following code is all thats required to get readings for the full supported current/power range of the device. See the README for further details.
#!/usr/bin/env python
from ina219 import INA219
from ina219 import DeviceRangeError
SHUNT_OHMS = 0.1
def read():
ina = INA219(SHUNT_OHMS)
ina.configure()
print "Bus Voltage: %.3f V" % ina.voltage()
try:
print "Bus Current: %.3f mA" % ina.current()
print "Power: %.3f mW" % ina.power()
print "Shunt voltage: %.3f mV" % ina.shunt_voltage()
except DeviceRangeError as e:
# Current out of device range with specified shunt resister
print e
if __name__ == "__main__":
read()
Comments