├── .gitattributes ├── .github ├── PULL_REQUEST_TEMPLATE │ └── adafruit_circuitpython_pr.md └── workflows │ ├── build.yml │ ├── failure-help-text.yml │ ├── release_gh.yml │ └── release_pypi.yml ├── .gitignore ├── .pre-commit-config.yaml ├── .readthedocs.yaml ├── CODE_OF_CONDUCT.md ├── LICENSE ├── LICENSES ├── CC-BY-4.0.txt ├── MIT.txt └── Unlicense.txt ├── README.rst ├── README.rst.license ├── adafruit_ina219.py ├── docs ├── _static │ ├── favicon.ico │ └── favicon.ico.license ├── api.rst ├── api.rst.license ├── conf.py ├── examples.rst ├── examples.rst.license ├── index.rst ├── index.rst.license └── requirements.txt ├── examples └── ina219_simpletest.py ├── optional_requirements.txt ├── pyproject.toml ├── requirements.txt └── ruff.toml /.gitattributes: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | .py text eol=lf 6 | .rst text eol=lf 7 | .txt text eol=lf 8 | .yaml text eol=lf 9 | .toml text eol=lf 10 | .license text eol=lf 11 | .md text eol=lf 12 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | Thank you for contributing! Before you submit a pull request, please read the following. 6 | 7 | Make sure any changes you're submitting are in line with the CircuitPython Design Guide, available here: https://docs.circuitpython.org/en/latest/docs/design_guide.html 8 | 9 | If your changes are to documentation, please verify that the documentation builds locally by following the steps found here: https://adafru.it/build-docs 10 | 11 | Before submitting the pull request, make sure you've run Pylint and Black locally on your code. You can do this manually or using pre-commit. Instructions are available here: https://adafru.it/check-your-code 12 | 13 | Please remove all of this text before submitting. Include an explanation or list of changes included in your PR, as well as, if applicable, a link to any related issues. 14 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: Build CI 6 | 7 | on: [pull_request, push] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Run Build CI workflow 14 | uses: adafruit/workflows-circuitpython-libs/build@main 15 | -------------------------------------------------------------------------------- /.github/workflows/failure-help-text.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Scott Shawcroft for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: Failure help text 6 | 7 | on: 8 | workflow_run: 9 | workflows: ["Build CI"] 10 | types: 11 | - completed 12 | 13 | jobs: 14 | post-help: 15 | runs-on: ubuntu-latest 16 | if: ${{ github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event == 'pull_request' }} 17 | steps: 18 | - name: Post comment to help 19 | uses: adafruit/circuitpython-action-library-ci-failed@v1 20 | -------------------------------------------------------------------------------- /.github/workflows/release_gh.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: GitHub Release Actions 6 | 7 | on: 8 | release: 9 | types: [published] 10 | 11 | jobs: 12 | upload-release-assets: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Run GitHub Release CI workflow 16 | uses: adafruit/workflows-circuitpython-libs/release-gh@main 17 | with: 18 | github-token: ${{ secrets.GITHUB_TOKEN }} 19 | upload-url: ${{ github.event.release.upload_url }} 20 | -------------------------------------------------------------------------------- /.github/workflows/release_pypi.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: PyPI Release Actions 6 | 7 | on: 8 | release: 9 | types: [published] 10 | 11 | jobs: 12 | upload-release-assets: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Run PyPI Release CI workflow 16 | uses: adafruit/workflows-circuitpython-libs/release-pypi@main 17 | with: 18 | pypi-username: ${{ secrets.pypi_username }} 19 | pypi-password: ${{ secrets.pypi_password }} 20 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Kattni Rembor, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | # Do not include files and directories created by your personal work environment, such as the IDE 6 | # you use, except for those already listed here. Pull requests including changes to this file will 7 | # not be accepted. 8 | 9 | # This .gitignore file contains rules for files generated by working with CircuitPython libraries, 10 | # including building Sphinx, testing with pip, and creating a virual environment, as well as the 11 | # MacOS and IDE-specific files generated by using MacOS in general, or the PyCharm or VSCode IDEs. 12 | 13 | # If you find that there are files being generated on your machine that should not be included in 14 | # your git commit, you should create a .gitignore_global file on your computer to include the 15 | # files created by your personal setup. To do so, follow the two steps below. 16 | 17 | # First, create a file called .gitignore_global somewhere convenient for you, and add rules for 18 | # the files you want to exclude from git commits. 19 | 20 | # Second, configure Git to use the exclude file for all Git repositories by running the 21 | # following via commandline, replacing "path/to/your/" with the actual path to your newly created 22 | # .gitignore_global file: 23 | # git config --global core.excludesfile path/to/your/.gitignore_global 24 | 25 | # CircuitPython-specific files 26 | *.mpy 27 | 28 | # Python-specific files 29 | __pycache__ 30 | *.pyc 31 | 32 | # Sphinx build-specific files 33 | _build 34 | 35 | # This file results from running `pip -e install .` in a local repository 36 | *.egg-info 37 | 38 | # Virtual environment-specific files 39 | .env 40 | .venv 41 | 42 | # MacOS-specific files 43 | *.DS_Store 44 | 45 | # IDE-specific files 46 | .idea 47 | .vscode 48 | *~ 49 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | repos: 6 | - repo: https://github.com/pre-commit/pre-commit-hooks 7 | rev: v4.5.0 8 | hooks: 9 | - id: check-yaml 10 | - id: end-of-file-fixer 11 | - id: trailing-whitespace 12 | - repo: https://github.com/astral-sh/ruff-pre-commit 13 | rev: v0.3.4 14 | hooks: 15 | - id: ruff-format 16 | - id: ruff 17 | args: ["--fix"] 18 | - repo: https://github.com/fsfe/reuse-tool 19 | rev: v3.0.1 20 | hooks: 21 | - id: reuse 22 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | # Read the Docs configuration file 6 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 7 | 8 | # Required 9 | version: 2 10 | 11 | sphinx: 12 | configuration: docs/conf.py 13 | 14 | build: 15 | os: ubuntu-20.04 16 | tools: 17 | python: "3" 18 | 19 | python: 20 | install: 21 | - requirements: docs/requirements.txt 22 | - requirements: requirements.txt 23 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 6 | 7 | # Adafruit Community Code of Conduct 8 | 9 | ## Our Pledge 10 | 11 | In the interest of fostering an open and welcoming environment, we as 12 | contributors and leaders pledge to making participation in our project and 13 | our community a harassment-free experience for everyone, regardless of age, body 14 | size, disability, ethnicity, gender identity and expression, level or type of 15 | experience, education, socio-economic status, nationality, personal appearance, 16 | race, religion, or sexual identity and orientation. 17 | 18 | ## Our Standards 19 | 20 | We are committed to providing a friendly, safe and welcoming environment for 21 | all. 22 | 23 | Examples of behavior that contributes to creating a positive environment 24 | include: 25 | 26 | * Be kind and courteous to others 27 | * Using welcoming and inclusive language 28 | * Being respectful of differing viewpoints and experiences 29 | * Collaborating with other community members 30 | * Gracefully accepting constructive criticism 31 | * Focusing on what is best for the community 32 | * Showing empathy towards other community members 33 | 34 | Examples of unacceptable behavior by participants include: 35 | 36 | * The use of sexualized language or imagery and sexual attention or advances 37 | * The use of inappropriate images, including in a community member's avatar 38 | * The use of inappropriate language, including in a community member's nickname 39 | * Any spamming, flaming, baiting or other attention-stealing behavior 40 | * Excessive or unwelcome helping; answering outside the scope of the question 41 | asked 42 | * Trolling, insulting/derogatory comments, and personal or political attacks 43 | * Promoting or spreading disinformation, lies, or conspiracy theories against 44 | a person, group, organisation, project, or community 45 | * Public or private harassment 46 | * Publishing others' private information, such as a physical or electronic 47 | address, without explicit permission 48 | * Other conduct which could reasonably be considered inappropriate 49 | 50 | The goal of the standards and moderation guidelines outlined here is to build 51 | and maintain a respectful community. We ask that you don’t just aim to be 52 | "technically unimpeachable", but rather try to be your best self. 53 | 54 | We value many things beyond technical expertise, including collaboration and 55 | supporting others within our community. Providing a positive experience for 56 | other community members can have a much more significant impact than simply 57 | providing the correct answer. 58 | 59 | ## Our Responsibilities 60 | 61 | Project leaders are responsible for clarifying the standards of acceptable 62 | behavior and are expected to take appropriate and fair corrective action in 63 | response to any instances of unacceptable behavior. 64 | 65 | Project leaders have the right and responsibility to remove, edit, or 66 | reject messages, comments, commits, code, issues, and other contributions 67 | that are not aligned to this Code of Conduct, or to ban temporarily or 68 | permanently any community member for other behaviors that they deem 69 | inappropriate, threatening, offensive, or harmful. 70 | 71 | ## Moderation 72 | 73 | Instances of behaviors that violate the Adafruit Community Code of Conduct 74 | may be reported by any member of the community. Community members are 75 | encouraged to report these situations, including situations they witness 76 | involving other community members. 77 | 78 | You may report in the following ways: 79 | 80 | In any situation, you may send an email to . 81 | 82 | On the Adafruit Discord, you may send an open message from any channel 83 | to all Community Moderators by tagging @community moderators. You may 84 | also send an open message from any channel, or a direct message to 85 | @kattni#1507, @tannewt#4653, @Dan Halbert#1614, @cater#2442, 86 | @sommersoft#0222, @Mr. Certainly#0472 or @Andon#8175. 87 | 88 | Email and direct message reports will be kept confidential. 89 | 90 | In situations on Discord where the issue is particularly egregious, possibly 91 | illegal, requires immediate action, or violates the Discord terms of service, 92 | you should also report the message directly to Discord. 93 | 94 | These are the steps for upholding our community’s standards of conduct. 95 | 96 | 1. Any member of the community may report any situation that violates the 97 | Adafruit Community Code of Conduct. All reports will be reviewed and 98 | investigated. 99 | 2. If the behavior is an egregious violation, the community member who 100 | committed the violation may be banned immediately, without warning. 101 | 3. Otherwise, moderators will first respond to such behavior with a warning. 102 | 4. Moderators follow a soft "three strikes" policy - the community member may 103 | be given another chance, if they are receptive to the warning and change their 104 | behavior. 105 | 5. If the community member is unreceptive or unreasonable when warned by a 106 | moderator, or the warning goes unheeded, they may be banned for a first or 107 | second offense. Repeated offenses will result in the community member being 108 | banned. 109 | 110 | ## Scope 111 | 112 | This Code of Conduct and the enforcement policies listed above apply to all 113 | Adafruit Community venues. This includes but is not limited to any community 114 | spaces (both public and private), the entire Adafruit Discord server, and 115 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 116 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 117 | interaction at a conference. 118 | 119 | This Code of Conduct applies both within project spaces and in public spaces 120 | when an individual is representing the project or its community. As a community 121 | member, you are representing our community, and are expected to behave 122 | accordingly. 123 | 124 | ## Attribution 125 | 126 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 127 | version 1.4, available at 128 | , 129 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 130 | 131 | For other projects adopting the Adafruit Community Code of 132 | Conduct, please contact the maintainers of those projects for enforcement. 133 | If you wish to use this code of conduct for your own project, consider 134 | explicitly mentioning your moderation policy or making a copy with your 135 | own moderation policy so as to avoid confusion. 136 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Dean Miller for Adafruit Industries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSES/CC-BY-4.0.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Attribution 4.0 International Creative Commons Corporation 2 | ("Creative Commons") is not a law firm and does not provide legal services 3 | or legal advice. Distribution of Creative Commons public licenses does not 4 | create a lawyer-client or other relationship. Creative Commons makes its licenses 5 | and related information available on an "as-is" basis. Creative Commons gives 6 | no warranties regarding its licenses, any material licensed under their terms 7 | and conditions, or any related information. Creative Commons disclaims all 8 | liability for damages resulting from their use to the fullest extent possible. 9 | 10 | Using Creative Commons Public Licenses 11 | 12 | Creative Commons public licenses provide a standard set of terms and conditions 13 | that creators and other rights holders may use to share original works of 14 | authorship and other material subject to copyright and certain other rights 15 | specified in the public license below. The following considerations are for 16 | informational purposes only, are not exhaustive, and do not form part of our 17 | licenses. 18 | 19 | Considerations for licensors: Our public licenses are intended for use by 20 | those authorized to give the public permission to use material in ways otherwise 21 | restricted by copyright and certain other rights. Our licenses are irrevocable. 22 | Licensors should read and understand the terms and conditions of the license 23 | they choose before applying it. Licensors should also secure all rights necessary 24 | before applying our licenses so that the public can reuse the material as 25 | expected. Licensors should clearly mark any material not subject to the license. 26 | This includes other CC-licensed material, or material used under an exception 27 | or limitation to copyright. More considerations for licensors : wiki.creativecommons.org/Considerations_for_licensors 28 | 29 | Considerations for the public: By using one of our public licenses, a licensor 30 | grants the public permission to use the licensed material under specified 31 | terms and conditions. If the licensor's permission is not necessary for any 32 | reason–for example, because of any applicable exception or limitation to copyright–then 33 | that use is not regulated by the license. Our licenses grant only permissions 34 | under copyright and certain other rights that a licensor has authority to 35 | grant. Use of the licensed material may still be restricted for other reasons, 36 | including because others have copyright or other rights in the material. A 37 | licensor may make special requests, such as asking that all changes be marked 38 | or described. Although not required by our licenses, you are encouraged to 39 | respect those requests where reasonable. More considerations for the public 40 | : wiki.creativecommons.org/Considerations_for_licensees Creative Commons Attribution 41 | 4.0 International Public License 42 | 43 | By exercising the Licensed Rights (defined below), You accept and agree to 44 | be bound by the terms and conditions of this Creative Commons Attribution 45 | 4.0 International Public License ("Public License"). To the extent this Public 46 | License may be interpreted as a contract, You are granted the Licensed Rights 47 | in consideration of Your acceptance of these terms and conditions, and the 48 | Licensor grants You such rights in consideration of benefits the Licensor 49 | receives from making the Licensed Material available under these terms and 50 | conditions. 51 | 52 | Section 1 – Definitions. 53 | 54 | a. Adapted Material means material subject to Copyright and Similar Rights 55 | that is derived from or based upon the Licensed Material and in which the 56 | Licensed Material is translated, altered, arranged, transformed, or otherwise 57 | modified in a manner requiring permission under the Copyright and Similar 58 | Rights held by the Licensor. For purposes of this Public License, where the 59 | Licensed Material is a musical work, performance, or sound recording, Adapted 60 | Material is always produced where the Licensed Material is synched in timed 61 | relation with a moving image. 62 | 63 | b. Adapter's License means the license You apply to Your Copyright and Similar 64 | Rights in Your contributions to Adapted Material in accordance with the terms 65 | and conditions of this Public License. 66 | 67 | c. Copyright and Similar Rights means copyright and/or similar rights closely 68 | related to copyright including, without limitation, performance, broadcast, 69 | sound recording, and Sui Generis Database Rights, without regard to how the 70 | rights are labeled or categorized. For purposes of this Public License, the 71 | rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. 72 | 73 | d. Effective Technological Measures means those measures that, in the absence 74 | of proper authority, may not be circumvented under laws fulfilling obligations 75 | under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, 76 | and/or similar international agreements. 77 | 78 | e. Exceptions and Limitations means fair use, fair dealing, and/or any other 79 | exception or limitation to Copyright and Similar Rights that applies to Your 80 | use of the Licensed Material. 81 | 82 | f. Licensed Material means the artistic or literary work, database, or other 83 | material to which the Licensor applied this Public License. 84 | 85 | g. Licensed Rights means the rights granted to You subject to the terms and 86 | conditions of this Public License, which are limited to all Copyright and 87 | Similar Rights that apply to Your use of the Licensed Material and that the 88 | Licensor has authority to license. 89 | 90 | h. Licensor means the individual(s) or entity(ies) granting rights under this 91 | Public License. 92 | 93 | i. Share means to provide material to the public by any means or process that 94 | requires permission under the Licensed Rights, such as reproduction, public 95 | display, public performance, distribution, dissemination, communication, or 96 | importation, and to make material available to the public including in ways 97 | that members of the public may access the material from a place and at a time 98 | individually chosen by them. 99 | 100 | j. Sui Generis Database Rights means rights other than copyright resulting 101 | from Directive 96/9/EC of the European Parliament and of the Council of 11 102 | March 1996 on the legal protection of databases, as amended and/or succeeded, 103 | as well as other essentially equivalent rights anywhere in the world. 104 | 105 | k. You means the individual or entity exercising the Licensed Rights under 106 | this Public License. Your has a corresponding meaning. 107 | 108 | Section 2 – Scope. 109 | 110 | a. License grant. 111 | 112 | 1. Subject to the terms and conditions of this Public License, the Licensor 113 | hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, 114 | irrevocable license to exercise the Licensed Rights in the Licensed Material 115 | to: 116 | 117 | A. reproduce and Share the Licensed Material, in whole or in part; and 118 | 119 | B. produce, reproduce, and Share Adapted Material. 120 | 121 | 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions 122 | and Limitations apply to Your use, this Public License does not apply, and 123 | You do not need to comply with its terms and conditions. 124 | 125 | 3. Term. The term of this Public License is specified in Section 6(a). 126 | 127 | 4. Media and formats; technical modifications allowed. The Licensor authorizes 128 | You to exercise the Licensed Rights in all media and formats whether now known 129 | or hereafter created, and to make technical modifications necessary to do 130 | so. The Licensor waives and/or agrees not to assert any right or authority 131 | to forbid You from making technical modifications necessary to exercise the 132 | Licensed Rights, including technical modifications necessary to circumvent 133 | Effective Technological Measures. For purposes of this Public License, simply 134 | making modifications authorized by this Section 2(a)(4) never produces Adapted 135 | Material. 136 | 137 | 5. Downstream recipients. 138 | 139 | A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed 140 | Material automatically receives an offer from the Licensor to exercise the 141 | Licensed Rights under the terms and conditions of this Public License. 142 | 143 | B. No downstream restrictions. You may not offer or impose any additional 144 | or different terms or conditions on, or apply any Effective Technological 145 | Measures to, the Licensed Material if doing so restricts exercise of the Licensed 146 | Rights by any recipient of the Licensed Material. 147 | 148 | 6. No endorsement. Nothing in this Public License constitutes or may be construed 149 | as permission to assert or imply that You are, or that Your use of the Licensed 150 | Material is, connected with, or sponsored, endorsed, or granted official status 151 | by, the Licensor or others designated to receive attribution as provided in 152 | Section 3(a)(1)(A)(i). 153 | 154 | b. Other rights. 155 | 156 | 1. Moral rights, such as the right of integrity, are not licensed under this 157 | Public License, nor are publicity, privacy, and/or other similar personality 158 | rights; however, to the extent possible, the Licensor waives and/or agrees 159 | not to assert any such rights held by the Licensor to the limited extent necessary 160 | to allow You to exercise the Licensed Rights, but not otherwise. 161 | 162 | 2. Patent and trademark rights are not licensed under this Public License. 163 | 164 | 3. To the extent possible, the Licensor waives any right to collect royalties 165 | from You for the exercise of the Licensed Rights, whether directly or through 166 | a collecting society under any voluntary or waivable statutory or compulsory 167 | licensing scheme. In all other cases the Licensor expressly reserves any right 168 | to collect such royalties. 169 | 170 | Section 3 – License Conditions. 171 | 172 | Your exercise of the Licensed Rights is expressly made subject to the following 173 | conditions. 174 | 175 | a. Attribution. 176 | 177 | 1. If You Share the Licensed Material (including in modified form), You must: 178 | 179 | A. retain the following if it is supplied by the Licensor with the Licensed 180 | Material: 181 | 182 | i. identification of the creator(s) of the Licensed Material and any others 183 | designated to receive attribution, in any reasonable manner requested by the 184 | Licensor (including by pseudonym if designated); 185 | 186 | ii. a copyright notice; 187 | 188 | iii. a notice that refers to this Public License; 189 | 190 | iv. a notice that refers to the disclaimer of warranties; 191 | 192 | v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; 193 | 194 | B. indicate if You modified the Licensed Material and retain an indication 195 | of any previous modifications; and 196 | 197 | C. indicate the Licensed Material is licensed under this Public License, and 198 | include the text of, or the URI or hyperlink to, this Public License. 199 | 200 | 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner 201 | based on the medium, means, and context in which You Share the Licensed Material. 202 | For example, it may be reasonable to satisfy the conditions by providing a 203 | URI or hyperlink to a resource that includes the required information. 204 | 205 | 3. If requested by the Licensor, You must remove any of the information required 206 | by Section 3(a)(1)(A) to the extent reasonably practicable. 207 | 208 | 4. If You Share Adapted Material You produce, the Adapter's License You apply 209 | must not prevent recipients of the Adapted Material from complying with this 210 | Public License. 211 | 212 | Section 4 – Sui Generis Database Rights. 213 | 214 | Where the Licensed Rights include Sui Generis Database Rights that apply to 215 | Your use of the Licensed Material: 216 | 217 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, 218 | reuse, reproduce, and Share all or a substantial portion of the contents of 219 | the database; 220 | 221 | b. if You include all or a substantial portion of the database contents in 222 | a database in which You have Sui Generis Database Rights, then the database 223 | in which You have Sui Generis Database Rights (but not its individual contents) 224 | is Adapted Material; and 225 | 226 | c. You must comply with the conditions in Section 3(a) if You Share all or 227 | a substantial portion of the contents of the database. 228 | 229 | For the avoidance of doubt, this Section 4 supplements and does not replace 230 | Your obligations under this Public License where the Licensed Rights include 231 | other Copyright and Similar Rights. 232 | 233 | Section 5 – Disclaimer of Warranties and Limitation of Liability. 234 | 235 | a. Unless otherwise separately undertaken by the Licensor, to the extent possible, 236 | the Licensor offers the Licensed Material as-is and as-available, and makes 237 | no representations or warranties of any kind concerning the Licensed Material, 238 | whether express, implied, statutory, or other. This includes, without limitation, 239 | warranties of title, merchantability, fitness for a particular purpose, non-infringement, 240 | absence of latent or other defects, accuracy, or the presence or absence of 241 | errors, whether or not known or discoverable. Where disclaimers of warranties 242 | are not allowed in full or in part, this disclaimer may not apply to You. 243 | 244 | b. To the extent possible, in no event will the Licensor be liable to You 245 | on any legal theory (including, without limitation, negligence) or otherwise 246 | for any direct, special, indirect, incidental, consequential, punitive, exemplary, 247 | or other losses, costs, expenses, or damages arising out of this Public License 248 | or use of the Licensed Material, even if the Licensor has been advised of 249 | the possibility of such losses, costs, expenses, or damages. Where a limitation 250 | of liability is not allowed in full or in part, this limitation may not apply 251 | to You. 252 | 253 | c. The disclaimer of warranties and limitation of liability provided above 254 | shall be interpreted in a manner that, to the extent possible, most closely 255 | approximates an absolute disclaimer and waiver of all liability. 256 | 257 | Section 6 – Term and Termination. 258 | 259 | a. This Public License applies for the term of the Copyright and Similar Rights 260 | licensed here. However, if You fail to comply with this Public License, then 261 | Your rights under this Public License terminate automatically. 262 | 263 | b. Where Your right to use the Licensed Material has terminated under Section 264 | 6(a), it reinstates: 265 | 266 | 1. automatically as of the date the violation is cured, provided it is cured 267 | within 30 days of Your discovery of the violation; or 268 | 269 | 2. upon express reinstatement by the Licensor. 270 | 271 | c. For the avoidance of doubt, this Section 6(b) does not affect any right 272 | the Licensor may have to seek remedies for Your violations of this Public 273 | License. 274 | 275 | d. For the avoidance of doubt, the Licensor may also offer the Licensed Material 276 | under separate terms or conditions or stop distributing the Licensed Material 277 | at any time; however, doing so will not terminate this Public License. 278 | 279 | e. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. 280 | 281 | Section 7 – Other Terms and Conditions. 282 | 283 | a. The Licensor shall not be bound by any additional or different terms or 284 | conditions communicated by You unless expressly agreed. 285 | 286 | b. Any arrangements, understandings, or agreements regarding the Licensed 287 | Material not stated herein are separate from and independent of the terms 288 | and conditions of this Public License. 289 | 290 | Section 8 – Interpretation. 291 | 292 | a. For the avoidance of doubt, this Public License does not, and shall not 293 | be interpreted to, reduce, limit, restrict, or impose conditions on any use 294 | of the Licensed Material that could lawfully be made without permission under 295 | this Public License. 296 | 297 | b. To the extent possible, if any provision of this Public License is deemed 298 | unenforceable, it shall be automatically reformed to the minimum extent necessary 299 | to make it enforceable. If the provision cannot be reformed, it shall be severed 300 | from this Public License without affecting the enforceability of the remaining 301 | terms and conditions. 302 | 303 | c. No term or condition of this Public License will be waived and no failure 304 | to comply consented to unless expressly agreed to by the Licensor. 305 | 306 | d. Nothing in this Public License constitutes or may be interpreted as a limitation 307 | upon, or waiver of, any privileges and immunities that apply to the Licensor 308 | or You, including from the legal processes of any jurisdiction or authority. 309 | 310 | Creative Commons is not a party to its public licenses. Notwithstanding, Creative 311 | Commons may elect to apply one of its public licenses to material it publishes 312 | and in those instances will be considered the "Licensor." The text of the 313 | Creative Commons public licenses is dedicated to the public domain under the 314 | CC0 Public Domain Dedication. Except for the limited purpose of indicating 315 | that material is shared under a Creative Commons public license or as otherwise 316 | permitted by the Creative Commons policies published at creativecommons.org/policies, 317 | Creative Commons does not authorize the use of the trademark "Creative Commons" 318 | or any other trademark or logo of Creative Commons without its prior written 319 | consent including, without limitation, in connection with any unauthorized 320 | modifications to any of its public licenses or any other arrangements, understandings, 321 | or agreements concerning use of licensed material. For the avoidance of doubt, 322 | this paragraph does not form part of the public licenses. 323 | 324 | Creative Commons may be contacted at creativecommons.org. 325 | -------------------------------------------------------------------------------- /LICENSES/MIT.txt: -------------------------------------------------------------------------------- 1 | MIT License Copyright (c) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice (including the next 11 | paragraph) shall be included in all copies or substantial portions of the 12 | Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 17 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 | OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /LICENSES/Unlicense.txt: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute 4 | this software, either in source code form or as a compiled binary, for any 5 | purpose, commercial or non-commercial, and by any means. 6 | 7 | In jurisdictions that recognize copyright laws, the author or authors of this 8 | software dedicate any and all copyright interest in the software to the public 9 | domain. We make this dedication for the benefit of the public at large and 10 | to the detriment of our heirs and successors. We intend this dedication to 11 | be an overt act of relinquishment in perpetuity of all present and future 12 | rights to this software under copyright law. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 17 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 19 | THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, 20 | please refer to 21 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | 2 | Introduction 3 | ============ 4 | 5 | .. image:: https://readthedocs.org/projects/adafruit-circuitpython-ina219/badge/?version=latest 6 | :target: https://docs.circuitpython.org/projects/ina219/en/latest/ 7 | :alt: Documentation Status 8 | 9 | .. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg 10 | :target: https://adafru.it/discord 11 | :alt: Discord 12 | 13 | .. image:: https://github.com/adafruit/Adafruit_CircuitPython_INA219/workflows/Build%20CI/badge.svg 14 | :target: https://github.com/adafruit/Adafruit_CircuitPython_INA219/actions/ 15 | :alt: Build Status 16 | 17 | .. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json 18 | :target: https://github.com/astral-sh/ruff 19 | :alt: Code Style: Ruff 20 | 21 | CircuitPython driver for the `INA219 current sensor `_. 22 | 23 | Dependencies 24 | ============= 25 | This driver depends on: 26 | 27 | * `Adafruit CircuitPython `_ 28 | * `Bus Device `_ 29 | * `Register `_ 30 | 31 | Please ensure all dependencies are available on the CircuitPython filesystem. 32 | This is easily achieved by downloading 33 | `the Adafruit library and driver bundle `_. 34 | 35 | Installing from PyPI 36 | ==================== 37 | 38 | On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from 39 | PyPI `_. To install for current user: 40 | 41 | .. code-block:: shell 42 | 43 | pip3 install adafruit-circuitpython-ina219 44 | 45 | To install system-wide (this may be required in some cases): 46 | 47 | .. code-block:: shell 48 | 49 | sudo pip3 install adafruit-circuitpython-ina219 50 | 51 | To install in a virtual environment in your current project: 52 | 53 | .. code-block:: shell 54 | 55 | mkdir project-name && cd project-name 56 | python3 -m venv .venv 57 | source .venv/bin/activate 58 | pip3 install adafruit-circuitpython-ina219 59 | 60 | Usage Example 61 | ============= 62 | 63 | see `example `_ 64 | 65 | Documentation 66 | ============= 67 | 68 | API documentation for this library can be found on `Read the Docs `_. 69 | 70 | For information on building library documentation, please check out `this guide `_. 71 | 72 | Contributing 73 | ============ 74 | 75 | Contributions are welcome! Please read our `Code of Conduct 76 | `_ 77 | before contributing to help this project stay welcoming. 78 | -------------------------------------------------------------------------------- /README.rst.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | 3 | SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /adafruit_ina219.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Dean Miller for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | """ 6 | `adafruit_ina219` 7 | ==================================================== 8 | 9 | CircuitPython driver for the INA219 current sensor. 10 | 11 | * Author(s): Dean Miller 12 | 13 | Implementation Notes 14 | -------------------- 15 | 16 | **Hardware:** 17 | 18 | * `Adafruit INA219 High Side DC Current Sensor Breakout `_ 19 | 20 | * `Adafruit INA219 FeatherWing `_ 21 | 22 | **Software and Dependencies:** 23 | 24 | * Adafruit CircuitPython firmware (2.2.0+) for the ESP8622 and M0-based boards: 25 | https://github.com/adafruit/circuitpython/releases 26 | * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice 27 | """ 28 | 29 | from adafruit_bus_device.i2c_device import I2CDevice 30 | from adafruit_register.i2c_bit import ROBit 31 | from adafruit_register.i2c_bits import ROBits, RWBits 32 | from adafruit_register.i2c_struct import ROUnaryStruct, UnaryStruct 33 | from micropython import const 34 | 35 | try: 36 | import typing 37 | 38 | from busio import I2C 39 | except ImportError: 40 | # define I2C to avoid the error: 41 | # def __init__(self, i2c_bus: I2C, addr: int = 0x40) -> None: 42 | # NameError: name 'I2C' is not defined 43 | I2C = None 44 | 45 | __version__ = "0.0.0+auto.0" 46 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_INA219.git" 47 | 48 | # Bits 49 | 50 | # Config Register (R/W) 51 | _REG_CONFIG = const(0x00) 52 | 53 | 54 | class BusVoltageRange: 55 | """Constants for ``bus_voltage_range``""" 56 | 57 | RANGE_16V = 0x00 # set bus voltage range to 16V 58 | RANGE_32V = 0x01 # set bus voltage range to 32V (default) 59 | 60 | 61 | class Gain: 62 | """Constants for ``gain``""" 63 | 64 | DIV_1_40MV = 0x00 # shunt prog. gain set to 1, 40 mV range 65 | DIV_2_80MV = 0x01 # shunt prog. gain set to /2, 80 mV range 66 | DIV_4_160MV = 0x02 # shunt prog. gain set to /4, 160 mV range 67 | DIV_8_320MV = 0x03 # shunt prog. gain set to /8, 320 mV range 68 | 69 | 70 | class ADCResolution: 71 | """Constants for ``bus_adc_resolution`` or ``shunt_adc_resolution``""" 72 | 73 | ADCRES_9BIT_1S = 0x00 # 9bit, 1 sample, 84us 74 | ADCRES_10BIT_1S = 0x01 # 10bit, 1 sample, 148us 75 | ADCRES_11BIT_1S = 0x02 # 11 bit, 1 sample, 276us 76 | ADCRES_12BIT_1S = 0x03 # 12 bit, 1 sample, 532us 77 | ADCRES_12BIT_2S = 0x09 # 12 bit, 2 samples, 1.06ms 78 | ADCRES_12BIT_4S = 0x0A # 12 bit, 4 samples, 2.13ms 79 | ADCRES_12BIT_8S = 0x0B # 12bit, 8 samples, 4.26ms 80 | ADCRES_12BIT_16S = 0x0C # 12bit, 16 samples, 8.51ms 81 | ADCRES_12BIT_32S = 0x0D # 12bit, 32 samples, 17.02ms 82 | ADCRES_12BIT_64S = 0x0E # 12bit, 64 samples, 34.05ms 83 | ADCRES_12BIT_128S = 0x0F # 12bit, 128 samples, 68.10ms 84 | 85 | 86 | class Mode: 87 | """Constants for ``mode``""" 88 | 89 | POWERDOWN = 0x00 # power down 90 | SVOLT_TRIGGERED = 0x01 # shunt voltage triggered 91 | BVOLT_TRIGGERED = 0x02 # bus voltage triggered 92 | SANDBVOLT_TRIGGERED = 0x03 # shunt and bus voltage triggered 93 | ADCOFF = 0x04 # ADC off 94 | SVOLT_CONTINUOUS = 0x05 # shunt voltage continuous 95 | BVOLT_CONTINUOUS = 0x06 # bus voltage continuous 96 | SANDBVOLT_CONTINUOUS = 0x07 # shunt and bus voltage continuous 97 | 98 | 99 | # SHUNT VOLTAGE REGISTER (R) 100 | _REG_SHUNTVOLTAGE = const(0x01) 101 | 102 | # BUS VOLTAGE REGISTER (R) 103 | _REG_BUSVOLTAGE = const(0x02) 104 | 105 | # POWER REGISTER (R) 106 | _REG_POWER = const(0x03) 107 | 108 | # CURRENT REGISTER (R) 109 | _REG_CURRENT = const(0x04) 110 | 111 | # CALIBRATION REGISTER (R/W) 112 | _REG_CALIBRATION = const(0x05) 113 | # pylint: enable=too-few-public-methods 114 | 115 | 116 | def _to_signed(num: int) -> int: 117 | if num > 0x7FFF: 118 | num -= 0x10000 119 | return num 120 | 121 | 122 | class INA219: 123 | """Driver for the INA219 current sensor""" 124 | 125 | # Basic API: 126 | 127 | # INA219( i2c_bus, addr) Create instance of INA219 sensor 128 | # :param i2c_bus The I2C bus the INA219is connected to 129 | # :param addr (0x40) Address of the INA219 on the bus (default 0x40) 130 | 131 | # shunt_voltage RO : shunt voltage scaled to Volts 132 | # bus_voltage RO : bus voltage (V- to GND) scaled to volts (==load voltage) 133 | # current RO : current through shunt, scaled to mA 134 | # power RO : power consumption of the load, scaled to Watt 135 | # set_calibration_32V_2A() Initialize chip for 32V max and up to 2A (default) 136 | # set_calibration_32V_1A() Initialize chip for 32V max and up to 1A 137 | # set_calibration_16V_400mA() Initialize chip for 16V max and up to 400mA 138 | 139 | # Advanced API: 140 | # config register break-up 141 | # reset WO : Write Reset.RESET to reset the chip (must recalibrate) 142 | # bus_voltage_range RW : Bus Voltage Range field (use BusVoltageRange.XXX constants) 143 | # gain RW : Programmable Gain field (use Gain.XXX constants) 144 | # bus_adc_resolution RW : Bus ADC resolution and averaging modes (ADCResolution.XXX) 145 | # shunt_adc_resolution RW : Shunt ADC resolution and averaging modes (ADCResolution.XXX) 146 | # mode RW : operating modes in config register (use Mode.XXX constants) 147 | 148 | # raw_shunt_voltage RO : Shunt Voltage register (not scaled) 149 | # raw_bus_voltage RO : Bus Voltage field in Bus Voltage register (not scaled) 150 | # conversion_ready RO : Conversion Ready bit in Bus Voltage register 151 | # overflow RO : Math Overflow bit in Bus Voltage register 152 | # raw_power RO : Power register (not scaled) 153 | # raw_current RO : Current register (not scaled) 154 | # calibration RW : calibration register (note: value is cached) 155 | 156 | def __init__(self, i2c_bus: I2C, addr: int = 0x40) -> None: 157 | self.i2c_device = I2CDevice(i2c_bus, addr) 158 | self.i2c_addr = addr 159 | 160 | # Set chip to known config values to start 161 | self._cal_value = 0 162 | self._current_lsb = 0 163 | self._power_lsb = 0 164 | self.set_calibration_32V_2A() 165 | 166 | # config register break-up 167 | reset = RWBits(1, _REG_CONFIG, 15, 2, False) 168 | bus_voltage_range = RWBits(1, _REG_CONFIG, 13, 2, False) 169 | gain = RWBits(2, _REG_CONFIG, 11, 2, False) 170 | bus_adc_resolution = RWBits(4, _REG_CONFIG, 7, 2, False) 171 | shunt_adc_resolution = RWBits(4, _REG_CONFIG, 3, 2, False) 172 | mode = RWBits(3, _REG_CONFIG, 0, 2, False) 173 | 174 | # shunt voltage register 175 | raw_shunt_voltage = ROUnaryStruct(_REG_SHUNTVOLTAGE, ">h") 176 | 177 | # bus voltage register 178 | raw_bus_voltage = ROBits(13, _REG_BUSVOLTAGE, 3, 2, False) 179 | conversion_ready = ROBit(_REG_BUSVOLTAGE, 1, 2, False) 180 | overflow = ROBit(_REG_BUSVOLTAGE, 0, 2, False) 181 | 182 | # power and current registers 183 | raw_power = ROUnaryStruct(_REG_POWER, ">H") 184 | raw_current = ROUnaryStruct(_REG_CURRENT, ">h") 185 | 186 | # calibration register 187 | _raw_calibration = UnaryStruct(_REG_CALIBRATION, ">H") 188 | 189 | @property 190 | def calibration(self) -> int: 191 | """Calibration register (cached value)""" 192 | return self._cal_value # return cached value 193 | 194 | @calibration.setter 195 | def calibration(self, cal_value: int) -> None: 196 | self._cal_value = cal_value # value is cached for ``current`` and ``power`` properties 197 | self._raw_calibration = self._cal_value 198 | 199 | @property 200 | def shunt_voltage(self) -> float: 201 | """The shunt voltage (between V+ and V-) in Volts (so +-.327V)""" 202 | # The least signficant bit is 10uV which is 0.00001 volts 203 | return self.raw_shunt_voltage * 0.00001 204 | 205 | @property 206 | def bus_voltage(self) -> float: 207 | """The bus voltage (between V- and GND) in Volts""" 208 | # Shift to the right 3 to drop CNVR and OVF and multiply by LSB 209 | # Each least signficant bit is 4mV 210 | return self.raw_bus_voltage * 0.004 211 | 212 | @property 213 | def current(self) -> float: 214 | """The current through the shunt resistor in milliamps.""" 215 | # Sometimes a sharp load will reset the INA219, which will 216 | # reset the cal register, meaning CURRENT and POWER will 217 | # not be available ... always setting a cal 218 | # value even if it's an unfortunate extra step 219 | self._raw_calibration = self._cal_value 220 | # Now we can safely read the CURRENT register! 221 | return self.raw_current * self._current_lsb 222 | 223 | @property 224 | def power(self) -> float: 225 | """The power through the load in Watt.""" 226 | # Sometimes a sharp load will reset the INA219, which will 227 | # reset the cal register, meaning CURRENT and POWER will 228 | # not be available ... always setting a cal 229 | # value even if it's an unfortunate extra step 230 | self._raw_calibration = self._cal_value 231 | # Now we can safely read the CURRENT register! 232 | return self.raw_power * self._power_lsb 233 | 234 | def set_calibration_32V_2A(self) -> None: 235 | """Configures to INA219 to be able to measure up to 32V and 2A of current. Counter 236 | overflow occurs at 3.2A. 237 | 238 | .. note:: These calculations assume a 0.1 shunt ohm resistor is present 239 | """ 240 | # By default we use a pretty huge range for the input voltage, 241 | # which probably isn't the most appropriate choice for system 242 | # that don't use a lot of power. But all of the calculations 243 | # are shown below if you want to change the settings. You will 244 | # also need to change any relevant register settings, such as 245 | # setting the VBUS_MAX to 16V instead of 32V, etc. 246 | 247 | # VBUS_MAX = 32V (Assumes 32V, can also be set to 16V) 248 | # VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04) 249 | # RSHUNT = 0.1 (Resistor value in ohms) 250 | 251 | # 1. Determine max possible current 252 | # MaxPossible_I = VSHUNT_MAX / RSHUNT 253 | # MaxPossible_I = 3.2A 254 | 255 | # 2. Determine max expected current 256 | # MaxExpected_I = 2.0A 257 | 258 | # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 259 | # MinimumLSB = MaxExpected_I/32767 260 | # MinimumLSB = 0.000061 (61uA per bit) 261 | # MaximumLSB = MaxExpected_I/4096 262 | # MaximumLSB = 0,000488 (488uA per bit) 263 | 264 | # 4. Choose an LSB between the min and max values 265 | # (Preferrably a roundish number close to MinLSB) 266 | # CurrentLSB = 0.0001 (100uA per bit) 267 | self._current_lsb = 0.1 # Current LSB = 100uA per bit 268 | 269 | # 5. Compute the calibration register 270 | # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 271 | # Cal = 4096 (0x1000) 272 | 273 | self._cal_value = 4096 274 | 275 | # 6. Calculate the power LSB 276 | # PowerLSB = 20 * CurrentLSB 277 | # PowerLSB = 0.002 (2mW per bit) 278 | self._power_lsb = 0.002 # Power LSB = 2mW per bit 279 | 280 | # 7. Compute the maximum current and shunt voltage values before overflow 281 | # 282 | # Max_Current = Current_LSB * 32767 283 | # Max_Current = 3.2767A before overflow 284 | # 285 | # If Max_Current > Max_Possible_I then 286 | # Max_Current_Before_Overflow = MaxPossible_I 287 | # Else 288 | # Max_Current_Before_Overflow = Max_Current 289 | # End If 290 | # 291 | # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 292 | # Max_ShuntVoltage = 0.32V 293 | # 294 | # If Max_ShuntVoltage >= VSHUNT_MAX 295 | # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 296 | # Else 297 | # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 298 | # End If 299 | 300 | # 8. Compute the Maximum Power 301 | # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 302 | # MaximumPower = 3.2 * 32V 303 | # MaximumPower = 102.4W 304 | 305 | # Set Calibration register to 'Cal' calculated above 306 | self._raw_calibration = self._cal_value 307 | 308 | # Set Config register to take into account the settings above 309 | self.bus_voltage_range = BusVoltageRange.RANGE_32V 310 | self.gain = Gain.DIV_8_320MV 311 | self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S 312 | self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S 313 | self.mode = Mode.SANDBVOLT_CONTINUOUS 314 | 315 | def set_calibration_32V_1A(self) -> None: 316 | """Configures to INA219 to be able to measure up to 32V and 1A of current. Counter overflow 317 | occurs at 1.3A. 318 | 319 | .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" 320 | # By default we use a pretty huge range for the input voltage, 321 | # which probably isn't the most appropriate choice for system 322 | # that don't use a lot of power. But all of the calculations 323 | # are shown below if you want to change the settings. You will 324 | # also need to change any relevant register settings, such as 325 | # setting the VBUS_MAX to 16V instead of 32V, etc. 326 | 327 | # VBUS_MAX = 32V (Assumes 32V, can also be set to 16V) 328 | # VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04) 329 | # RSHUNT = 0.1 (Resistor value in ohms) 330 | 331 | # 1. Determine max possible current 332 | # MaxPossible_I = VSHUNT_MAX / RSHUNT 333 | # MaxPossible_I = 3.2A 334 | 335 | # 2. Determine max expected current 336 | # MaxExpected_I = 1.0A 337 | 338 | # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 339 | # MinimumLSB = MaxExpected_I/32767 340 | # MinimumLSB = 0.0000305 (30.5uA per bit) 341 | # MaximumLSB = MaxExpected_I/4096 342 | # MaximumLSB = 0.000244 (244uA per bit) 343 | 344 | # 4. Choose an LSB between the min and max values 345 | # (Preferrably a roundish number close to MinLSB) 346 | # CurrentLSB = 0.0000400 (40uA per bit) 347 | self._current_lsb = 0.04 # In milliamps 348 | 349 | # 5. Compute the calibration register 350 | # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 351 | # Cal = 10240 (0x2800) 352 | 353 | self._cal_value = 10240 354 | 355 | # 6. Calculate the power LSB 356 | # PowerLSB = 20 * CurrentLSB 357 | # PowerLSB = 0.0008 (800uW per bit) 358 | self._power_lsb = 0.0008 359 | 360 | # 7. Compute the maximum current and shunt voltage values before overflow 361 | # 362 | # Max_Current = Current_LSB * 32767 363 | # Max_Current = 1.31068A before overflow 364 | # 365 | # If Max_Current > Max_Possible_I then 366 | # Max_Current_Before_Overflow = MaxPossible_I 367 | # Else 368 | # Max_Current_Before_Overflow = Max_Current 369 | # End If 370 | # 371 | # ... In this case, we're good though since Max_Current is less than MaxPossible_I 372 | # 373 | # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 374 | # Max_ShuntVoltage = 0.131068V 375 | # 376 | # If Max_ShuntVoltage >= VSHUNT_MAX 377 | # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 378 | # Else 379 | # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 380 | # End If 381 | 382 | # 8. Compute the Maximum Power 383 | # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 384 | # MaximumPower = 1.31068 * 32V 385 | # MaximumPower = 41.94176W 386 | 387 | # Set Calibration register to 'Cal' calculated above 388 | self._raw_calibration = self._cal_value 389 | 390 | # Set Config register to take into account the settings above 391 | self.bus_voltage_range = BusVoltageRange.RANGE_32V 392 | self.gain = Gain.DIV_8_320MV 393 | self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S 394 | self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S 395 | self.mode = Mode.SANDBVOLT_CONTINUOUS 396 | 397 | def set_calibration_16V_400mA(self) -> None: 398 | """Configures to INA219 to be able to measure up to 16V and 400mA of current. Counter 399 | overflow occurs at 1.6A. 400 | 401 | .. note:: These calculations assume a 0.1 ohm shunt resistor is present""" 402 | # Calibration which uses the highest precision for 403 | # current measurement (0.1mA), at the expense of 404 | # only supporting 16V at 400mA max. 405 | 406 | # VBUS_MAX = 16V 407 | # VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV) 408 | # RSHUNT = 0.1 (Resistor value in ohms) 409 | 410 | # 1. Determine max possible current 411 | # MaxPossible_I = VSHUNT_MAX / RSHUNT 412 | # MaxPossible_I = 0.4A 413 | 414 | # 2. Determine max expected current 415 | # MaxExpected_I = 0.4A 416 | 417 | # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 418 | # MinimumLSB = MaxExpected_I/32767 419 | # MinimumLSB = 0.0000122 (12uA per bit) 420 | # MaximumLSB = MaxExpected_I/4096 421 | # MaximumLSB = 0.0000977 (98uA per bit) 422 | 423 | # 4. Choose an LSB between the min and max values 424 | # (Preferrably a roundish number close to MinLSB) 425 | # CurrentLSB = 0.00005 (50uA per bit) 426 | self._current_lsb = 0.05 # in milliamps 427 | 428 | # 5. Compute the calibration register 429 | # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 430 | # Cal = 8192 (0x2000) 431 | 432 | self._cal_value = 8192 433 | 434 | # 6. Calculate the power LSB 435 | # PowerLSB = 20 * CurrentLSB 436 | # PowerLSB = 0.001 (1mW per bit) 437 | self._power_lsb = 0.001 438 | 439 | # 7. Compute the maximum current and shunt voltage values before overflow 440 | # 441 | # Max_Current = Current_LSB * 32767 442 | # Max_Current = 1.63835A before overflow 443 | # 444 | # If Max_Current > Max_Possible_I then 445 | # Max_Current_Before_Overflow = MaxPossible_I 446 | # Else 447 | # Max_Current_Before_Overflow = Max_Current 448 | # End If 449 | # 450 | # Max_Current_Before_Overflow = MaxPossible_I 451 | # Max_Current_Before_Overflow = 0.4 452 | # 453 | # Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT 454 | # Max_ShuntVoltage = 0.04V 455 | # 456 | # If Max_ShuntVoltage >= VSHUNT_MAX 457 | # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 458 | # Else 459 | # Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage 460 | # End If 461 | # 462 | # Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX 463 | # Max_ShuntVoltage_Before_Overflow = 0.04V 464 | 465 | # 8. Compute the Maximum Power 466 | # MaximumPower = Max_Current_Before_Overflow * VBUS_MAX 467 | # MaximumPower = 0.4 * 16V 468 | # MaximumPower = 6.4W 469 | 470 | # Set Calibration register to 'Cal' calculated above 471 | self._raw_calibration = self._cal_value 472 | 473 | # Set Config register to take into account the settings above 474 | self.bus_voltage_range = BusVoltageRange.RANGE_16V 475 | self.gain = Gain.DIV_1_40MV 476 | self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S 477 | self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S 478 | self.mode = Mode.SANDBVOLT_CONTINUOUS 479 | 480 | def set_calibration_16V_5A(self) -> None: 481 | """Configures to INA219 to be able to measure up to 16V and 5000mA of current. Counter 482 | overflow occurs at 8.0A. 483 | 484 | .. note:: These calculations assume a 0.02 ohm shunt resistor is present""" 485 | # Calibration which uses the highest precision for 486 | # current measurement (0.1mA), at the expense of 487 | # only supporting 16V at 5000mA max. 488 | 489 | # VBUS_MAX = 16V 490 | # VSHUNT_MAX = 0.16 (Assumes Gain 3, 160mV) 491 | # RSHUNT = 0.02 (Resistor value in ohms) 492 | 493 | # 1. Determine max possible current 494 | # MaxPossible_I = VSHUNT_MAX / RSHUNT 495 | # MaxPossible_I = 8.0A 496 | 497 | # 2. Determine max expected current 498 | # MaxExpected_I = 5.0A 499 | 500 | # 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit) 501 | # MinimumLSB = MaxExpected_I/32767 502 | # MinimumLSB = 0.0001529 (uA per bit) 503 | # MaximumLSB = MaxExpected_I/4096 504 | # MaximumLSB = 0.0012207 (uA per bit) 505 | 506 | # 4. Choose an LSB between the min and max values 507 | # (Preferrably a roundish number close to MinLSB) 508 | # CurrentLSB = 0.00016 (uA per bit) 509 | self._current_lsb = 0.1524 # in milliamps 510 | 511 | # 5. Compute the calibration register 512 | # Cal = trunc (0.04096 / (Current_LSB * RSHUNT)) 513 | # Cal = 13434 (0x347a) 514 | 515 | self._cal_value = 13434 516 | 517 | # 6. Calculate the power LSB 518 | # PowerLSB = 20 * CurrentLSB 519 | # PowerLSB = 0.003 (3.048mW per bit) 520 | self._power_lsb = 0.003048 521 | 522 | # 7. Compute the maximum current and shunt voltage values before overflow 523 | # 524 | # 8. Compute the Maximum Power 525 | # 526 | 527 | # Set Calibration register to 'Cal' calcutated above 528 | self._raw_calibration = self._cal_value 529 | 530 | # Set Config register to take into account the settings above 531 | self.bus_voltage_range = BusVoltageRange.RANGE_16V 532 | self.gain = Gain.DIV_4_160MV 533 | self.bus_adc_resolution = ADCResolution.ADCRES_12BIT_1S 534 | self.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_1S 535 | self.mode = Mode.SANDBVOLT_CONTINUOUS 536 | -------------------------------------------------------------------------------- /docs/_static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_INA219/1cce6de54594a84f7be92d5cd0eec1dd89816bf7/docs/_static/favicon.ico -------------------------------------------------------------------------------- /docs/_static/favicon.ico.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: 2018 Phillip Torrone for Adafruit Industries 2 | 3 | SPDX-License-Identifier: CC-BY-4.0 4 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 1 | 2 | .. If you created a package, create one automodule per module in the package. 3 | 4 | API Reference 5 | ############# 6 | 7 | .. automodule:: adafruit_ina219 8 | :members: 9 | -------------------------------------------------------------------------------- /docs/api.rst.license: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | import datetime 6 | import os 7 | import sys 8 | 9 | sys.path.insert(0, os.path.abspath("..")) 10 | 11 | # -- General configuration ------------------------------------------------ 12 | 13 | # Add any Sphinx extension module names here, as strings. They can be 14 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 15 | # ones. 16 | extensions = [ 17 | "sphinx.ext.autodoc", 18 | "sphinxcontrib.jquery", 19 | "sphinx.ext.intersphinx", 20 | "sphinx.ext.viewcode", 21 | ] 22 | 23 | # Uncomment the below if you use native CircuitPython modules such as 24 | # digitalio, micropython and busio. List the modules you use. Without it, the 25 | # autodoc module docs will fail to generate with a warning. 26 | # autodoc_mock_imports = ["adafruit_bus_device", "micropython"] 27 | 28 | intersphinx_mapping = { 29 | "python": ("https://docs.python.org/3", None), 30 | "BusDevice": ( 31 | "https://docs.circuitpython.org/projects/busdevice/en/latest/", 32 | None, 33 | ), 34 | "CircuitPython": ("https://docs.circuitpython.org/en/latest/", None), 35 | } 36 | 37 | # Add any paths that contain templates here, relative to this directory. 38 | templates_path = ["_templates"] 39 | 40 | source_suffix = ".rst" 41 | 42 | # The master toctree document. 43 | master_doc = "index" 44 | 45 | # General information about the project. 46 | project = "Adafruit INA219 Library" 47 | creation_year = "2017" 48 | current_year = str(datetime.datetime.now().year) 49 | year_duration = ( 50 | current_year if current_year == creation_year else creation_year + " - " + current_year 51 | ) 52 | copyright = year_duration + " Dean Miller" 53 | author = "Dean Miller" 54 | 55 | # The version info for the project you're documenting, acts as replacement for 56 | # |version| and |release|, also used in various other places throughout the 57 | # built documents. 58 | # 59 | # The short X.Y version. 60 | version = "1.0" 61 | # The full version, including alpha/beta/rc tags. 62 | release = "1.0" 63 | 64 | # The language for content autogenerated by Sphinx. Refer to documentation 65 | # for a list of supported languages. 66 | # 67 | # This is also used if you do content translation via gettext catalogs. 68 | # Usually you set "language" from the command line for these cases. 69 | language = "en" 70 | 71 | # List of patterns, relative to source directory, that match files and 72 | # directories to ignore when looking for source files. 73 | # This patterns also effect to html_static_path and html_extra_path 74 | exclude_patterns = ["_build", "Thumbs.db", ".DS_Store", ".env", "CODE_OF_CONDUCT.md"] 75 | 76 | # The reST default role (used for this markup: `text`) to use for all 77 | # documents. 78 | # 79 | default_role = "any" 80 | 81 | # If true, '()' will be appended to :func: etc. cross-reference text. 82 | # 83 | add_function_parentheses = True 84 | 85 | # The name of the Pygments (syntax highlighting) style to use. 86 | pygments_style = "sphinx" 87 | 88 | # If true, `todo` and `todoList` produce output, else they produce nothing. 89 | todo_include_todos = False 90 | 91 | # If this is True, todo emits a warning for each TODO entries. The default is False. 92 | todo_emit_warnings = True 93 | 94 | 95 | # -- Options for HTML output ---------------------------------------------- 96 | 97 | # The theme to use for HTML and HTML Help pages. See the documentation for 98 | # a list of builtin themes. 99 | # 100 | import sphinx_rtd_theme 101 | 102 | html_theme = "sphinx_rtd_theme" 103 | 104 | # Add any paths that contain custom static files (such as style sheets) here, 105 | # relative to this directory. They are copied after the builtin static files, 106 | # so a file named "default.css" will overwrite the builtin "default.css". 107 | html_static_path = ["_static"] 108 | 109 | # The name of an image file (relative to this directory) to use as a favicon of 110 | # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 111 | # pixels large. 112 | # 113 | html_favicon = "_static/favicon.ico" 114 | 115 | # Output file base name for HTML help builder. 116 | htmlhelp_basename = "AdafruitINA219Librarydoc" 117 | 118 | # -- Options for LaTeX output --------------------------------------------- 119 | 120 | latex_elements = { 121 | # The paper size ('letterpaper' or 'a4paper'). 122 | # 123 | # 'papersize': 'letterpaper', 124 | # The font size ('10pt', '11pt' or '12pt'). 125 | # 126 | # 'pointsize': '10pt', 127 | # Additional stuff for the LaTeX preamble. 128 | # 129 | # 'preamble': '', 130 | # Latex figure (float) alignment 131 | # 132 | # 'figure_align': 'htbp', 133 | } 134 | 135 | # Grouping the document tree into LaTeX files. List of tuples 136 | # (source start file, target name, title, 137 | # author, documentclass [howto, manual, or own class]). 138 | latex_documents = [ 139 | ( 140 | master_doc, 141 | "AdafruitINA219Library.tex", 142 | "Adafruit INA219 Library Documentation", 143 | author, 144 | "manual", 145 | ), 146 | ] 147 | 148 | # -- Options for manual page output --------------------------------------- 149 | 150 | # One entry per manual page. List of tuples 151 | # (source start file, name, description, authors, manual section). 152 | man_pages = [ 153 | ( 154 | master_doc, 155 | "adafruitINA219library", 156 | "Adafruit INA219 Library Documentation", 157 | [author], 158 | 1, 159 | ) 160 | ] 161 | 162 | # -- Options for Texinfo output ------------------------------------------- 163 | 164 | # Grouping the document tree into Texinfo files. List of tuples 165 | # (source start file, target name, title, author, 166 | # dir menu entry, description, category) 167 | texinfo_documents = [ 168 | ( 169 | master_doc, 170 | "AdafruitINA219Library", 171 | "Adafruit INA219 Library Documentation", 172 | author, 173 | "AdafruitINA219Library", 174 | "One line description of project.", 175 | "Miscellaneous", 176 | ), 177 | ] 178 | -------------------------------------------------------------------------------- /docs/examples.rst: -------------------------------------------------------------------------------- 1 | Simple test 2 | ------------ 3 | 4 | Ensure your device works with this simple test. 5 | 6 | .. literalinclude:: ../examples/ina219_simpletest.py 7 | :caption: examples/ina219_simpletest.py 8 | :linenos: 9 | -------------------------------------------------------------------------------- /docs/examples.rst.license: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. include:: ../README.rst 2 | 3 | Table of Contents 4 | ================= 5 | 6 | .. toctree:: 7 | :maxdepth: 4 8 | :hidden: 9 | 10 | self 11 | 12 | .. toctree:: 13 | :caption: Examples 14 | 15 | examples 16 | 17 | .. toctree:: 18 | :caption: API Reference 19 | :maxdepth: 3 20 | 21 | api 22 | 23 | .. toctree:: 24 | :caption: Tutorials 25 | 26 | .. toctree:: 27 | :caption: Related Products 28 | 29 | Adafruit INA219 High Side DC Current Sensor Breakout 30 | 31 | Adafruit INA219 FeatherWing 32 | 33 | .. toctree:: 34 | :caption: Other Links 35 | 36 | Download from GitHub 37 | Download Library Bundle 38 | CircuitPython Reference Documentation 39 | CircuitPython Support Forum 40 | Discord Chat 41 | Adafruit Learning System 42 | Adafruit Blog 43 | Adafruit Store 44 | 45 | Indices and tables 46 | ================== 47 | 48 | * :ref:`genindex` 49 | * :ref:`modindex` 50 | * :ref:`search` 51 | -------------------------------------------------------------------------------- /docs/index.rst.license: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2020 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | sphinx 6 | sphinxcontrib-jquery 7 | sphinx-rtd-theme 8 | -------------------------------------------------------------------------------- /examples/ina219_simpletest.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries 2 | # SPDX-License-Identifier: MIT 3 | 4 | """Sample code and test for adafruit_ina219""" 5 | 6 | import time 7 | 8 | import board 9 | 10 | from adafruit_ina219 import INA219, ADCResolution, BusVoltageRange 11 | 12 | i2c_bus = board.I2C() # uses board.SCL and board.SDA 13 | # i2c_bus = board.STEMMA_I2C() # For using the built-in STEMMA QT connector on a microcontroller 14 | 15 | ina219 = INA219(i2c_bus) 16 | 17 | print("ina219 test") 18 | 19 | # display some of the advanced field (just to test) 20 | print("Config register:") 21 | print(" bus_voltage_range: 0x%1X" % ina219.bus_voltage_range) 22 | print(" gain: 0x%1X" % ina219.gain) 23 | print(" bus_adc_resolution: 0x%1X" % ina219.bus_adc_resolution) 24 | print(" shunt_adc_resolution: 0x%1X" % ina219.shunt_adc_resolution) 25 | print(" mode: 0x%1X" % ina219.mode) 26 | print("") 27 | 28 | # optional : change configuration to use 32 samples averaging for both bus voltage and shunt voltage 29 | ina219.bus_adc_resolution = ADCResolution.ADCRES_12BIT_32S 30 | ina219.shunt_adc_resolution = ADCResolution.ADCRES_12BIT_32S 31 | # optional : change voltage range to 16V 32 | ina219.bus_voltage_range = BusVoltageRange.RANGE_16V 33 | 34 | # measure and display loop 35 | while True: 36 | bus_voltage = ina219.bus_voltage # voltage on V- (load side) 37 | shunt_voltage = ina219.shunt_voltage # voltage between V+ and V- across the shunt 38 | current = ina219.current # current in mA 39 | power = ina219.power # power in watts 40 | 41 | # INA219 measure bus voltage on the load side. So PSU voltage = bus_voltage + shunt_voltage 42 | print(f"Voltage (VIN+) : {bus_voltage + shunt_voltage:6.3f} V") 43 | print(f"Voltage (VIN-) : {bus_voltage:6.3f} V") 44 | print(f"Shunt Voltage : {shunt_voltage:8.5f} V") 45 | print(f"Shunt Current : {current / 1000:7.4f} A") 46 | print(f"Power Calc. : {bus_voltage * (current / 1000):8.5f} W") 47 | print(f"Power Register : {power:6.3f} W") 48 | print("") 49 | 50 | # Check internal calculations haven't overflowed (doesn't detect ADC overflows) 51 | if ina219.overflow: 52 | print("Internal Math Overflow Detected!") 53 | print("") 54 | 55 | time.sleep(2) 56 | -------------------------------------------------------------------------------- /optional_requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | [build-system] 6 | requires = [ 7 | "setuptools", 8 | "wheel", 9 | "setuptools-scm", 10 | ] 11 | 12 | [project] 13 | name = "adafruit-circuitpython-ina219" 14 | description = "CircuitPython library for INA219 high side DC current sensor." 15 | version = "0.0.0+auto.0" 16 | readme = "README.rst" 17 | authors = [ 18 | {name = "Adafruit Industries", email = "circuitpython@adafruit.com"} 19 | ] 20 | urls = {Homepage = "https://github.com/adafruit/Adafruit_CircuitPython_INA219"} 21 | keywords = [ 22 | "adafruit", 23 | "blinka", 24 | "circuitpython", 25 | "micropython", 26 | "ina219", 27 | "sensor", 28 | "current", 29 | "high", 30 | "voltage", 31 | "featherwing", 32 | "breakout", 33 | "hardware", 34 | ] 35 | license = {text = "MIT"} 36 | classifiers = [ 37 | "Intended Audience :: Developers", 38 | "Topic :: Software Development :: Libraries", 39 | "Topic :: Software Development :: Embedded Systems", 40 | "Topic :: System :: Hardware", 41 | "License :: OSI Approved :: MIT License", 42 | "Programming Language :: Python :: 3", 43 | ] 44 | dynamic = ["dependencies"] 45 | 46 | [tool.setuptools] 47 | py-modules = ["adafruit_ina219"] 48 | 49 | [tool.setuptools.dynamic] 50 | dependencies = {file = ["requirements.txt"]} 51 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | Adafruit-Blinka 6 | adafruit-circuitpython-register 7 | adafruit-circuitpython-busdevice 8 | -------------------------------------------------------------------------------- /ruff.toml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | target-version = "py38" 6 | line-length = 100 7 | 8 | [lint] 9 | preview = true 10 | select = ["I", "PL", "UP"] 11 | 12 | extend-select = [ 13 | "D419", # empty-docstring 14 | "E501", # line-too-long 15 | "W291", # trailing-whitespace 16 | "PLC0414", # useless-import-alias 17 | "PLC2401", # non-ascii-name 18 | "PLC2801", # unnecessary-dunder-call 19 | "PLC3002", # unnecessary-direct-lambda-call 20 | "E999", # syntax-error 21 | "PLE0101", # return-in-init 22 | "F706", # return-outside-function 23 | "F704", # yield-outside-function 24 | "PLE0116", # continue-in-finally 25 | "PLE0117", # nonlocal-without-binding 26 | "PLE0241", # duplicate-bases 27 | "PLE0302", # unexpected-special-method-signature 28 | "PLE0604", # invalid-all-object 29 | "PLE0605", # invalid-all-format 30 | "PLE0643", # potential-index-error 31 | "PLE0704", # misplaced-bare-raise 32 | "PLE1141", # dict-iter-missing-items 33 | "PLE1142", # await-outside-async 34 | "PLE1205", # logging-too-many-args 35 | "PLE1206", # logging-too-few-args 36 | "PLE1307", # bad-string-format-type 37 | "PLE1310", # bad-str-strip-call 38 | "PLE1507", # invalid-envvar-value 39 | "PLE2502", # bidirectional-unicode 40 | "PLE2510", # invalid-character-backspace 41 | "PLE2512", # invalid-character-sub 42 | "PLE2513", # invalid-character-esc 43 | "PLE2514", # invalid-character-nul 44 | "PLE2515", # invalid-character-zero-width-space 45 | "PLR0124", # comparison-with-itself 46 | "PLR0202", # no-classmethod-decorator 47 | "PLR0203", # no-staticmethod-decorator 48 | "UP004", # useless-object-inheritance 49 | "PLR0206", # property-with-parameters 50 | "PLR0904", # too-many-public-methods 51 | "PLR0911", # too-many-return-statements 52 | "PLR0912", # too-many-branches 53 | "PLR0913", # too-many-arguments 54 | "PLR0914", # too-many-locals 55 | "PLR0915", # too-many-statements 56 | "PLR0916", # too-many-boolean-expressions 57 | "PLR1702", # too-many-nested-blocks 58 | "PLR1704", # redefined-argument-from-local 59 | "PLR1711", # useless-return 60 | "C416", # unnecessary-comprehension 61 | "PLR1733", # unnecessary-dict-index-lookup 62 | "PLR1736", # unnecessary-list-index-lookup 63 | 64 | # ruff reports this rule is unstable 65 | #"PLR6301", # no-self-use 66 | 67 | "PLW0108", # unnecessary-lambda 68 | "PLW0120", # useless-else-on-loop 69 | "PLW0127", # self-assigning-variable 70 | "PLW0129", # assert-on-string-literal 71 | "B033", # duplicate-value 72 | "PLW0131", # named-expr-without-context 73 | "PLW0245", # super-without-brackets 74 | "PLW0406", # import-self 75 | "PLW0602", # global-variable-not-assigned 76 | "PLW0603", # global-statement 77 | "PLW0604", # global-at-module-level 78 | 79 | # fails on the try: import typing used by libraries 80 | #"F401", # unused-import 81 | 82 | "F841", # unused-variable 83 | "E722", # bare-except 84 | "PLW0711", # binary-op-exception 85 | "PLW1501", # bad-open-mode 86 | "PLW1508", # invalid-envvar-default 87 | "PLW1509", # subprocess-popen-preexec-fn 88 | "PLW2101", # useless-with-lock 89 | "PLW3301", # nested-min-max 90 | ] 91 | 92 | ignore = [ 93 | "PLR2004", # magic-value-comparison 94 | "UP030", # format literals 95 | "PLW1514", # unspecified-encoding 96 | "PLR0913", # too-many-arguments 97 | "PLR0915", # too-many-statements 98 | "PLR0917", # too-many-positional-arguments 99 | "PLR0904", # too-many-public-methods 100 | "PLR0912", # too-many-branches 101 | "PLR0916", # too-many-boolean-expressions 102 | ] 103 | 104 | [format] 105 | line-ending = "lf" 106 | --------------------------------------------------------------------------------