pi-ina219

This Python library supports the INA219 voltage, current and power monitor from Texas Instruments with a Raspberry Pi using the I2C bus. The intent of the library is to make it easy to use the quite complex functionality of this sensor.

https://github.com/chrisb2/pi_ina219

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.1%) to scientific vocabulary

Keywords

beaglebone-black ina-219 lopy4 python python-library raspberry-pi
Last synced: 6 months ago · JSON representation

Repository

This Python library supports the INA219 voltage, current and power monitor from Texas Instruments with a Raspberry Pi using the I2C bus. The intent of the library is to make it easy to use the quite complex functionality of this sensor.

Basic Info
  • Host: GitHub
  • Owner: chrisb2
  • License: mit
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 54.7 KB
Statistics
  • Stars: 116
  • Watchers: 14
  • Forks: 38
  • Open Issues: 7
  • Releases: 4
Topics
beaglebone-black ina-219 lopy4 python python-library raspberry-pi
Created over 9 years ago · Last pushed over 2 years ago
Metadata Files
Readme License

README.md

Raspberry Pi Python Library for Voltage and Current Sensors Using the INA219

Build

codecov

PyPI version

This Python library supports the INA219 voltage, current and power monitor sensor from Texas Instruments on Python 3. The intent of the library is to make it easy to use the quite complex functionality of this sensor.

The library currently only supports continuous reads of voltage and power, but not triggered reads.

The library supports the detection of overflow in the current/power calculations which results in meaningless values for these readings.

The low power mode of the INA219 is supported, so if only occasional reads are being made in a battery based system, current consumption can be minimised.

The library has been tested with the Adafruit INA219 Breakout.

Installation and Upgrade

This library and its dependency (Adafruit GPIO library) can be installed from PyPI by executing:

shell sudo pip3 install pi-ina219

To upgrade from a previous version installed direct from Github execute:

shell sudo pip3 uninstall pi-ina219 sudo pip3 install pi-ina219

The Adafruit library supports the I2C protocol on all versions of the Raspberry Pi. Remember to enable the I2C bus under the Advanced Options of raspi-config.

Usage

The address of the sensor unless otherwise specified is the default of 0x40.

Note that the bus voltage is that on the load side of the shunt resistor, if you want the voltage on the supply side then you should add the bus voltage and shunt voltage together, or use the supplyvoltage()_ function.

I2C Bus number

In most cases this will be determined automatically, however if this fails you will see the exception: Could not determine default I2C bus for platform In this case just set the bus number in the INA219 constructor, for example: ina = INA219(SHUNT_OHMS, busnum=1) This is known to be required with Raspberry Pi 4 and the 'Bullseye' (October 2021) Raspberry Pi OS.

Simple - Auto Gain

This mode is great for getting started, as it will provide valid readings until the device current capability is exceeded for the value of the shunt resistor connected (3.2A for 0.1Ω shunt resistor). It does this by automatically adjusting the gain as required until the maximum is reached, when a DeviceRangeError exception is thrown to avoid invalid readings being taken.

The downside of this approach is reduced current and power resolution.

```python

!/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 resistor
    print(e)

if name == "main": read() ```

Advanced - Auto Gain, High Resolution

In this mode by understanding the maximum current expected in your system and specifying this in the script you can achieve the best possible current and power resolution. The library will calculate the best gain to achieve the highest resolution based on the maximum expected current.

In this mode if the current exceeds the maximum specified, the gain will be automatically increased, so a valid reading will still result, but at a lower resolution.

As above when the maximum gain is reached, an exception is thrown to avoid invalid readings being taken.

```python

!/usr/bin/env python

from ina219 import INA219 from ina219 import DeviceRangeError

SHUNTOHMS = 0.1 MAXEXPECTED_AMPS = 0.2

def read(): ina = INA219(SHUNTOHMS, MAXEXPECTEDAMPS) ina.configure(ina.RANGE16V)

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 resistor
    print(e)

if name == "main": read() ```

Advanced - Manual Gain, High Resolution

In this mode by understanding the maximum current expected in your system and specifying this and the gain in the script you can always achieve the best possible current and power resolution, at the price of missing current and power values if a current overflow occurs.

```python

!/usr/bin/env python

from ina219 import INA219 from ina219 import DeviceRangeError

SHUNTOHMS = 0.1 MAXEXPECTED_AMPS = 0.2

def read(): ina = INA219(SHUNTOHMS, MAXEXPECTEDAMPS) ina.configure(ina.RANGE16V, ina.GAIN140MV)

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:
    print("Current overflow")

if name == "main": read() ```

Sensor Address

The sensor address may be altered as follows:

python ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS, address=0x41)

Low Power Mode

The sensor may be put in low power mode between reads as follows:

python ina.configure(ina.RANGE_16V) while True: print("Voltage : %.3f V" % ina.voltage()) ina.sleep() time.sleep(60) ina.wake()

Note that if you do not wake the device after sleeping, the value returned from a read will be the previous value taken before sleeping.

Functions

  • INA219() constructs the class. The arguments, are:
    • shunt_ohms: The value of the shunt resistor in Ohms (mandatory).
    • maxexpectedamps: The maximum expected current in Amps (optional).
    • busnum: The I2C bus number for the device platform, defaults to auto detects 0 or 1 for Raspberry Pi or Beaglebone Black (optional).
    • address: The I2C address of the INA219, defaults to 0x40 (optional).
    • loglevel: Set to _logging.INFO to see the detailed calibration calculations and logging.DEBUG to see register operations (optional).
  • configure() configures and calibrates how the INA219 will take measurements. The arguments, which are all optional, are:
    • voltage_range: The full scale voltage range, this is either 16V or 32V, represented by one of the following constants (optional).
    • RANGE_16V: Range zero to 16 volts
    • RANGE_32V: Range zero to 32 volts (default). Device only supports up to 26V.
    • gain: The gain, which controls the maximum range of the shunt voltage, represented by one of the following constants (optional).
    • GAIN140MV: Maximum shunt voltage 40mV
    • GAIN280MV: Maximum shunt voltage 80mV
    • GAIN4160MV: Maximum shunt voltage 160mV
    • GAIN8320MV: Maximum shunt voltage 320mV
    • GAIN_AUTO: Automatically calculate the gain (default)
    • bus_adc: The bus ADC resolution (9, 10, 11, or 12-bit), or set the number of samples used when averaging results, represented by one of the following constants (optional).
    • ADC_9BIT: 9 bit, conversion time 84us.
    • ADC_10BIT: 10 bit, conversion time 148us.
    • ADC_11BIT: 11 bit, conversion time 276us.
    • ADC_12BIT: 12 bit, conversion time 532us (default).
    • ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.
    • ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.
    • ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.
    • ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms
    • ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.
    • ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.
    • ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.
    • shunt_adc: The shunt ADC resolution (9, 10, 11, or 12-bit), or set the number of samples used when averaging results, represented by one of the following constants (optional).
    • ADC_9BIT: 9 bit, conversion time 84us.
    • ADC_10BIT: 10 bit, conversion time 148us.
    • ADC_11BIT: 11 bit, conversion time 276us.
    • ADC_12BIT: 12 bit, conversion time 532us (default).
    • ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.
    • ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.
    • ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.
    • ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms
    • ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.
    • ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.
    • ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.
  • voltage() Returns the bus voltage in volts (V).
  • supply_voltage() Returns the bus supply voltage in volts (V). This is the sum of the bus voltage and shunt voltage. A DeviceRangeError exception is thrown if current overflow occurs.
  • current() Returns the bus current in milliamps (mA). A DeviceRangeError exception is thrown if current overflow occurs.
  • power() Returns the bus power consumption in milliwatts (mW). A DeviceRangeError exception is thrown if current overflow occurs.
  • shunt_voltage() Returns the shunt voltage in millivolts (mV). A DeviceRangeError exception is thrown if current overflow occurs.
  • current_overflow() Returns 'True' if an overflow has occured. Alternatively handle the DeviceRangeError exception as shown in the examples above.
  • sleep() Put the INA219 into power down mode.
  • wake() Wake the INA219 from power down mode.
  • reset() Reset the INA219 to its default configuration.
  • is_conversion_ready() check if conversion was done before reading the next measurement results.

Performance

On a Raspberry Pi 2 Model B running Raspbian Jesse and reading a 12-bit voltage in a loop, a read occurred approximately every 10 milliseconds.

On a Raspberry Pi 4 running Raspbian Buster a read occurred approximately every 570 microseconds.

Debugging

To understand the calibration calculation results and automatic gain increases, informational output can be enabled with:

python ina = INA219(SHUNT_OHMS, log_level=logging.INFO)

Detailed logging of device register operations can be enabled with:

python ina = INA219(SHUNT_OHMS, log_level=logging.DEBUG)

Testing

Install the library as described above, this will install all the dependencies required for the unit tests, as well as the library itself. Clone the library source from Github then execute the test suite from the top level directory with:

shell python3 -m unittest discover -s tests -p 'test_*.py'

A single unit test class may be run as follows:

shell python3 -m unittest tests.test_configuration.TestConfiguration

Code coverage metrics may be generated and viewed with:

shell coverage run --branch --source=ina219 -m unittest discover -s tests -p 'test_*.py' coverage report -m

Coding Standard

This library adheres to the PEP8 standard and follows the idiomatic style described in the book Writing Idiomatic Python by Jeff Knupp.

Owner

  • Name: Chris Borrill
  • Login: chrisb2
  • Kind: user
  • Location: Christchurch, New Zealand

GitHub Events

Total
  • Watch event: 2
  • Fork event: 3
Last Year
  • Watch event: 2
  • Fork event: 3

Committers

Last synced: 9 months ago

All Time
  • Total Commits: 65
  • Total Committers: 7
  • Avg Commits per committer: 9.286
  • Development Distribution Score (DDS): 0.108
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Chris Borrill c****l@g****m 58
Andreas Tobola g****b@t****e 2
Trick van Staveren t****k@v****s 1
Thomas Kluyver t****l@g****m 1
Ted Timmons t****d@p****t 1
Steve Bauer e****0 1
John S j****n@j****m 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 8 months ago

All Time
  • Total issues: 29
  • Total pull requests: 13
  • Average time to close issues: about 1 month
  • Average time to close pull requests: 3 months
  • Total issue authors: 24
  • Total pull request authors: 9
  • Average comments per issue: 3.93
  • Average comments per pull request: 2.23
  • Merged pull requests: 10
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • chrisb2 (5)
  • Gbassous (2)
  • rrnicolay (1)
  • csreades (1)
  • BritishTechGuru (1)
  • DotEXEGaming (1)
  • abeng94 (1)
  • SmartTransp (1)
  • JennaSys (1)
  • ibyng (1)
  • msi-matthew (1)
  • Micha-123 (1)
  • andreagilardoni (1)
  • skibum-za (1)
  • mschlenstedt (1)
Pull Request Authors
  • chrisb2 (3)
  • Tobola (2)
  • janoskut (2)
  • trickv (1)
  • eXodus1440 (1)
  • tedder (1)
  • takluyver (1)
  • JennaSys (1)
  • renedis (1)
Top Labels
Issue Labels
bug (3) enhancement (2)
Pull Request Labels

Packages

  • Total packages: 2
  • Total downloads:
    • pypi 1,996 last-month
  • Total dependent packages: 2
    (may contain duplicates)
  • Total dependent repositories: 17
    (may contain duplicates)
  • Total versions: 12
  • Total maintainers: 1
pypi.org: pi-ina219

This Python library for Raspberry Pi makes it easy to leverage the complex functionality of the Texas Instruments INA219 sensor to measure voltage, current and power.

  • Versions: 5
  • Dependent Packages: 2
  • Dependent Repositories: 17
  • Downloads: 1,996 Last month
  • Docker Downloads: 0
Rankings
Dependent packages count: 3.1%
Dependent repos count: 3.5%
Docker downloads count: 4.0%
Average: 4.9%
Downloads: 5.2%
Forks count: 6.7%
Stargazers count: 7.0%
Maintainers (1)
Last synced: 6 months ago
proxy.golang.org: github.com/chrisb2/pi_ina219
  • Versions: 7
  • Dependent Packages: 0
  • Dependent Repositories: 0
Rankings
Dependent packages count: 6.5%
Average: 6.7%
Dependent repos count: 6.9%
Last synced: 6 months ago

Dependencies

tests/requirements.txt pypi
  • Adafruit_GPIO ==1.0.1 test
  • mock ==2.0.0 test
.github/workflows/python-package.yml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
  • codecov/codecov-action v3 composite
setup.py pypi