Adafruit INA219 Arduino Library
Adafruit_INA219.h
Go to the documentation of this file.
1 
17 #ifndef _LIB_ADAFRUIT_INA219_
18 #define _LIB_ADAFRUIT_INA219_
19 
20 #include "Arduino.h"
21 #include <Adafruit_BusIO_Register.h>
22 #include <Adafruit_I2CDevice.h>
23 #include <Wire.h>
24 
26 /* The address is controlled by the A0 and A1 inputs on the INA219:
27  *
28  * Calculated address: b100ABCD
29  * A0 controls C and D: GND = 00, V+ = 01, SDA = 10, SCL = 11
30  * A1 controls A and B: GND = 00, V+ = 01, SDA = 10, SCL = 11
31  *
32  * E.g. if A0 is tied to ground and A1 is tied to V+,
33  * the resulting address is b1000100 = 0x44
34  *
35  * SDA and SCL options aren't implemented.
36  */
37 #define INA219_CALC_ADDRESS(INA_ADDR0, INA_ADDR1) \
38  (0x40 | (INA_ADDR0 != 0 ? 0x01 : 0x00) | (INA_ADDR1 != 0 ? 0x04 : 0x00))
39 
41 #define INA219_ADDRESS (0x40) // 1000000 (A0+A1=GND)
42 
44 #define INA219_READ (0x01)
45 
46 /*=========================================================================
47  CONFIG REGISTER (R/W)
48 **************************************************************************/
49 
51 #define INA219_REG_CONFIG (0x00)
52 
54 #define INA219_CONFIG_RESET (0x8000) // Reset Bit
55 
57 #define INA219_CONFIG_BVOLTAGERANGE_MASK (0x2000) // Bus Voltage Range Mask
58 
60 enum {
61  INA219_CONFIG_BVOLTAGERANGE_16V = (0x0000), // 0-16V Range
62  INA219_CONFIG_BVOLTAGERANGE_32V = (0x2000), // 0-32V Range
63 };
64 
66 #define INA219_CONFIG_GAIN_MASK (0x1800) // Gain Mask
67 
69 enum {
70  INA219_CONFIG_GAIN_1_40MV = (0x0000), // Gain 1, 40mV Range
71  INA219_CONFIG_GAIN_2_80MV = (0x0800), // Gain 2, 80mV Range
72  INA219_CONFIG_GAIN_4_160MV = (0x1000), // Gain 4, 160mV Range
73  INA219_CONFIG_GAIN_8_320MV = (0x1800), // Gain 8, 320mV Range
74 };
75 
77 #define INA219_CONFIG_BADCRES_MASK (0x0780)
78 
80 enum {
81  INA219_CONFIG_BADCRES_9BIT = (0x0000), // 9-bit bus res = 0..511
82  INA219_CONFIG_BADCRES_10BIT = (0x0080), // 10-bit bus res = 0..1023
83  INA219_CONFIG_BADCRES_11BIT = (0x0100), // 11-bit bus res = 0..2047
84  INA219_CONFIG_BADCRES_12BIT = (0x0180), // 12-bit bus res = 0..4097
85  INA219_CONFIG_BADCRES_12BIT_2S_1060US =
86  (0x0480), // 2 x 12-bit bus samples averaged together
87  INA219_CONFIG_BADCRES_12BIT_4S_2130US =
88  (0x0500), // 4 x 12-bit bus samples averaged together
89  INA219_CONFIG_BADCRES_12BIT_8S_4260US =
90  (0x0580), // 8 x 12-bit bus samples averaged together
91  INA219_CONFIG_BADCRES_12BIT_16S_8510US =
92  (0x0600), // 16 x 12-bit bus samples averaged together
93  INA219_CONFIG_BADCRES_12BIT_32S_17MS =
94  (0x0680), // 32 x 12-bit bus samples averaged together
95  INA219_CONFIG_BADCRES_12BIT_64S_34MS =
96  (0x0700), // 64 x 12-bit bus samples averaged together
97  INA219_CONFIG_BADCRES_12BIT_128S_69MS =
98  (0x0780), // 128 x 12-bit bus samples averaged together
99 
100 };
101 
103 #define INA219_CONFIG_SADCRES_MASK \
104  (0x0078) // Shunt ADC Resolution and Averaging Mask
105 
107 enum {
108  INA219_CONFIG_SADCRES_9BIT_1S_84US = (0x0000), // 1 x 9-bit shunt sample
109  INA219_CONFIG_SADCRES_10BIT_1S_148US = (0x0008), // 1 x 10-bit shunt sample
110  INA219_CONFIG_SADCRES_11BIT_1S_276US = (0x0010), // 1 x 11-bit shunt sample
111  INA219_CONFIG_SADCRES_12BIT_1S_532US = (0x0018), // 1 x 12-bit shunt sample
112  INA219_CONFIG_SADCRES_12BIT_2S_1060US =
113  (0x0048), // 2 x 12-bit shunt samples averaged together
114  INA219_CONFIG_SADCRES_12BIT_4S_2130US =
115  (0x0050), // 4 x 12-bit shunt samples averaged together
116  INA219_CONFIG_SADCRES_12BIT_8S_4260US =
117  (0x0058), // 8 x 12-bit shunt samples averaged together
118  INA219_CONFIG_SADCRES_12BIT_16S_8510US =
119  (0x0060), // 16 x 12-bit shunt samples averaged together
120  INA219_CONFIG_SADCRES_12BIT_32S_17MS =
121  (0x0068), // 32 x 12-bit shunt samples averaged together
122  INA219_CONFIG_SADCRES_12BIT_64S_34MS =
123  (0x0070), // 64 x 12-bit shunt samples averaged together
124  INA219_CONFIG_SADCRES_12BIT_128S_69MS =
125  (0x0078), // 128 x 12-bit shunt samples averaged together
126 };
127 
129 #define INA219_CONFIG_MODE_MASK (0x0007) // Operating Mode Mask
130 
132 enum {
137  0x03,
142  0x07,
143 };
144 
146 #define INA219_REG_SHUNTVOLTAGE (0x01)
147 
149 #define INA219_REG_BUSVOLTAGE (0x02)
150 
152 #define INA219_REG_POWER (0x03)
153 
155 #define INA219_REG_CURRENT (0x04)
156 
158 #define INA219_REG_CALIBRATION (0x05)
159 
165 public:
166  Adafruit_INA219(uint8_t addr = INA219_ADDRESS);
168  bool begin(TwoWire *theWire = &Wire);
169  void setCalibration_32V_2A();
170  void setCalibration_32V_1A();
172  float getBusVoltage_V();
173  float getShuntVoltage_mV();
174  float getCurrent_mA();
175  float getPower_mW();
176  void powerSave(bool on);
177  bool success();
178 
179 private:
180  Adafruit_I2CDevice *i2c_dev = NULL;
181 
182  bool _success;
183 
184  uint8_t ina219_i2caddr = -1;
185  uint32_t ina219_calValue;
186  // The following multipliers are used to convert raw current and power
187  // values to mA and mW, taking into account the current config settings
188  uint32_t ina219_currentDivider_mA;
189  float ina219_powerMultiplier_mW;
190 
191  void init();
192  int16_t getBusVoltage_raw();
193  int16_t getShuntVoltage_raw();
194  int16_t getCurrent_raw();
195  int16_t getPower_raw();
196 };
197 
198 #endif
void powerSave(bool on)
Set power save mode according to parameters.
Definition: Adafruit_INA219.cpp:280
float getCurrent_mA()
Gets the current value in mA, taking into account the config settings and current LSB...
Definition: Adafruit_INA219.cpp:168
float getShuntVoltage_mV()
Gets the shunt voltage in mV (so +-327mV)
Definition: Adafruit_INA219.cpp:148
bool begin(TwoWire *theWire=&Wire)
Sets up the HW (defaults to 32V and 2A for calibration values)
Definition: Adafruit_INA219.cpp:53
Definition: Adafruit_INA219.h:135
bool success()
Provides the the underlying return value from the last operation called on the device.
Definition: Adafruit_INA219.cpp:486
Definition: Adafruit_INA219.h:139
Definition: Adafruit_INA219.h:133
void setCalibration_32V_1A()
Configures to INA219 to be able to measure up to 32V and 1A of current. Each unit of current correspo...
Definition: Adafruit_INA219.cpp:300
float getPower_mW()
Gets the power value in mW, taking into account the config settings and current LSB.
Definition: Adafruit_INA219.cpp:179
Definition: Adafruit_INA219.h:140
float getBusVoltage_V()
Gets the bus voltage in volts.
Definition: Adafruit_INA219.cpp:158
void setCalibration_16V_400mA()
set device to alibration which uses the highest precision for current measurement (0...
Definition: Adafruit_INA219.cpp:391
Class that stores state and functions for interacting with INA219 current/power monitor IC...
Definition: Adafruit_INA219.h:164
Definition: Adafruit_INA219.h:134
~Adafruit_INA219()
INA219 class destructor.
Definition: Adafruit_INA219.cpp:46
Definition: Adafruit_INA219.h:136
Definition: Adafruit_INA219.h:138
Definition: Adafruit_INA219.h:141
Adafruit_INA219(uint8_t addr=INA219_ADDRESS)
Instantiates a new INA219 class.
Definition: Adafruit_INA219.cpp:37
void setCalibration_32V_2A()
Configures to INA219 to be able to measure up to 32V and 2A of current. Each unit of current correspo...
Definition: Adafruit_INA219.cpp:192
#define INA219_ADDRESS
Definition: Adafruit_INA219.h:41