{"id":15020686,"url":"https://github.com/chrisb2/pi_ina219","last_synced_at":"2025-09-12T12:43:49.403Z","repository":{"id":41168808,"uuid":"72261645","full_name":"chrisb2/pi_ina219","owner":"chrisb2","description":"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.","archived":false,"fork":false,"pushed_at":"2023-06-23T19:47:51.000Z","size":56,"stargazers_count":113,"open_issues_count":7,"forks_count":34,"subscribers_count":16,"default_branch":"master","last_synced_at":"2024-10-30T03:31:23.633Z","etag":null,"topics":["beaglebone-black","ina-219","lopy4","python","python-library","raspberry-pi"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chrisb2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-10-29T03:02:06.000Z","updated_at":"2024-08-06T05:38:04.000Z","dependencies_parsed_at":"2024-06-18T20:14:37.297Z","dependency_job_id":null,"html_url":"https://github.com/chrisb2/pi_ina219","commit_stats":{"total_commits":65,"total_committers":8,"mean_commits":8.125,"dds":"0.12307692307692308","last_synced_commit":"5eb0868b6afd5879b656cbcbbb377d002e414e58"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisb2%2Fpi_ina219","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisb2%2Fpi_ina219/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisb2%2Fpi_ina219/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chrisb2%2Fpi_ina219/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chrisb2","download_url":"https://codeload.github.com/chrisb2/pi_ina219/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247595334,"owners_count":20963943,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["beaglebone-black","ina-219","lopy4","python","python-library","raspberry-pi"],"created_at":"2024-09-24T19:55:26.167Z","updated_at":"2025-04-07T05:09:36.473Z","avatar_url":"https://github.com/chrisb2.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Raspberry Pi Python Library for Voltage and Current Sensors Using the INA219\n\n[![Build](https://github.com/chrisb2/pi_ina219/actions/workflows/python-package.yml/badge.svg)](https://github.com/chrisb2/pi_ina219/actions/workflows/python-package.yml)\n\n[![codecov](https://codecov.io/gh/chrisb2/pi_ina219/branch/master/graph/badge.svg)](https://codecov.io/gh/chrisb2/pi_ina219)\n\n[![PyPI version](https://badge.fury.io/py/pi-ina219.svg)](https://badge.fury.io/py/pi-ina219)\n\nThis Python library supports the [INA219](http://www.ti.com/lit/ds/symlink/ina219.pdf)\nvoltage, current and power monitor sensor from Texas Instruments on Python 3. The intent of the library is to make it easy to use the\nquite complex functionality of this sensor.\n\nThe library currently only supports _continuous_ reads of voltage and\npower, but not _triggered_ reads.\n\nThe library supports the detection of _overflow_ in the current/power\ncalculations which results in meaningless values for these readings.\n\nThe low power mode of the INA219 is supported, so if only occasional\nreads are being made in a battery based system, current consumption can\nbe minimised.\n\nThe library has been tested with the\n[Adafruit INA219 Breakout](https://www.adafruit.com/products/904).\n\n## Installation and Upgrade\n\nThis library and its dependency\n([Adafruit GPIO library](https://github.com/adafruit/Adafruit_Python_GPIO))\ncan be installed from PyPI by executing:\n\n```shell\nsudo pip3 install pi-ina219\n```\n\nTo upgrade from a previous version installed direct from Github execute:\n\n```shell\nsudo pip3 uninstall pi-ina219\nsudo pip3 install pi-ina219\n```\n\nThe Adafruit library supports the I2C protocol on all versions of the\nRaspberry Pi. Remember to enable the I2C bus under the _Advanced Options_\nof _raspi-config_.\n\n## Usage\n\nThe address of the sensor unless otherwise specified is the default\nof _0x40_.\n\nNote that the bus voltage is that on the load side of the shunt resistor,\nif you want the voltage on the supply side then you should add the bus\nvoltage and shunt voltage together, or use the _supply_voltage()_\nfunction.\n\n### I2C Bus number\n\nIn most cases this will be determined automatically, however if this fails you will see the exception:\n```\nCould not determine default I2C bus for platform\n```\nIn this case just set the bus number in the INA219 constructor, for example:\n```\nina = INA219(SHUNT_OHMS, busnum=1)\n```\nThis is known to be required with Raspberry Pi 4 and the 'Bullseye' (October 2021) Raspberry Pi OS.\n\n### Simple - Auto Gain\n\nThis mode is great for getting started, as it will provide valid readings\nuntil the device current capability is exceeded for the value of the\nshunt resistor connected (3.2A for 0.1\u0026Omega; shunt resistor). It does this by\nautomatically adjusting the gain as required until the maximum is reached,\nwhen a _DeviceRangeError_ exception is thrown to avoid invalid readings being taken.\n\nThe downside of this approach is reduced current and power resolution.\n\n```python\n#!/usr/bin/env python\nfrom ina219 import INA219\nfrom ina219 import DeviceRangeError\n\nSHUNT_OHMS = 0.1\n\n\ndef read():\n    ina = INA219(SHUNT_OHMS)\n    ina.configure()\n\n    print(\"Bus Voltage: %.3f V\" % ina.voltage())\n    try:\n        print(\"Bus Current: %.3f mA\" % ina.current())\n        print(\"Power: %.3f mW\" % ina.power())\n        print(\"Shunt voltage: %.3f mV\" % ina.shunt_voltage())\n    except DeviceRangeError as e:\n        # Current out of device range with specified shunt resistor\n        print(e)\n\n\nif __name__ == \"__main__\":\n    read()\n```\n\n### Advanced - Auto Gain, High Resolution\n\nIn this mode by understanding the maximum current expected in your system\nand specifying this in the script you can achieve the best possible current\nand power resolution. The library will calculate the best gain to achieve\nthe highest resolution based on the maximum expected current.\n\nIn this mode if the current exceeds the maximum specified, the gain will\nbe automatically increased, so a valid reading will still result, but at\na lower resolution.\n\nAs above when the maximum gain is reached, an exception is thrown to\navoid invalid readings being taken.\n\n```python\n#!/usr/bin/env python\nfrom ina219 import INA219\nfrom ina219 import DeviceRangeError\n\nSHUNT_OHMS = 0.1\nMAX_EXPECTED_AMPS = 0.2\n\n\ndef read():\n    ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)\n    ina.configure(ina.RANGE_16V)\n\n    print(\"Bus Voltage: %.3f V\" % ina.voltage())\n    try:\n        print(\"Bus Current: %.3f mA\" % ina.current())\n        print(\"Power: %.3f mW\" % ina.power())\n        print(\"Shunt voltage: %.3f mV\" % ina.shunt_voltage())\n    except DeviceRangeError as e:\n        # Current out of device range with specified shunt resistor\n        print(e)\n\n\nif __name__ == \"__main__\":\n    read()\n```\n\n### Advanced - Manual Gain, High Resolution\n\nIn this mode by understanding the maximum current expected in your system\nand specifying this and the gain in the script you can always achieve the\nbest possible current and power resolution, at the price of missing current\nand power values if a current overflow occurs.\n\n```python\n#!/usr/bin/env python\nfrom ina219 import INA219\nfrom ina219 import DeviceRangeError\n\nSHUNT_OHMS = 0.1\nMAX_EXPECTED_AMPS = 0.2\n\n\ndef read():\n    ina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS)\n    ina.configure(ina.RANGE_16V, ina.GAIN_1_40MV)\n\n    print(\"Bus Voltage: %.3f V\" % ina.voltage())\n    try:\n        print(\"Bus Current: %.3f mA\" % ina.current())\n        print(\"Power: %.3f mW\" % ina.power())\n        print(\"Shunt voltage: %.3f mV\" % ina.shunt_voltage())\n    except DeviceRangeError as e:\n        print(\"Current overflow\")\n\n\nif __name__ == \"__main__\":\n    read()\n```\n\n### Sensor Address\n\nThe sensor address may be altered as follows:\n\n```python\nina = INA219(SHUNT_OHMS, MAX_EXPECTED_AMPS, address=0x41)\n```\n\n### Low Power Mode\n\nThe sensor may be put in low power mode between reads as follows:\n\n```python\nina.configure(ina.RANGE_16V)\nwhile True:\n    print(\"Voltage : %.3f V\" % ina.voltage())\n    ina.sleep()\n    time.sleep(60)\n    ina.wake()\n```\n\nNote that if you do not wake the device after sleeping, the value\nreturned from a read will be the previous value taken before sleeping.\n\n## Functions\n\n- `INA219()` constructs the class.\n  The arguments, are:\n  - shunt_ohms: The value of the shunt resistor in Ohms (mandatory).\n  - max_expected_amps: The maximum expected current in Amps (optional).\n  - busnum: The I2C bus number for the device platform, defaults to _auto detects 0 or 1 for Raspberry Pi or Beaglebone Black_ (optional).\n  - address: The I2C address of the INA219, defaults to _0x40_ (optional).\n  - log_level: Set to _logging.INFO_ to see the detailed calibration calculations and _logging.DEBUG_ to see register operations (optional).\n- `configure()` configures and calibrates how the INA219 will take measurements.\n  The arguments, which are all optional, are:\n  - voltage_range: The full scale voltage range, this is either 16V or 32V,\n  represented by one of the following constants (optional).\n    - RANGE_16V: Range zero to 16 volts\n    - RANGE_32V: Range zero to 32 volts (**default**). **Device only supports up to 26V.**\n  - gain: The gain, which controls the maximum range of the shunt voltage,\n  represented by one of the following constants (optional).\n    - GAIN_1_40MV: Maximum shunt voltage 40mV\n    - GAIN_2_80MV: Maximum shunt voltage 80mV\n    - GAIN_4_160MV: Maximum shunt voltage 160mV\n    - GAIN_8_320MV: Maximum shunt voltage 320mV\n    - GAIN_AUTO: Automatically calculate the gain (**default**)\n  - bus_adc: The bus ADC resolution (9, 10, 11, or 12-bit), or\n  set the number of samples used when averaging results, represented by\n  one of the following constants (optional).\n    - ADC_9BIT: 9 bit, conversion time 84us.\n    - ADC_10BIT: 10 bit, conversion time 148us.\n    - ADC_11BIT: 11 bit, conversion time 276us.\n    - ADC_12BIT: 12 bit, conversion time 532us (**default**).\n    * ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.\n    * ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.\n    * ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.\n    * ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms\n    * ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.\n    * ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.\n    * ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.\n  * shunt_adc: The shunt ADC resolution (9, 10, 11, or 12-bit), or\n  set the number of samples used when averaging results, represented by\n  one of the following constants (optional).\n    * ADC_9BIT: 9 bit, conversion time 84us.\n    * ADC_10BIT: 10 bit, conversion time 148us.\n    * ADC_11BIT: 11 bit, conversion time 276us.\n    * ADC_12BIT: 12 bit, conversion time 532us (**default**).\n    * ADC_2SAMP: 2 samples at 12 bit, conversion time 1.06ms.\n    * ADC_4SAMP: 4 samples at 12 bit, conversion time 2.13ms.\n    * ADC_8SAMP: 8 samples at 12 bit, conversion time 4.26ms.\n    * ADC_16SAMP: 16 samples at 12 bit, conversion time 8.51ms\n    * ADC_32SAMP: 32 samples at 12 bit, conversion time 17.02ms.\n    * ADC_64SAMP: 64 samples at 12 bit, conversion time 34.05ms.\n    * ADC_128SAMP: 128 samples at 12 bit, conversion time 68.10ms.\n- `voltage()` Returns the bus voltage in volts (V).\n- `supply_voltage()` Returns the bus supply voltage in volts (V). This\n  is the sum of the bus voltage and shunt voltage. A _DeviceRangeError_\n  exception is thrown if current overflow occurs.\n- `current()` Returns the bus current in milliamps (mA).\n  A _DeviceRangeError_ exception is thrown if current overflow occurs.\n- `power()` Returns the bus power consumption in milliwatts (mW).\n  A _DeviceRangeError_ exception is thrown if current overflow occurs.\n- `shunt_voltage()` Returns the shunt voltage in millivolts (mV).\n  A _DeviceRangeError_ exception is thrown if current overflow occurs.\n- `current_overflow()` Returns 'True' if an overflow has\n  occured. Alternatively handle the _DeviceRangeError_ exception\n  as shown in the examples above.\n- `sleep()` Put the INA219 into power down mode.\n- `wake()` Wake the INA219 from power down mode.\n- `reset()` Reset the INA219 to its default configuration.\n- `is_conversion_ready()` check if conversion was done before reading the next measurement results.\n\n## Performance\n\nOn a Raspberry Pi 2 Model B running Raspbian Jesse and reading a 12-bit\nvoltage in a loop, a read occurred approximately every 10 milliseconds.\n\nOn a Raspberry Pi 4 running Raspbian Buster a read occurred approximately every 570 microseconds.\n\n## Debugging\n\nTo understand the calibration calculation results and automatic gain\nincreases, informational output can be enabled with:\n\n```python\n    ina = INA219(SHUNT_OHMS, log_level=logging.INFO)\n```\n\nDetailed logging of device register operations can be enabled with:\n\n```python\n    ina = INA219(SHUNT_OHMS, log_level=logging.DEBUG)\n```\n\n## Testing\n\nInstall the library as described above, this will install all the\ndependencies required for the unit tests, as well as the library\nitself. Clone the library source from Github then execute the test suite\nfrom the top level directory with:\n\n```shell\npython3 -m unittest discover -s tests -p 'test_*.py'\n```\n\nA single unit test class may be run as follows:\n\n```shell\npython3 -m unittest tests.test_configuration.TestConfiguration\n```\n\nCode coverage metrics may be generated and viewed with:\n\n```shell\ncoverage run --branch --source=ina219 -m unittest discover -s tests -p 'test_*.py'\ncoverage report -m\n```\n\n## Coding Standard\n\nThis library adheres to the _PEP8_ standard and follows the _idiomatic_\nstyle described in the book _Writing Idiomatic Python_ by _Jeff Knupp_.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisb2%2Fpi_ina219","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchrisb2%2Fpi_ina219","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchrisb2%2Fpi_ina219/lists"}