Skip to content

Commit a5ad481

Browse files
committed
0.1.5 INA228
1 parent 4c4eaf0 commit a5ad481

File tree

12 files changed

+198
-41
lines changed

12 files changed

+198
-41
lines changed

libraries/INA228/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ 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.1.5] - 2025-01-30
10+
- fix #17, cache ADCRange to improve getShuntVoltage()
11+
- add **INA_comparison_table.md**
12+
- minor edits
13+
914
## [0.1.4] - 2024-10-28
1015
- add and rename wrappers for core functions
1116
- update all examples

libraries/INA228/INA228.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// FILE: INA228.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.1.4
3+
// VERSION: 0.1.5
44
// DATE: 2024-05-09
5-
// PURPOSE: Arduino library for INA228 voltage, current and power sensor.
5+
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
66
// URL: https://github.com/RobTillaart/INA228
77
// https://www.adafruit.com/product/5832 ( 10 A version)
88
// https://www.mateksys.com/?portfolio=i2c-ina-bm (200 A version))
@@ -42,7 +42,7 @@
4242
#define INA228_CFG_CONVDLY 0x3FC0
4343
#define INA228_CFG_TEMPCOMP 0x0020
4444
#define INA228_CFG_ADCRANGE 0x0010
45-
#define INA228_CFG_RESERVED 0x000F
45+
#define INA228_CFG_RESERVED 0x000F // all unused bits
4646

4747

4848
// ADC MASKS (register 1)
@@ -71,6 +71,8 @@ INA228::INA228(const uint8_t address, TwoWire *wire)
7171
bool INA228::begin()
7272
{
7373
if (! isConnected()) return false;
74+
75+
getADCRange();
7476
return true;
7577
}
7678

@@ -107,7 +109,7 @@ float INA228::getShuntVoltage()
107109
{
108110
// shunt_LSB depends on ADCRANGE in INA228_CONFIG register.
109111
float shunt_LSB = 312.5e-9; // 312.5 nV
110-
if (getADCRange() == 1)
112+
if (_ADCRange == true)
111113
{
112114
shunt_LSB = 78.125e-9; // 78.125 nV
113115
}
@@ -140,7 +142,7 @@ float INA228::getCurrent()
140142
// PAGE 26 + 8.1.2
141143
float INA228::getPower()
142144
{
143-
uint32_t value = _readRegister(INA228_POWER, 3) ;
145+
uint32_t value = _readRegister(INA228_POWER, 3);
144146
// PAGE 31 (8.1.2)
145147
return value * 3.2 * _current_LSB;
146148
}
@@ -192,8 +194,8 @@ bool INA228::setAccumulation(uint8_t value)
192194
{
193195
if (value > 1) return false;
194196
uint16_t reg = _readRegister(INA228_CONFIG, 2);
195-
reg &= ~INA228_CFG_RSTACC;
196197
if (value == 1) reg |= INA228_CFG_RSTACC;
198+
else reg &= ~INA228_CFG_RSTACC;
197199
_writeRegister(INA228_CONFIG, reg);
198200
return true;
199201
}
@@ -221,8 +223,8 @@ uint8_t INA228::getConversionDelay()
221223
void INA228::setTemperatureCompensation(bool on)
222224
{
223225
uint16_t value = _readRegister(INA228_CONFIG, 2);
224-
value &= ~INA228_CFG_TEMPCOMP;
225226
if (on) value |= INA228_CFG_TEMPCOMP;
227+
else value &= ~INA228_CFG_TEMPCOMP;
226228
_writeRegister(INA228_CONFIG, value);
227229
}
228230

@@ -234,16 +236,20 @@ bool INA228::getTemperatureCompensation()
234236

235237
void INA228::setADCRange(bool flag)
236238
{
239+
// if (flag == _ADCRange) return;
240+
_ADCRange = flag;
237241
uint16_t value = _readRegister(INA228_CONFIG, 2);
238-
value &= ~INA228_CFG_ADCRANGE;
239242
if (flag) value |= INA228_CFG_ADCRANGE;
243+
else value &= ~INA228_CFG_ADCRANGE;
244+
// if value has not changed we do not need to write it back.
240245
_writeRegister(INA228_CONFIG, value);
241246
}
242247

243248
bool INA228::getADCRange()
244249
{
245250
uint16_t value = _readRegister(INA228_CONFIG, 2);
246-
return (value & INA228_CFG_ADCRANGE) > 0;
251+
_ADCRange = (value & INA228_CFG_ADCRANGE) > 0;
252+
return _ADCRange;
247253
}
248254

249255

@@ -347,7 +353,7 @@ int INA228::setMaxCurrentShunt(float maxCurrent, float shunt)
347353
// PAGE 31 (8.1.2)
348354
float shunt_cal = 13107.2e6 * _current_LSB * _shunt;
349355
// depends on ADCRANGE in INA228_CONFIG register.
350-
if (getADCRange() == 1)
356+
if (_ADCRange == true)
351357
{
352358
shunt_cal *= 4;
353359
}
@@ -373,6 +379,7 @@ float INA228::getCurrentLSB()
373379
return _current_LSB;
374380
}
375381

382+
376383
////////////////////////////////////////////////////////
377384
//
378385
// SHUNT TEMPERATURE COEFFICIENT REGISTER 3
@@ -391,7 +398,6 @@ uint16_t INA228::getShuntTemperatureCoefficent()
391398
}
392399

393400

394-
395401
////////////////////////////////////////////////////////
396402
//
397403
// DIAGNOSE ALERT REGISTER 11
@@ -411,7 +417,7 @@ void INA228::setDiagnoseAlertBit(uint8_t bit)
411417
{
412418
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
413419
uint16_t mask = (1 << bit);
414-
// only write new value if needed.
420+
// only write new value if bit not set
415421
if ((value & mask) == 0)
416422
{
417423
value |= mask;
@@ -423,7 +429,7 @@ void INA228::clearDiagnoseAlertBit(uint8_t bit)
423429
{
424430
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
425431
uint16_t mask = (1 << bit);
426-
// only write new value if needed.
432+
// only write new value if bit not set.
427433
if ((value & mask ) != 0)
428434
{
429435
value &= ~mask;
@@ -471,26 +477,26 @@ uint16_t INA228::getShuntUndervoltageTH()
471477
void INA228::setBusOvervoltageTH(uint16_t threshold)
472478
{
473479
if (threshold > 0x7FFF) return;
474-
//float LSB = 3.125e-3;
480+
//float LSB = 3.125e-3; // 3.125 mV/LSB.
475481
_writeRegister(INA228_BOVL, threshold);
476482
}
477483

478484
uint16_t INA228::getBusOvervoltageTH()
479485
{
480-
//float LSB = 3.125e-3;
486+
//float LSB = 3.125e-3; // 3.125 mV/LSB.
481487
return _readRegister(INA228_BOVL, 2);
482488
}
483489

484490
void INA228::setBusUndervoltageTH(uint16_t threshold)
485491
{
486492
if (threshold > 0x7FFF) return;
487-
//float LSB = 3.125e-3;
493+
//float LSB = 3.125e-3; // 3.125 mV/LSB.
488494
_writeRegister(INA228_BUVL, threshold);
489495
}
490496

491497
uint16_t INA228::getBusUndervoltageTH()
492498
{
493-
//float LSB = 3.125e-3;
499+
//float LSB = 3.125e-3; // 3.125 mV/LSB.
494500
return _readRegister(INA228_BUVL, 2);
495501
}
496502

libraries/INA228/INA228.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22
// FILE: INA228.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.4
4+
// VERSION: 0.1.5
55
// DATE: 2024-05-09
6-
// PURPOSE: Arduino library for INA228 voltage, current and power sensor.
6+
// PURPOSE: Arduino library for the INA228, I2C, 20 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA228
88
// https://www.adafruit.com/product/5832 ( 10 A version)
99
// https://www.mateksys.com/?portfolio=i2c-ina-bm (200 A version))
@@ -16,7 +16,7 @@
1616
#include "Wire.h"
1717

1818

19-
#define INA228_LIB_VERSION (F("0.1.4"))
19+
#define INA228_LIB_VERSION (F("0.1.5"))
2020

2121

2222
// for setMode() and getMode()
@@ -253,6 +253,7 @@ class INA228
253253
float _current_LSB;
254254
float _shunt;
255255
float _maxCurrent;
256+
bool _ADCRange;
256257

257258
uint8_t _address;
258259
TwoWire * _wire;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
23+
https://www.ti.com/amplifier-circuit/current-sense/digital-power-monitors/products.html
24+

libraries/INA228/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) 2024-2024 Rob Tillaart
3+
Copyright (c) 2024-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/INA228/README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Most important difference is that the INA228 has a 20 bit ADC.
2626
This should result in higher precision however this is expected to only
2727
be visible with stable loads and low noise.
2828

29-
Another important difference is that the INA228 works up to 85 Volts,
29+
An important difference is that the INA228 works up to 85 Volts,
3030
which is more than twice the 36 volt of the INA226.
3131
The INA228 has a build in temperature sensor (±1°C) to be used for
3232
monitoring and temperature compensation.
@@ -35,9 +35,10 @@ Finally the INA228 has an **energy** and **charge** register.
3535
These are values accumulated over time, and only work in continuous mode.
3636
(to be investigated what those mean ).
3737

38-
The INA228 also provides an alert line, to generate an interrupt
38+
The INA228 also provides an ALERT line, to generate an interrupt
3939
in case a predefined threshold has been met.
4040
This can be an under- or over-voltage, temperature or power limit.
41+
The library does not handle these interrupts.
4142

4243
The library is limited tested and verified with hardware.
4344

@@ -141,10 +142,13 @@ _Tested_
141142
- https://www.ti.com/product/INA228#tech-docs
142143
- https://www.ti.com/product/INA228#params
143144
- https://www.ti.com/document-viewer/INA228/datasheet
144-
- https://github.com/RobTillaart/INA219
145-
- https://github.com/RobTillaart/INA226
146-
- https://github.com/RobTillaart/INA228
147-
- https://github.com/RobTillaart/INA3221_RT
145+
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
146+
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
147+
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
148+
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
149+
- https://github.com/RobTillaart/INA229 85 Volt, SPI, 20 bit
150+
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
151+
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
148152
- https://www.adafruit.com/product/5832
149153
- https://www.mateksys.com/?portfolio=i2c-ina-bm
150154
- https://github.com/RobTillaart/printHelpers (for scientific notation)
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
BOARD: Arduino UNO
2+
IDE: 1.8.19
3+
4+
5+
INA228_performance\INA228_performance.ino
6+
7+
INA228_LIB_VERSION: 0.1.4
8+
9+
========================================================
10+
Speed: 100000
11+
12+
test_core
13+
getBusVoltage: 688
14+
getShuntVoltage: 1244
15+
getCurrent: 684
16+
getPower: 680
17+
getTemperature: 588
18+
getEnergy: 936
19+
getCharge: 956
20+
21+
========================================================
22+
Speed: 200000
23+
24+
test_core
25+
getBusVoltage: 396
26+
getShuntVoltage: 720
27+
getCurrent: 400
28+
getPower: 392
29+
getTemperature: 344
30+
getEnergy: 556
31+
getCharge: 584
32+
33+
========================================================
34+
Speed: 400000
35+
36+
test_core
37+
getBusVoltage: 256
38+
getShuntVoltage: 456
39+
getCurrent: 252
40+
getPower: 256
41+
getTemperature: 216
42+
getEnergy: 364
43+
getCharge: 392
44+
45+
========================================================
46+
Speed: 800000
47+
48+
test_core
49+
getBusVoltage: 184
50+
getShuntVoltage: 328
51+
getCurrent: 184
52+
getPower: 180
53+
getTemperature: 160
54+
getEnergy: 276
55+
getCharge: 300
56+
57+
Done
58+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
BOARD: Arduino UNO
2+
IDE: 1.8.19
3+
4+
5+
INA228_performance\INA228_performance.ino
6+
7+
INA228_LIB_VERSION: 0.1.5
8+
9+
========================================================
10+
Speed: 100000
11+
12+
test_core
13+
getBusVoltage: 680
14+
getShuntVoltage: 684
15+
getCurrent: 688
16+
getPower: 680
17+
getTemperature: 580
18+
getEnergy: 932
19+
getCharge: 956
20+
21+
========================================================
22+
Speed: 200000
23+
24+
test_core
25+
getBusVoltage: 396
26+
getShuntVoltage: 396
27+
getCurrent: 396
28+
getPower: 396
29+
getTemperature: 348
30+
getEnergy: 556
31+
getCharge: 592
32+
33+
========================================================
34+
Speed: 400000
35+
36+
test_core
37+
getBusVoltage: 248
38+
getShuntVoltage: 256
39+
getCurrent: 256
40+
getPower: 256
41+
getTemperature: 220
42+
getEnergy: 364
43+
getCharge: 400
44+
45+
========================================================
46+
Speed: 800000
47+
48+
test_core
49+
getBusVoltage: 180
50+
getShuntVoltage: 188
51+
getCurrent: 188
52+
getPower: 184
53+
getTemperature: 160
54+
getEnergy: 276
55+
getCharge: 308
56+
57+
Done
58+

0 commit comments

Comments
 (0)