INA219
Arduino library for INA219 voltage, current and power sensor
Install / Use
/learn @RobTillaart/INA219README
INA219
Arduino library for INA219 voltage, current and power sensor.
Description
Experimental library for the INA219 power sensor. Minimal tested, so usage remarks and comments are welcome.
Read datasheet for details.
USE WITH CARE
The INA219 is a voltage, current and power measurement device. A few important maxima, see datasheet, chapter 7, esp 7.5
| description | max | unit | notes | |:----------------|------:|--------:|:--------| | bus voltage | 32 | Volt | depends on BRNG setting | shunt voltage | 320 | mVolt | depends on PGA setting
Feedback as always is welcome.
0.4.0 Breaking change
Version 0.4.0 fixed negative values for getShuntVoltage(). Older versions are obsolete now.
0.2.0 Breaking change
Version 0.2.0 introduced a breaking change. You cannot set the pins in begin() any more. This reduces the dependency of processor dependent Wire implementations. The user has to call Wire.begin() and can optionally set the Wire pins before calling begin().
Special characters
- Ω == Ohm = ALT-234 (Windows)
- µ == micro = ALT-0181 (Windows)
Related
- https://www.ti.com/product/INA219#tech-docs
- https://www.ti.com/product/INA219#params
- https://www.ti.com/document-viewer/INA219/datasheet
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
- https://github.com/RobTillaart/INA229 85 Volt, SPI, 20 bit
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
- https://github.com/RobTillaart/INA238 85 Volt, I2C, 16 bit
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
- https://github.com/RobTillaart/INA260 36 Volt, SPI, 16 bit
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
- https://www.adafruit.com/product/5832
- https://www.mateksys.com/?portfolio=i2c-ina-bm
- https://github.com/RobTillaart/printHelpers (for scientific notation)
I2C
Address
The sensor can be configured to use 1 of 16 I2C addresses between 0x40 and 0x4F. The address depends on how the A0 and A1 address lines are connected to the SCL, SDA, GND and VCC pins. (datasheet chapter 8.5.5.1 Serial Bus Address)
Performance
Datasheet states it supports 1 KHz .. 2.56 MHz. Note: higher speeds and longer wires need smaller pull up resistors.
Some timings in micros for INA.getMode() on an Arduino UNO. This is just one readRegister call, similar to most functions.
Above 600 KHz there is little performance gain.
| speed | time | speed | time | speed | time | speed | time | |:-------:|:------:|:-------:|:------:|:-------:|:------:|:-------:|:------:| | 100000 | 560 | 150000 | 396 | 200000 | 320 | 250000 | 272 | | 300000 | 232 | 350000 | 208 | 400000 | 196 | 450000 | 176 | | 500000 | 172 | 550000 | 164 | 600000 | 152 | 650000 | 152 | | 700000 | 144 | 750000 | 144 | 800000 | 140 | | |
use INA219_test_I2C.ino
Interface
#include "INA219.h"
Constructor
- INA219(const uint8_t address, TwoWire *wire = Wire) Constructor to set the address and optional Wire interface.
- bool begin() initializes the class. Returns true if the INA219 address (set in the constructor) is on the I2C bus.
- bool isConnected() Returns true if the INA219 address (set in the constructor) is on the I2C bus.
- uint8_t getAddress() Returns the INA219 address set in the constructor.
Core Functions
Note the power and the current are not meaningful without calibrating the sensor. Also the value is not meaningful if there is no shunt connected.
- float getShuntVoltage() idem, in volts.
- float getBusVoltage() idem. in volts. Max 32 Volt.
- float getCurrent() returns the current through the shunt in Ampere.
- float getPower() returns the current x BusVoltage in Watt.
The library has helper functions to convert above output to a more appropriate scale of units.
Helper functions for the milli scale.
- float getBusVoltage_mV() idem, in milliVolts. Note: returns -100 if the math overflow bit is set.
- float getShuntVoltage_mV() idem, in milliVolts.
- float getCurrent_mA() idem, in milliAmpere.
- float getPower_mW() idem, in milliWatt.
Helper functions for the micro scale.
- float getBusVoltage_uV() idem, in microVolts.
- float getShuntVoltage_uV() idem, in microVolts.
- float getCurrent_uA() idem, in microAmpere.
- float getPower_uW() idem, in microWatt.
Indicator flags
- bool getMathOverflowFlag() internal math overflow.
- bool getConversionFlag() conversion is ready. Especially useful in non-continuous modi.
Configuration
- bool reset() software power on reset. This implies that calibration with setMaxCurrentShunt() needs to be redone. See section below. Returns false if it could not write settings to device.
- bool setBusVoltageRange(uint8_t voltage = 16) set to 16 or 32. Values <= 16 map to 16 and values between 16 and 32 map to 32. Returns false if voltage is above 32. Returns false if it could not write settings to device.
- uint8_t getBusVoltageRange() returns 16 or 32. (Volts)
- bool setGain(uint8_t factor) factor = 1, 2, 4, 8 (default). Determines the shunt voltage range. 40, 80, 160 or 320 mV. Returns false if factor is not a valid value. Returns false if it could not write settings to device.
- uint8_t getGain() returns set factor.
- int getMaxShuntVoltage() returns 40, 80, 160 or 320 (mV). 320 is the sensors default.
Configuration BUS and SHUNT
Note: The internal conversions runs in the background in the device. If a conversion is finished the measured value is stored in the appropriate register. The last obtained values can always be read from the registers, so they will not block. Result can be that you get the very same value if no new data is available yet. This is especially true if you increase the number of samples. (See also discussion in INA219 issue 11).
Using more samples reduces the noise level, but one will miss the faster changes in voltage or current. Depending on your project needs you can choose one over the other.
As a rule of thumb one could take the time between two I2C reads of a register as an upper limit. This would result in a fresh measurement every time one reads the register. NB it is always possible to average readings fetched from the device in your own code.
Use one of these three so set bus resolution and sampling.
- bool setBusResolution(uint8_t bits) bits = 9..12, always 1 sample. Returns false if parameter out of range. Returns false if it could not write settings to device.
- bool setBusSamples(uint8_t value) value = 0..7 => maps to 2^value samples. Always 12 bits. Returns false if parameter out of range. Returns false if it could not write settings to device.
- bool setBusADC(uint8_t mask = 0x03) see table below. Check datasheet for all details. Returns false if parameter out of range (mask > 0x0F). Returns false if it could not write settings to device.
- uint8_t getBusADC() returns mask, see table below.
Use one of these three so set shunt resolution and sampling.
- bool setShuntResolution(uint8_t bits) bits = 9..12, always 1 sample. Returns false if parameter out of range. Returns false if it could not write settings to device.
- bool setShuntSamples(uint8_t value) value = 0..7 => maps to 2^value samples. Always 12 bits. Returns false if parameter out of range. Returns false if it could not write settings to device.
- bool setShuntADC(uint8_t mask = 0x03) see table below. Check datasheet for all details. Returns false if parameter out of range (mask > 0x0F). Returns false if it could not write settings to device.
- uint8_t getShuntADC() returns mask, see table below.
Resolution samples table
mask = both resolution + averaging multiple samples. minus - == don't care
| bit mask | value | resolution | samples | conversion time | |:----------:|:-------:|:-------------|:--------------|:---------------:| | 0-00 | 0 / 4 | 9 bit | 1 sample | 84 μs | | 0-01 | 1 / 5 | 10 bit | 1 sample | 148 μs | | 0-10 | 2 / 6 | 11 bit | 1 sample | 276 μs | | 0-11 | 3 / 7 | 12 bit | 1 sample | 532 μs | | | | | | | | 1000 | 8 | 12 bit | 1 sample | 532 μs | | 1001 | 9 | 12 bit | 2 samples | 1.06 ms | | 1010 | 10 | 12 bit | 4 samples | 2.13 ms | | 1011 | 11 | 12 bit | 8 samples | 4.26 ms | | 1100 | 12 | 12 bit | 16 s
