Skip to content

Commit eaa92fa

Browse files
committed
0.6.1 INA226
1 parent f9155f8 commit eaa92fa

File tree

9 files changed

+94
-53
lines changed

9 files changed

+94
-53
lines changed

libraries/INA226/CHANGELOG.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ 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.1] - 2025-01-27
10+
- fix #49, precision math setMaxCurrentShunt() in INA226.cpp
11+
- changed max shunt voltage to 81.9 mV (0.02 under datasheet limit)
12+
- changed INA226_MINIMAL_SHUNT_OHM
13+
- update comments in INA226.h
14+
- add INA_comparison_table.md
15+
- updated readme.md
16+
917
## [0.6.0] - 2024-05-27
1018
- Fix #47, calibration register is 15 bit, not 16 bit.
1119
- minor edits
@@ -42,7 +50,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4250
- update readme.md
4351

4452
## [0.5.1] - 2023-12-10
45-
- reimplementation of **setMaxCurrentShunt()**,
53+
- reimplementation of **setMaxCurrentShunt()**,
4654
- thanks to tileiar
4755
- update readme.md
4856
- minor edits
@@ -95,7 +103,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
95103
- fix #11 normalize
96104
- fix #13 simplify sign handling shunt and current
97105
- add releaseNotes.md
98-
- **reset()** also resets the calibration (current_lsb, maxCurrent and shunt),
106+
- **reset()** also resets the calibration (current_lsb, maxCurrent and shunt),
99107
thereby forcing the user to redo the calibration call with **setMaxCurrentShunt()**.
100108
- fixes issue #11 => a factor 10 bug in current_lsb.
101109
- some edits in readme.md.
@@ -104,19 +112,19 @@ thereby forcing the user to redo the calibration call with **setMaxCurrentShunt(
104112
----
105113

106114
## [0.1.6] - 2021-12-20
107-
- update library.json,
108-
- license,
115+
- update library.json,
116+
- license,
109117
- minor edits
110118

111119
## [0.1.5] - 2021-11-05
112-
- update build-CI,
120+
- update build-CI,
113121
- add badges in readme.md
114122
- fix address in constructor
115123

116-
## [0.1.4] - 2021-08-07
124+
## [0.1.4] - 2021-08-07
117125
- fix getCurrent()
118126

119-
## [0.1.3] - 2021-06-22
127+
## [0.1.3] - 2021-06-22
120128
- add getCurrentLSB_uA()
121129
- improve examples
122130
- fix for calibration
@@ -127,11 +135,11 @@ thereby forcing the user to redo the calibration call with **setMaxCurrentShunt(
127135
- add getShunt()
128136
- add getMaxCurrent()
129137

130-
## [0.1.1] - 2021-06-21
138+
## [0.1.1] - 2021-06-21
131139
- improved calibration
132140
- added functions
133141

134-
## [0.1.0] - 2021-05-18
142+
## [0.1.0] - 2021-05-18
135143
- initial version
136144

137145

libraries/INA226/INA226.cpp

Lines changed: 7 additions & 5 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.0
3+
// VERSION: 0.6.1
44
// DATE: 2021-05-18
55
// PURPOSE: Arduino library for INA226 power sensor
66
// URL: https://github.com/RobTillaart/INA226
@@ -205,12 +205,14 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
205205

206206
// #define printdebug true
207207

208-
// fix #16 - datasheet 6.5 Electrical Characteristics
208+
// fix #16 - datasheet 6.5 Electrical Characteristics: 81.92 mV
209209
// rounded value to 80 mV
210+
// fix #49 - changed to 81.90 mV to have parameters (0.8, 0.1) working
211+
// 81.90 == datasheet limit - 0.02 mV to catch math overflow
210212
float shuntVoltage = maxCurrent * shunt;
211-
if (shuntVoltage > 0.080) return INA226_ERR_SHUNTVOLTAGE_HIGH;
212-
if (maxCurrent < 0.001) return INA226_ERR_MAXCURRENT_LOW;
213-
if (shunt < INA226_MINIMAL_SHUNT) return INA226_ERR_SHUNT_LOW;
213+
if (shuntVoltage > 0.08190) return INA226_ERR_SHUNTVOLTAGE_HIGH;
214+
if (maxCurrent < 0.001) return INA226_ERR_MAXCURRENT_LOW;
215+
if (shunt < INA226_MINIMAL_SHUNT_OHM) return INA226_ERR_SHUNT_LOW;
214216

215217
_current_LSB = maxCurrent * 3.0517578125e-5; // maxCurrent / 32768;
216218

libraries/INA226/INA226.h

Lines changed: 9 additions & 7 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.0
4+
// VERSION: 0.6.1
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.0"
16+
#define INA226_LIB_VERSION "0.6.1"
1717

1818

1919
// set by setAlertRegister
@@ -41,7 +41,7 @@
4141
#define INA226_ERR_NORMALIZE_FAILED 0x8003
4242

4343
// See issue #26
44-
#define INA226_MINIMAL_SHUNT 0.001
44+
#define INA226_MINIMAL_SHUNT_OHM 0.001
4545

4646
#define INA226_MAX_WAIT_MS 600 // millis
4747

@@ -116,10 +116,12 @@ class INA226
116116

117117

118118
// Calibration
119-
// mandatory to set these!
120-
// shunt * maxCurrent < 81 mV
121-
// maxCurrent >= 0.001
122-
// shunt >= 0.001
119+
// mandatory to set these values!
120+
// datasheet limit == 81.92 mV;
121+
// to prevent math overflow 0.02 mV is subtracted.
122+
// shunt * maxCurrent <= 81.9 mV otherwise returns INA226_ERR_SHUNTVOLTAGE_HIGH
123+
// maxCurrent >= 0.001 otherwise returns INA226_ERR_MAXCURRENT_LOW
124+
// shunt >= 0.001 otherwise returns INA226_ERR_SHUNT_LOW
123125
int setMaxCurrentShunt(float maxCurrent = 20.0, float shunt = 0.002, bool normalize = true);
124126
bool isCalibrated() { return _current_LSB != 0.0; };
125127

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
## Comparison table INA sensors
3+
4+
Comparison of my INA2xx libraries
5+
6+
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 | - | - | - |
21+
22+

libraries/INA226/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021-2024 Rob Tillaart
3+
Copyright (c) 2021-2025 Rob Tillaart
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

libraries/INA226/README.md

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ Read datasheet for details.
2626
The INA226 is a voltage, current and power measurement device.
2727
A few important maxima, see datasheet, chapter 6.
2828

29-
| description | max | unit | notes |
30-
|:--------------|------:|-------:|:------|
31-
| bus voltage | 36 | Volt | unclear for how long.
32-
| shunt voltage | 80 | mVolt |
33-
| current | 20 | Ampere |
29+
| description | max | unit | notes |
30+
|:----------------|-------:|---------:|:--------|
31+
| bus voltage | 36 | Volt | unclear for how long.
32+
| shunt voltage | 81.9 | mVolt | datasheet 81.92 mV
33+
| current | 20 | Ampere |
3434

3535

36-
#### 0.5.0 Breaking change
36+
### 0.5.0 Breaking change
3737

3838
Version 0.5.0 introduced a breaking change.
3939
You cannot set the pins in **begin()** any more.
@@ -42,25 +42,32 @@ The user has to call **Wire.begin()** and can optionally set the Wire pins
4242
before calling **begin()**.
4343

4444

45-
#### Special characters
45+
### Special characters
4646

4747
- Ω == Ohm = ALT-234 (Windows)
4848
- µ == micro = ALT-0181 (Windows)
4949

5050

51-
#### Related
51+
### Related
5252

5353
- https://www.ti.com/product/INA226#tech-docs
5454
- https://www.ti.com/product/INA226#params
5555
- https://www.ti.com/document-viewer/INA226/datasheet
56-
- https://github.com/RobTillaart/INA219
57-
- https://github.com/RobTillaart/INA226
58-
- https://github.com/RobTillaart/INA236
56+
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
57+
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
58+
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
59+
- https://github.com/RobTillaart/INA229 85 Volt, SPI, 20 bit
60+
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
61+
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
62+
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
63+
- https://www.adafruit.com/product/5832
64+
- https://www.mateksys.com/?portfolio=i2c-ina-bm
65+
- https://github.com/RobTillaart/printHelpers (for scientific notation)
5966

6067

6168
## I2C
6269

63-
#### Address
70+
### Address
6471

6572
The sensor can have 16 different I2C addresses,
6673
which depends on how the A0 and A1 address lines
@@ -88,7 +95,7 @@ See table - from datasheet table 2, page 18.
8895
| SCL | SCL | 79 | 0x4F |
8996

9097

91-
#### Performance
98+
### Performance
9299

93100
To be elaborated, example sketch available.
94101

@@ -137,7 +144,7 @@ Also see #30 for another typical deviation problem.
137144
```
138145

139146

140-
#### Constructor
147+
### Constructor
141148

142149
- **INA226(const uint8_t address, TwoWire \*wire = Wire)** Constructor to set
143150
the address and optional Wire interface.
@@ -148,7 +155,7 @@ Note: one needs to set **Wire.begin()** before calling **begin()**.
148155
- **uint8_t getAddress()** returns the address set in the constructor.
149156

150157

151-
#### Core Functions
158+
### Core Functions
152159

153160
Note the power and the current are not meaningful without calibrating the sensor.
154161
Also the value is not meaningful if there is no shunt connected.
@@ -179,7 +186,7 @@ Helper functions for the micro scale.
179186
- **float getPower_uW()** idem, in microWatt.
180187

181188

182-
#### Configuration
189+
### Configuration
183190

184191
**Note:**
185192
The internal conversions runs in the background in the device.
@@ -248,7 +255,7 @@ Note: times are typical, check datasheet for operational range.
248255
Note: total conversion time can take up to 1024 \* 8.3 ms ~ 10 seconds.
249256

250257

251-
#### Calibration
258+
### Calibration
252259

253260
See datasheet.
254261

@@ -257,7 +264,7 @@ Calibration is mandatory to get **getCurrent()** and **getPower()** to work.
257264
- **int setMaxCurrentShunt(float ampere = 20.0, float ohm = 0.002, bool normalize = true)**
258265
set the calibration register based upon the shunt and the max Ampere.
259266
From these two values the current_LSB is derived, the steps of the ADC when measuring current.
260-
Returns Error code, see below.
267+
Returns Error code, see below. See #49 about math rounding errors.
261268
- **bool isCalibrated()** returns true if CurrentLSB has been calculated by **setMaxCurrentShunt()**.
262269
Value should not be zero.
263270
- **float getCurrentLSB()** returns the LSB in Ampere == precision of the calibration.
@@ -269,7 +276,7 @@ Value should not be zero.
269276
To print these values in scientific notation use https://github.com/RobTillaart/printHelpers
270277

271278

272-
#### About normalization
279+
### About normalization
273280

274281
**setMaxCurrentShunt()** will round the current_LSB to nearest round value (typical 0.001) by default (normalize == true).
275282
- The user **must** check the return value == 0x000, otherwise the calibration register is **not** set.
@@ -283,18 +290,18 @@ normalize flag was set to true.
283290
See https://github.com/RobTillaart/INA226/pull/29 for details of the discussion.
284291

285292

286-
#### Error codes setMaxCurrentShunt
293+
### Error codes setMaxCurrentShunt
287294

288295
| descriptive name error | value | meaning |
289296
|:-------------------------------|:--------:|:----------|
290297
| INA226_ERR_NONE | 0x0000 | OK
291-
| INA226_ERR_SHUNTVOLTAGE_HIGH | 0x8000 | maxCurrent \* shunt > 80 mV
298+
| INA226_ERR_SHUNTVOLTAGE_HIGH | 0x8000 | maxCurrent \* shunt > 81.9 mV
292299
| INA226_ERR_MAXCURRENT_LOW | 0x8001 | maxCurrent < 0.001
293300
| INA226_ERR_SHUNT_LOW | 0x8002 | shunt < 0.001
294301
| INA226_ERR_NORMALIZE_FAILED | 0x8003 | not possible to normalize.
295302

296303

297-
#### Operating mode
304+
### Operating mode
298305

299306
See datasheet, partially tested.
300307

@@ -315,7 +322,7 @@ Descriptive mode functions (convenience wrappers).
315322
- **bool setModeShuntBusContinuous()** mode 7 - default.
316323

317324

318-
#### Alert functions
325+
### Alert functions
319326

320327
See datasheet, not tested yet.
321328

@@ -351,13 +358,13 @@ Returns true if write to register successful.
351358
The alert line falls when alert is reached.
352359

353360

354-
#### Meta information
361+
### Meta information
355362

356363
- **uint16_t getManufacturerID()** should return 0x5449
357364
- **uint16_t getDieID()** should return 0x2260
358365

359366

360-
#### Debugging
367+
### Debugging
361368

362369
- **uint16_t getRegister(uint8_t reg)** fetch registers directly, for debugging only.
363370

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.0",
18+
"version": "0.6.1",
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.0
2+
version=0.6.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for INA226 power sensor

libraries/INA226/test/unit_test_001.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ unittest(test_constants)
8080
assertEqual(0x8001, INA226_ERR_MAXCURRENT_LOW);
8181
assertEqual(0x8002, INA226_ERR_SHUNT_LOW);
8282

83-
assertEqualFloat(0.001, INA226_MINIMAL_SHUNT, 0.0001);
83+
assertEqualFloat(0.001, INA226_MINIMAL_SHUNT_OHM, 0.0001);
8484
}
8585

8686

@@ -157,9 +157,9 @@ unittest(test_calibration)
157157
assertEqual(INA226_ERR_NONE, INA.setMaxCurrentShunt(1, 0.05));
158158
assertEqual(INA226_ERR_NONE, INA.setMaxCurrentShunt(1, 0.080));
159159

160-
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(80.1, 0.001));
161-
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(40.1, 0.002));
162-
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(20.1, 0.004));
160+
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(82.0, 0.001));
161+
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(41.0, 0.002));
162+
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(20.5, 0.004));
163163
assertEqual(INA226_ERR_SHUNTVOLTAGE_HIGH, INA.setMaxCurrentShunt(1.1, 0.080));
164164

165165
assertEqual(INA226_ERR_MAXCURRENT_LOW, INA.setMaxCurrentShunt(0.0009));

0 commit comments

Comments
 (0)