Erriez I2C/SMB DC Voltage/Current/Power sensor library for Arduino  1.0.0
This is an I2C/SMB DC Voltage/Current/Power sensor library for Arduino
ErriezINA219.h
Go to the documentation of this file.
1 /*
2  * MIT License
3  *
4  * Copyright (c) 2020 Erriez
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 #ifndef ERRIEZ_INA219_H_
34 #define ERRIEZ_INA219_H_
35 
36 #include <stdint.h>
37 
38 // INA219 configuration
39 #define INA219_I2C_ADDRESS 0x40
40 #define INA219_SHUNT_RESISTOR 0.1
41 
42 // INA219 registers
43 #define INA219_REG_CONFIG 0x00
44 #define INA219_REG_SHUNTVOLTAGE 0x01
45 #define INA219_REG_BUSVOLTAGE 0x02
46 #define INA219_REG_POWER 0x03
47 #define INA219_REG_CURRENT 0x04
48 #define INA219_REG_CALIBRATION 0x05
49 
50 // INA219 register bit defines
51 #define INA219_CONFIG_RST 15
52 
53 #define INA219_CONFIG_BRNG 13
54 #define INA219_CONFIG_BRNG_16V (0 << 13)
55 #define INA219_CONFIG_BRNG_32V (1 << 13)
56 
57 #define INA219_CONFIG_GAIN_MASK (3 << 12)
58 #define INA219_CONFIG_GAIN_1 (0 << 12)
59 #define INA219_CONFIG_GAIN_2 (1 << 12)
60 #define INA219_CONFIG_GAIN_4 (2 << 12)
61 #define INA219_CONFIG_GAIN_8 (3 << 12)
62 
63 #define INA219_CONFIG_BADC_MASK 0x0780
64 #define INA219_CONFIG_BADC(adc) ((adc & INA219_CONFIG_BADC_MASK) << 7)
65 
66 #define INA219_CONFIG_SADC_MASK 0x0078
67 #define INA219_CONFIG_SADC(adc) ((adc & INA219_CONFIG_BADC_MASK) << 3)
68 
69 #define INA219_CONFIG_xADC_9B 0
70 #define INA219_CONFIG_xADC_10B 1
71 #define INA219_CONFIG_xADC_11B 2
72 #define INA219_CONFIG_xADC_12B 3
73 #define INA219_CONFIG_xADC_2S 9
74 #define INA219_CONFIG_xADC_4S 10
75 #define INA219_CONFIG_xADC_8S 11
76 #define INA219_CONFIG_xADC_16S 12
77 #define INA219_CONFIG_xADC_32S 13
78 #define INA219_CONFIG_xADC_64S 14
79 #define INA219_CONFIG_xADC_128S 15
80 
81 #define INA219_CONFIG_MODE_MASK 0x0007
82 #define INA219_CONFIG_MODE(mode) ((mode & INA219_CONFIG_MODE_MASK) << 0)
83 #define INA219_CONFIG_MODE_POWER_DOWN 0
84 #define INA219_CONFIG_MODE_SHUNT_TRG 1
85 #define INA219_CONFIG_MODE_BUS_TRG 2
86 #define INA219_CONFIG_MODE_SHUNT_BUS_TRG 3
87 #define INA219_CONFIG_MODE_ADC_OFF 4
88 #define INA219_CONFIG_MODE_SHUNT_CNT 5
89 #define INA219_CONFIG_MODE_BUS_CNT 6
90 #define INA219_CONFIG_MODE_SHUNT_BUS_CNT 7
91 
92 
100 #define REG_CONFIG_VALUE 0x399F
101 
102 
106 class INA219
107 {
108 public:
109  // Constructor
110  INA219(uint8_t i2cAddress=INA219_I2C_ADDRESS, float shuntResistor=INA219_SHUNT_RESISTOR);
111 
112  // INA219 functions
113  bool begin();
114  bool powerDown();
115  bool powerUp();
116  bool read();
117 
118  // I2C register access
119  void registerWrite(uint8_t reg, uint16_t val);
120  uint16_t registerRead(uint8_t reg);
121  uint8_t getI2CStatus();
122 
123  // Debug functions
124  void dumpRegisters(Stream *serial);
125 
126  float busVoltage;
127  float shuntVoltage;
128  float current;
129  float power;
130  bool overflow;
131  bool available;
132 
133 private:
134  void clear();
135 
136  uint8_t _i2cAddress;
137  uint8_t _i2cStatus;
138  float _shuntResistor;
139  bool _powerDown;
140 };
141 
142 
143 #endif // ERRIEZ_INA219_H_
#define INA219_SHUNT_RESISTOR
Default shunt resistor in Ohm.
Definition: ErriezINA219.h:40
#define INA219_I2C_ADDRESS
Default I2C address.
Definition: ErriezINA219.h:39
bool available
Successful conversion.
Definition: ErriezINA219.h:131
INA219 class.
Definition: ErriezINA219.h:106
bool overflow
Overflow.
Definition: ErriezINA219.h:130
void registerWrite(uint8_t reg, uint16_t val)
Write to INA219 register.
bool powerUp()
Power-up INA219.
bool read()
Read voltage and current from INA219.
float current
Current in mA.
Definition: ErriezINA219.h:128
uint8_t getI2CStatus()
Return status of the last I2C write, returned by Wire endTransfer()
float busVoltage
Bus voltage in V.
Definition: ErriezINA219.h:126
bool begin()
Initialize INA219.
uint16_t registerRead(uint8_t reg)
Read from INA219 register.
void dumpRegisters(Stream *serial)
Print I2C registers on serial port.
INA219(uint8_t i2cAddress=INA219_I2C_ADDRESS, float shuntResistor=INA219_SHUNT_RESISTOR)
INA219 constructor.
float shuntVoltage
Shunt voltage in mV.
Definition: ErriezINA219.h:127
bool powerDown()
Set INA219 in power-down mode.
float power
Power in mW.
Definition: ErriezINA219.h:129