{"id":13802456,"url":"https://github.com/elschopi/TI_INA226_micropython","last_synced_at":"2025-05-13T13:31:20.121Z","repository":{"id":188490260,"uuid":"272348219","full_name":"elschopi/TI_INA226_micropython","owner":"elschopi","description":"Micropython driver for Texas Instruments INA226 power measuring IC","archived":false,"fork":false,"pushed_at":"2022-03-26T14:43:33.000Z","size":33,"stargazers_count":27,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-04T00:07:14.369Z","etag":null,"topics":["ina226","micropython","micropython-esp32","micropython-esp8266","power-measurement","texas-instruments"],"latest_commit_sha":null,"homepage":"https://github.com/elschopi/TI_INA226_micropython","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/elschopi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2020-06-15T05:16:22.000Z","updated_at":"2024-07-10T12:27:47.000Z","dependencies_parsed_at":"2023-08-19T02:18:48.034Z","dependency_job_id":null,"html_url":"https://github.com/elschopi/TI_INA226_micropython","commit_stats":null,"previous_names":["elschopi/ti_ina226_micropython"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elschopi%2FTI_INA226_micropython","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elschopi%2FTI_INA226_micropython/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elschopi%2FTI_INA226_micropython/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/elschopi%2FTI_INA226_micropython/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/elschopi","download_url":"https://codeload.github.com/elschopi/TI_INA226_micropython/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225218063,"owners_count":17439713,"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":["ina226","micropython","micropython-esp32","micropython-esp8266","power-measurement","texas-instruments"],"created_at":"2024-08-04T00:01:44.893Z","updated_at":"2024-11-18T17:31:27.423Z","avatar_url":"https://github.com/elschopi.png","language":"Python","funding_links":[],"categories":["Libraries"],"sub_categories":["Sensors"],"readme":"# TI_INA226_micropython\n\nThis library provides support for the TI INA226 power measurement IC with micropython firmware.\nDatasheet and other information on the IC: https://www.ti.com/product/INA226\n#  \nThis library is derived from https://github.com/robert-hh/INA219 \u003c/br\u003e\nwith the friendly support of the community at https://forum.micropython.org/\n## Motivation\nI needed a micropython library for the INA226 devices for a small power meter project I was working on. I did find libraries for the \nRaspberry Pi, but none for micropython. Thus, I had to modify an existing library for the INA219 devices.\n\n# Basics\n\nTo use the device, it has to be configured at startup. In it's default configuration, the calibration register is not set and \nthus the current and power cannot be directly read out.\u003c/br\u003e\nBy default, this library configures the device to a maximum current of 3.6 A and 36V bus voltage. Resistance of the shunt is assumed as 0.1 Ohm.\n\n# Calculations\n\nThe following values need to be calculated in order to set the configuration and calibration register values:\n- calibration register value\n- power LSB value\n- current LSB value\n- configuration register value\nConfiguration register value is derived from the values of the corresponding bits.\n\nDefault configuration register is as follows:\n|BitNr \t|\tD15\t|\tD14\t|\tD13\t|\tD12\t|\tD11\t|\tD10\t|\tD09\t|\tD08\t|\tD07\t|\tD06\t|\tD05\t|\tD04\t|\tD03\t|\tD02\t|\tD01\t|\tD00\t|\n|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|---\t|\n|Name  \t|RST\t|N/A\t|N/A\t|N/A\t|AVG2\t|AVG1\t|AVG0\t|VBUSCT2|VBUSCT1|VBUSCT0|VSHCT2\t|VSHCT1\t|VSHCT0\t|MODE3\t|MODE2\t|MODE1\t|\n|Value \t|\t0\t|\t1\t|\t0\t|\t0\t|\t0\t|\t0\t|\t0\t|\t1\t|\t0\t|\t0\t|\t1\t|\t0\t|\t0\t|\t1\t|\t1\t|\t1\t|\n\nDefault configuration according to the datasheet:\n- Averaging mode: 1 sample\n- Bus voltage conversion time: 1.1ms\n- Shunt voltage conversion time: 1.1ms\n- Operating mode: Shunt and Bus voltage, continuous\n\nPossible values for the configuration registers can be found in the library.\n\n## Calculating the current_LSB\nAs example, a maximum expected current of 3.6A is assumed.\n\ncurrent_LSB = max_expected_I / (2^15)\u003c/br\u003e\ncurrent_LSB = 3.6 A / (2^15)\u003c/br\u003e\ncurrent_LSB = 0.0001098632813 = 0.00011 = 0.0001 -\u003e 100uA/bit\u003c/br\u003e\n\n## Calculating the calibration register\n\nCal_value = 0.00512 / (current_LSB * Rshunt)\u003c/br\u003e\nCal_value = 0.00512 / (0.0001 * 0.1)\u003c/br\u003e\nCal_value = 512\u003c/br\u003e\n\n## Calculating the power_LSB\n\npower_LSB = 25 * current_LSB\u003c/br\u003e\npower_LSB = 25 * 0.0001 = 0.0025 -\u003e 2.5mW/bit\u003c/br\u003e\n\n# Usage information\nIn order to be able to set calibration and configuration to custom values, some work needs to be done by the user\u003c/br\u003e\nIn the library, a method \"set_calibration_custom\" exists, which expects the calibration register value and the \u003c/br\u003e\nconfiguration register value as arguments. If no arguments are given, it uses default values.\u003c/br\u003e\nFor easier calculation, a Google spreadsheet is available at [this Google spreadsheet](https://docs.google.com/spreadsheets/d/1k0MbBsduRgoQ8huFrBwaHQnYhl97LjkpEw-sIXykTEg/edit?usp=sharing \"INA226 Google Spreadsheet\")\u003c/br\u003e\nWith ina_calc_config.py I've provided a crude, menu guided configuration calculator for Python3. It's a bit rough around the edges but works.\n\n## Default values\n`cal_value = 512`\u003c/br\u003e\n`current_lsb = 0.0001`\u003c/br\u003e\n`power_lsb = 0.0025`\u003c/br\u003e\n`Rshunt = 0.1`\u003c/br\u003e\n`Averaging mode = 512 samples`\u003c/br\u003e\n`Bus voltage conversion time = 588us`\u003c/br\u003e\n`Shunt voltage conversion time = 588us`\u003c/br\u003e\n`Operating mode = Shunt and Bus Voltage, continuous`\u003c/br\u003e\n\n# Example code\nThis code was written to test (i.e. just see if it works without errors) the library on an ESP01S module with 1MB flash\n\n```python\nimport ina226\nfrom time import sleep\nfrom machine import Pin, I2C\n# i2c\ni2c = I2C(scl=Pin(2), sda=Pin(0))\n# ina226\nina = ina226.INA226(i2c, 0x40)\n# default configuration and calibration value\nina.set_calibration()\nprint(ina.bus_voltage)\nprint(ina.shunt_voltage)\nprint(ina.current)\nprint(ina.power)\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felschopi%2FTI_INA226_micropython","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Felschopi%2FTI_INA226_micropython","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Felschopi%2FTI_INA226_micropython/lists"}