Skip to content

Commit bfda213

Browse files
committed
0.6.4 INA226
1 parent d5d1c8d commit bfda213

File tree

9 files changed

+81
-29
lines changed

9 files changed

+81
-29
lines changed

libraries/INA226/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.6.4] - 2025-03-05
10+
- add **getLastError()** low level (I2C) error handling
11+
912
## [0.6.3] - 2025-02-12
1013
- fix #53, BUG in 0.6.2 **getBusVoltage()** looses decimals.
1114

libraries/INA226/INA226.cpp

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA226.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.6.2
3+
// VERSION: 0.6.4
44
// DATE: 2021-05-18
55
// PURPOSE: Arduino library for INA226 power sensor
66
// URL: https://github.com/RobTillaart/INA226
@@ -43,6 +43,7 @@ INA226::INA226(const uint8_t address, TwoWire *wire)
4343
_current_LSB = 0;
4444
_maxCurrent = 0;
4545
_shunt = 0;
46+
_error = 0;
4647
}
4748

4849

@@ -447,20 +448,46 @@ uint16_t INA226::getDieID()
447448
}
448449

449450

451+
////////////////////////////////////////////////////////
452+
//
453+
// ERROR HANDLING
454+
//
455+
int INA226::getLastError()
456+
{
457+
int e = _error;
458+
_error = 0;
459+
return e;
460+
}
461+
462+
450463
////////////////////////////////////////////////////////
451464
//
452465
// PRIVATE
453466
//
454467
uint16_t INA226::_readRegister(uint8_t reg)
455468
{
469+
_error = 0;
456470
_wire->beginTransmission(_address);
457471
_wire->write(reg);
458-
_wire->endTransmission();
472+
int n = _wire->endTransmission();
473+
if (n != 0)
474+
{
475+
_error = -1;
476+
return 0;
477+
}
459478

460-
_wire->requestFrom(_address, (uint8_t)2);
461-
uint16_t value = _wire->read();
462-
value <<= 8;
463-
value |= _wire->read();
479+
uint16_t value = 0;
480+
if (2 == _wire->requestFrom(_address, (uint8_t)2))
481+
{
482+
value = _wire->read();
483+
value <<= 8;
484+
value |= _wire->read();
485+
}
486+
else
487+
{
488+
_error = -2;
489+
return 0;
490+
}
464491
return value;
465492
}
466493

@@ -471,7 +498,12 @@ uint16_t INA226::_writeRegister(uint8_t reg, uint16_t value)
471498
_wire->write(reg);
472499
_wire->write(value >> 8);
473500
_wire->write(value & 0xFF);
474-
return _wire->endTransmission();
501+
int n = _wire->endTransmission();
502+
if (n != 0)
503+
{
504+
_error = -1;
505+
}
506+
return n;
475507
}
476508

477509

libraries/INA226/INA226.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA226.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.6.3
4+
// VERSION: 0.6.4
55
// DATE: 2021-05-18
66
// PURPOSE: Arduino library for INA226 power sensor
77
// URL: https://github.com/RobTillaart/INA226
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define INA226_LIB_VERSION "0.6.3"
16+
#define INA226_LIB_VERSION (F("0.6.4"))
1717

1818

1919
// set by setAlertRegister
@@ -162,18 +162,25 @@ class INA226
162162

163163

164164
// Meta information
165-
uint16_t getManufacturerID(); // should return 0x5449
166-
uint16_t getDieID(); // should return 0x2260
165+
//
166+
// typical value
167+
uint16_t getManufacturerID(); // 0x5449
168+
uint16_t getDieID(); // 0x2260
167169

168170

169171
// DEBUG
170172
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
171173

174+
//
175+
// ERROR HANDLING
176+
//
177+
int getLastError();
172178

173179
private:
174180

175181
uint16_t _readRegister(uint8_t reg);
176182
uint16_t _writeRegister(uint8_t reg, uint16_t value);
183+
177184
float _current_LSB;
178185
float _shunt;
179186
float _maxCurrent;
@@ -182,6 +189,8 @@ class INA226
182189

183190
uint8_t _address;
184191
TwoWire * _wire;
192+
193+
int _error;
185194
};
186195

187196

libraries/INA226/INA_comparison_table.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@
44
Comparison of my INA2xx libraries
55

66

7-
| | 219 | 226 | 228 | 229 | 236 | 239 | 3221 |
8-
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
9-
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 13 |
10-
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 26 |
11-
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C |
12-
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
13-
| | | | | | | | |
14-
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y |
15-
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y |
16-
| getCurrent | Y | Y | Y | Y | Y | Y | Y |
17-
| getPower | Y | Y | Y | Y | Y | Y | Y |
18-
| getTemperature | - | - | Y | Y | - | Y | - |
19-
| getEnergy | - | - | Y | Y | - | - | - |
20-
| getCharge | - | - | Y | Y | - | - | - |
7+
| | 219 | 226 | 228 | 229 | 236 | 239 | 260 | 3221 |
8+
|:------------------|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:|
9+
| bits | 12 | 16 | 20 | 20 | 16 | 16 | 16 | 13 |
10+
| Max Voltage | 26 | 36 | 85 | 85 | 48 | 85 | 36 | 26 |
11+
| Communication | I2C | I2C | I2C | SPI | I2C | SPI | I2C | I2C |
12+
| Channels | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3 |
13+
| | | | | | | | | |
14+
| getBusVoltage | Y | Y | Y | Y | Y | Y | Y | Y |
15+
| getShuntVoltage | Y | Y | Y | Y | Y | Y | Y | Y |
16+
| getCurrent | Y | Y | Y | Y | Y | Y | Y | Y |
17+
| getPower | Y | Y | Y | Y | Y | Y | Y | Y |
18+
| getTemperature | - | - | Y | Y | - | Y | - | - |
19+
| getEnergy | - | - | Y | Y | - | - | - | - |
20+
| getCharge | - | - | Y | Y | - | - | - | - |
2121

2222

23+
https://www.ti.com/amplifier-circuit/current-sense/digital-power-monitors/products.html
24+

libraries/INA226/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ You might prefer to multiply those values yourself to get extra digits.
135135
Please be aware that more digits is not always more exact (think significant digits).
136136

137137
The example sketch **INA226_setMaxCurrentShunt.ino** switches between two calibration modes.
138-
It shows the **INA266** sensor needs time to accommodate to this change.
138+
It shows the **INA226** sensor needs time to accommodate to this change.
139139
In practice you should call **setMaxCurrentShunt()** only once in **setup()**.
140140

141141
Also see #30 for another typical deviation problem.
@@ -377,6 +377,11 @@ The alert line falls when alert is reached.
377377
- **uint16_t getRegister(uint8_t reg)** fetch registers directly, for debugging only.
378378

379379

380+
### Error Handling
381+
382+
- **int getLastError()** returns last (I2C) error.
383+
384+
380385
## Adjusting the range of the INA226
381386

382387
**use at own risk**

libraries/INA226/examples/INA226_test_I2C/INA226_test_I2C.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: INA226_test_I2C.ino
33
// AUTHOR: Rob Tillaart
44
// PURPOSE: test I2C speed.
5-
// URL: https://github.com/RobTillaart/INA219
5+
// URL: https://github.com/RobTillaart/INA226
66

77

88
#include "INA226.h"

libraries/INA226/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ getAlertLimit KEYWORD2
6565
getManufacturerID KEYWORD2
6666
getDieID KEYWORD2
6767

68+
getLastError KEYWORD2
6869

6970
# Constants (LITERAL1)
7071
INA226_LIB_VERSION LITERAL1

libraries/INA226/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/INA226.git"
1717
},
18-
"version": "0.6.3",
18+
"version": "0.6.4",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/INA226/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA226
2-
version=0.6.3
2+
version=0.6.4
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for INA226 power sensor

0 commit comments

Comments
 (0)