Close
0%
0%

Raspberry Pi INA219 voltage/current sensor library

Python library for Raspberry Pi which provides an easy to use interface for the INA219 voltage and current sensor from Texas Instruments.

Similar projects worth following
This Python library which is compatible Python 2 and 3, makes it easy to leverage the quite complex functionality of the Texas Instruments INA219 sensor.

Although I describe using the Raspberry Pi 3 and Adafruit INA219 breakout, 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.

The 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.

  • 1 × Raspberry Pi
  • 1 × Adafruit INA219 Breakout

  • Porting to MicroPython

    Chris05/17/2017 at 07:43 0 comments

    I am working on porting this library to MicroPython on the pyboard. Currently the functionality is working correctly based on manual testing and I am looking to port the unit tests.

    Take a look at the project here.

  • Automatic Gain and Current Overflow Detection

    Chris03/02/2017 at 07:43 0 comments

    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.

    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()

  • Recommended upgrade to fix invalid current and power readings

    Chris02/21/2017 at 07:17 0 comments

    If you are currently using this library I recommend you upgrade to v1.0.3 which fixes an issue with invalid current and power readings if low values of max expected amps are specified (Issue #4).

    To see what version you currently have execute:

    pip show pi-ina219

    To upgrade simply execute:

    sudo pip install git+git://github.com/chrisb2/pi_ina219.git
    

  • Unit Test Code Coverage

    Chris02/18/2017 at 09:29 0 comments

    I have updated the Travis CI configuration to automatically created a code coverage report on every build and upload it to 'codecov', see https://codecov.io/gh/chrisb2/pi_ina219. The code coverage is currently 97.7%.

    I have added 'badges' showing the build status and code coverage to the README:

View all 4 project logs

  • 1
    Step 1

    This great You Tube video by rdagger68 decribes how to connect the AdaFruit INA219 Breakout to the Raspberry Pi and also describes using this library to take measurements:

View all instructions

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates