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.cpp
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 #include <Arduino.h>
34 #include <Wire.h>
35 
36 #include "ErriezINA219.h"
37 
38 
46 INA219::INA219(uint8_t i2cAddress, float shuntResistor) :
47  _i2cAddress(i2cAddress), _shuntResistor(shuntResistor)
48 {
49 
50 }
51 
60 {
61  return powerUp();
62 }
63 
72 {
73  // Keep last conversion
74 
75  _powerDown = true;
76 
77  // Initialize config register with default configuration
79 
80  return _i2cStatus;
81 }
82 
91 {
92  // Clear values before start
93  clear();
94 
95  _powerDown = false;
96 
97  // Initialize config register
99 
100  return _i2cStatus;
101 }
102 
113 {
114  uint16_t shuntVoltageReg;
115  uint16_t busVoltageReg;
116 
117  // Check power state
118  if (_powerDown) {
119  return false;
120  }
121 
122  // Clear measurement variables before read
123  clear();
124 
125  // Read shunt voltage
126  shuntVoltageReg = registerRead(INA219_REG_SHUNTVOLTAGE);
127  if (_i2cStatus != 0) { return false; }
128 
129  // Read bus voltage
130  busVoltageReg = registerRead(INA219_REG_BUSVOLTAGE);
131  if (_i2cStatus != 0) { return false; }
132 
133  // Calculate shunt voltage in mV
134  if (shuntVoltageReg & 0x8000) {
135  // Two's complete on 16-bit unsigned register value
136  shuntVoltage = (int16_t)((~shuntVoltageReg) + 1) * -1;
137  } else {
138  shuntVoltage = shuntVoltageReg;
139  }
140  shuntVoltage /= 100;
141 
142  // Calculate bus voltage in mV
143  if (busVoltageReg & 0x01) {
144  // OVF (Overflow) bit is set
145  busVoltage = 0xFFFF;
146  overflow = true;
147  } else {
148  // Return bus voltage bits 3..15 where LSB = 4 mV
149  busVoltage = (busVoltageReg >> 3) * 4;
150  overflow = false;
151  }
152  busVoltage /= 1000;
153 
154  // Calculate current in mA: I = U / Rshunt
155  current = shuntVoltage / _shuntResistor;
156 
157  // Calculate power in mW: P = U * I
159  if (power < 0) {
160  // Make power absolute
161  power *= -1;
162  }
163 
164  // Conversion completed
165  available = true;
166 
167  return available;
168 }
169 
173 void INA219::clear()
174 {
175  busVoltage = 0;
176  shuntVoltage = 0;
177  current = 0;
178  power = 0;
179  available = false;
180 }
181 
191 uint16_t INA219::registerRead(uint8_t reg)
192 {
193  Wire.beginTransmission(_i2cAddress);
194  Wire.write(&reg, 1);
195  _i2cStatus = Wire.endTransmission(false);
196  (void)Wire.requestFrom(_i2cAddress, (uint8_t)2);
197  return (Wire.read() << 8) | Wire.read();
198 }
199 
209 void INA219::registerWrite(uint8_t reg, uint16_t val)
210 {
211  const uint8_t buffer[3] = { reg, (uint8_t)(val >> 8), (uint8_t)(val & 0xff) };
212 
213  Wire.beginTransmission(_i2cAddress);
214  Wire.write(buffer, sizeof(buffer));
215  _i2cStatus = (Wire.endTransmission() == 0) ? true : false;
216 }
217 
227 {
228  /* Status of the endTransmission */
229  return _i2cStatus;
230 }
231 
239 void INA219::dumpRegisters(Stream *serial)
240 {
241  serial->println(F("INA219 registers:"));
242 
243  serial->print(F(" 00 CONFIG: 0x"));
244  serial->println(registerRead(INA219_REG_CONFIG), HEX);
245  serial->print(F(" 01 SHUNTVOLTAGE: 0x"));
246  serial->println(registerRead(INA219_REG_SHUNTVOLTAGE), HEX);
247  serial->print(F(" 02 BUSVOLTAGE: 0x"));
248  serial->println(registerRead(INA219_REG_BUSVOLTAGE), HEX);
249  serial->print(F(" 03 POWER: 0x"));
250  serial->println(registerRead(INA219_REG_POWER), HEX);
251  serial->print(F(" 04 CURRENT: 0x"));
252  serial->println(registerRead(INA219_REG_CURRENT), HEX);
253  serial->print(F(" 05 CALIBRATION: 0x"));
254  serial->println(registerRead(INA219_REG_CALIBRATION), HEX);
255 }
#define INA219_REG_POWER
Power register.
Definition: ErriezINA219.h:46
bool available
Successful conversion.
Definition: ErriezINA219.h:131
bool overflow
Overflow.
Definition: ErriezINA219.h:130
void registerWrite(uint8_t reg, uint16_t val)
Write to INA219 register.
bool powerUp()
Power-up INA219.
#define REG_CONFIG_VALUE
Default config register value.
Definition: ErriezINA219.h:100
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()
INA219 voltage and current sensor library for Arduino.
#define INA219_REG_CALIBRATION
Calibration register.
Definition: ErriezINA219.h:48
float busVoltage
Bus voltage in V.
Definition: ErriezINA219.h:126
#define INA219_REG_CONFIG
Config register.
Definition: ErriezINA219.h:43
#define INA219_REG_CURRENT
Current register.
Definition: ErriezINA219.h:47
bool begin()
Initialize INA219.
#define INA219_REG_SHUNTVOLTAGE
Shunt/voltage register.
Definition: ErriezINA219.h:44
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
#define INA219_REG_BUSVOLTAGE
Bus voltage register.
Definition: ErriezINA219.h:45
bool powerDown()
Set INA219 in power-down mode.
float power
Power in mW.
Definition: ErriezINA219.h:129