INA2xx  v1.1.0
Arduino Library to read current, voltage and power data from one or more INA2xx device(s)
Arduino library to support the INAxxx family of current sensors

Description

Class definition header for the INA class. This library gives a common interface to various INA power monitor devices, see https://github.com/Zanduino/INA/wiki or the code below for a full list of currently supported devices. The INA devices have a 3-5V power supply and, depending upon the model, can measure voltages up to 26V or 36V. They are devices with High-Side / Low-Side Measurement, Bi-Directional Current and Power Monitor with I2C Compatible Interface. The device documentation can be found at the following location:
http://www.ti.com/amplifier-circuit/current-sense/power-current-monitors/products.html

Detailed library descriptions are on the INA GitHub Wiki pages at https://github.com/Zanduino/INA/wiki

The INA devices, apart from the INA250 and INA260, require an external shunt of known resistance to be placed across the high-side or low-side supply or ground line and they use the small current generated by the shunt to compute the amperage passing across the circuit. This value, coupled with the voltage measurement, allows the amperage and wattage to be computed by the INA device and these values can be read from the devices using the industry standard I2C protocol.

Programming

Coding and comments

OK, I admit that I'm "old school" when it comes to programming style. I am used to using a full monitor and keyboard for development and testing, plus I like to heavily document code as it helps me remember what I did when I revisit code after several months (or years). I make use of the full width (which I've limited to 112 characters here) and put my comments at the end of lines. I prefer to use descriptive variable names, which means that they tend to be long.

Style Guide

Different languages have different coding styles. For the Arduino c++ language I've opted to go with one of the big players in the industry and have adopted the coding and style rules that Google recommends and which are documented at Google c++ Style Guide. I have chosen to put braces on their own lines and include braces for even 1-liners. End braces are always commented so that convoluted code is more easily untangled.

Documentation

The comments have been formatted for use with Doxygen, one of the leading documentation systems which is not only free but covers just about anything worth documenting. The Doxygen system parses the source files of a package and creates documentation. The default output is a set of HTML pages although it can produce single documents.

Comment Format

This package uses Markdown syntax for formatting comments, which makes for easy reading directly in the source code and well-formatted pretty-print in postprocessing.

IDE

I've opted for using Microsoft Visual Studio (version 16.2.5) for development, the community version can be downloaded for free at Microsoft Downloads and I use the fantastic Visual Micro package which give the Arduino IDE inside Visual Studio. The base version is free or becomes only slightly annoying nagware after 90 days, but the software is inexpensive and the price is well worth it for supporting continued development.

doxygen configuration

This library is built with the standard "Doxyfile", which is located at https://github.com/Zanduino/Common/blob/main/Doxygen. As described on that page, there are only 5 environment variables used, and these are set in the project's actions file, located at https://github.com/Zanduino/INA/blob/master/.github/workflows/ci-doxygen.yml Edit this file and set the 5 variables - PRETTYNAME, PROJECT_NAME, PROJECT_NUMBER, PROJECT_BRIEF and PROJECT_LOGO so that these values are used in the doxygen documentation. The local copy of the doxyfile should be in the project's root directory in order to do local doxygen testing, but the file is ignored on upload to GitHub.

clang-format

Part of the GitHub actions for CI is running every source file through "clang-format" to ensure that coding formatting is done the same for all files. The configuration file ".clang-format" is located at https://github.com/Zanduino/Common/tree/main/clang-format and this is used for CI tests when pushing to GitHub. The local file, if present in the root directory, is ignored when committing and uploading.

GNU General Public License v3.0

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Author

Written by Arnd Arnd@.nosp@m.Zand.nosp@m.uino..nosp@m.Com at https://www.github.com/SV-Zanshin

Changelog

Version Date Developer Comments
1.1.2 2022-01-16 Oleg-Sob Issue #87. getBusMicroWatts() only returns positive values
1.1.1 2021-03-12 x3mEr Issue #79. Documentation Update
1.0.14 2020-12-01 SV-Zanshin Issue #72. Allow INA structures to be in memory rather than EEPROM
1.0.14 2020-11-30 johntaves Issue #64. begin() does not set values on subsequent calls
1.0.14 2020-11-29 SV-Zanshin Issue #71. Optimize library, cleanup source code
1.0.14 2020-10-25 gallium70 Issue #66. Error in INA226/230/231 value for "power_LSB"
1.0.13 2020-07-13 fg89o Issue #62. Added "_EEPROM_size" for ESP32 and ESP8266
1.0.13 2020-07-13 fg89o Issue #62. Incorrect "_EEPROM_offset" computation
1.0.12 2020-07-13 SV-Zanshin Issue #41. Added "_EEPROM_offset" variable
1.0.12 2020-07-03 sages Issue #60. Possible Overflow getBus(MicroAmps,MicroWatts)
1.0.11 2020-06-30 SV-Zanshin Issue #58, changed formatting to use clang-format
1.0.11 2020-06-29 SV-Zanshin Issue #57. "Alert..." functions should be "alert..."
1.0.11 2020-05-05 oliverb68 Issue #56. Limit of +/- 2kW on getBusMicroWatts
1.0.10 2020-05-03 we9v Issue #54. Limit of 127A maximum current changed to 1022A
1.0.10 2020-05-01 nathancheek Issue #53. Extraneous conversion on getShuntMicrovolts
1.0.10 2020-03-24 nathancheek Issue #52. Search for all 16 possible devices
1.0.10 2020-03-22 alphaarea Issue #50. Wiki fix for "begin()" - MaxBusAmps overflow
1.0.9 2019-12-15 Steamerzone Issue #49. Added ifdef for STM32F1 device support
1.0.9 2019-10-27 SV-Zanshin Cleaned up Doxygen formatting
1.0.9 2019-10-17 nathancheek Issue #47. Added EEPROM support for teensy
1.0.8 2019-09-03 miky2k Issue #43. Added new method "conversionFinished()"
1.0.8 2019-05-23 avaldebe Issue #42. Restrict I2C scan to possible devices
1.0.8 2019-03-24 mattlogic Issue #40. Corrected INA226_CONFIG_SADC_MASK value
1.0.8 2019-03-17 SV-Zanshin Issue #19. Corrected 4 value ranges in bus/shunt conversion
1.0.8 2019-02-16 SV-Zanshin Corrected and tested ESP32 implementation
1.0.8 2019-02-10 SV-Zanshin Issue #39. Allow non-AVR processors without EEPROM to run
1.0.8 2019-02-09 SV-Zanshin Cleaned up doxygen comment formatting in .h and .cpp files
1.0.8 2019-02-09 SV-Zanshin Issue #38. Add getDeviceAddress() function
1.0.7 2019-01-20 SV-Zanshin Issue #36&37. Changed for Travis-CI and automated doxygen
1.0.7 2018-12-27 SV-Zanshin Issue #33. Change program documentation to doxygen format
1.0.6 2018-12-13 delboy711 Issue #32. Incorrect ESP2866 rather than ESP8266
1.0.6 2018-10-19 SV-Zanshin Issue #31. Use full 0-32V Range on INA219 all the time
1.0.6 2018-10-19 SV-Zanshin Issue #30. Added TEENSY support & support large EEPROM
1.0.6 2018-10-14 SV-Zanshin Added correct wire handling for ESP8266 and ESP32
1.0.6 2018-10-07 SV-Zanshin Optimized getBusRaw() and getShuntRaw() functions
1.0.5 2018-10-04 SV-Zanshin Added getBusRaw() and getShuntRaw() functions
1.0.5 2018-09-29 SV-Zanshin Reformatted comments to different c++ coding style
1.0.4 2018-09-22 SV-Zanshin Issue #27. EEPROM Calls don't work with ESP32
1.0.4 2018-09-19 SV-Zanshin Issue #28. Ovef error when >31Amps specified in begin()
1.0.3 2018-09-04 delboy711 Issue #26. Incorrect INA3221 negative current readings
1.0.3 2018-08-18 SV-Zanshin Issue #22. Reduce EEPROM Footprint
1.0.3 2018-08-18 SV-Zanshin Issue #21. Rename I2C Constants to avoid redefine STM32F1
1.0.2 2018-07-22 SV-Zanshin Issue #11. Reduce EEPROM footprint. Removed "deviceName", 8B. Changed datatype in structure to bit-level length defs, saving additional 3 bytes
1.0.2 2018-07-21 avaldeve Issue #12. Incorrect const datatype for I2C Speeds
1.0.2 2018-07-12 coelner Issue #9. Esplora doesn't accept "Wire.begin()"
1.0.2 2018-07-08 SV-Zanshin Issue #2. Finished testing INA3221 across all functions
1.0.2 2018-07-07 dnlwgnd Issue #4. Guard code used incorrect label
1.0.2 2018-06-30 SV-Zanshin Issue #3. Adding faster I2C bus support
1.0.2 2018-06-29 SV-Zanshin Issue #2. Adding INA3221 support to library
1.0.2 2018-06-29 SV-Zanshin Issue #2. Adding INA3221 support to library
1.0.1 2018-06-24 SV-Zanshin Removed extraneous elements from ina struct, optimized code
1.0.1b 2018-06-23 SV-Zanshin Fixed error on multiple devices with ina structure contents
1.0.1a 2018-06-23 SV-Zanshin Removed debug mode and code
1.0.0 2018-06-22 SV-Zanshin Initial release
1.0.0b 2018-06-17 SV-Zanshin Continued coding, tested on INA219 and INA226
1.0.0a 2018-06-10 SV-Zanshin Initial coding began