Skip to content

Commit b60cc55

Browse files
committed
0.1.1 INA239
1 parent c4828d0 commit b60cc55

File tree

11 files changed

+113
-32
lines changed

11 files changed

+113
-32
lines changed

libraries/INA239/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ 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.1] - 2025-01-30
10+
- fix #4, cache ADCrange to improve **getShuntVoltage()**
11+
- add INA_comparison_table.md
12+
- minor edits
13+
14+
915
## [0.1.0] - 2024-12-05
1016
- initial version, based upon INA228 stripped
1117

libraries/INA239/INA239.cpp

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA239.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.1.0
3+
// VERSION: 0.1.1
44
// DATE: 2024-12-05
55
// PURPOSE: Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
66
// URL: https://github.com/RobTillaart/INA239
@@ -99,6 +99,8 @@ bool INA239::begin()
9999
digitalWrite(_dataOut, LOW);
100100
digitalWrite(_clock, LOW);
101101
}
102+
103+
getADCRange();
102104
return true;
103105
}
104106

@@ -107,7 +109,7 @@ bool INA239::begin()
107109
//
108110
// CORE FUNCTIONS
109111
//
110-
// PAGE 22 DONE
112+
// PAGE 22
111113
float INA239::getBusVoltage()
112114
{
113115
// always positive, remove reserved bits.
@@ -117,12 +119,12 @@ float INA239::getBusVoltage()
117119
return voltage;
118120
}
119121

120-
// PAGE 22 DONE
122+
// PAGE 22
121123
float INA239::getShuntVoltage()
122124
{
123125
// shunt_LSB depends on ADCRANGE in INA239_CONFIG register.
124126
float shunt_LSB = 5e-6; // 5.0 uV/LSB
125-
if (getADCRange() == 1)
127+
if (_ADCRange == true)
126128
{
127129
shunt_LSB = 1.25e-6; // 1.25 uV/LSB
128130
}
@@ -138,15 +140,15 @@ float INA239::getShuntVoltage()
138140
return voltage;
139141
}
140142

141-
// PAGE 23 + 8.1.2 DONE
143+
// PAGE 23 + 8.1.2
142144
float INA239::getCurrent()
143145
{
144146
int16_t value = _readRegister(INA239_CURRENT, 2);
145147
float current = value * _current_LSB;
146148
return current;
147149
}
148150

149-
// PAGE 23 + 8.1.2 DONE
151+
// PAGE 23 + 8.1.2
150152
float INA239::getPower()
151153
{
152154
uint32_t value = _readRegister(INA239_POWER, 3);
@@ -167,15 +169,15 @@ float INA239::getTemperature()
167169
//
168170
// CONFIG REGISTER 0
169171
//
170-
// PAGE 20 DONE
172+
// PAGE 20
171173
void INA239::reset()
172174
{
173175
uint16_t value = _readRegister(INA239_CONFIG, 2);
174176
value |= INA239_CFG_RST;
175177
_writeRegister(INA239_CONFIG, value);
176178
}
177179

178-
// PAGE 20 DONE
180+
// PAGE 20
179181
void INA239::setConversionDelay(uint8_t steps)
180182
{
181183
uint16_t value = _readRegister(INA239_CONFIG, 2);
@@ -190,27 +192,31 @@ uint8_t INA239::getConversionDelay()
190192
return (value >> 6) & 0xFF;
191193
}
192194

193-
// PAGE 20 DONE
195+
// PAGE 20
194196
void INA239::setADCRange(bool flag)
195197
{
198+
// if (flag == _ADCRange) return;
199+
_ADCRange = flag;
196200
uint16_t value = _readRegister(INA239_CONFIG, 2);
197-
value &= ~INA239_CFG_ADCRANGE;
198201
if (flag) value |= INA239_CFG_ADCRANGE;
202+
else value &= ~INA239_CFG_ADCRANGE;
203+
// if value has not changed we do not need to write it back.
199204
_writeRegister(INA239_CONFIG, value);
200205
}
201206

202207
bool INA239::getADCRange()
203208
{
204209
uint16_t value = _readRegister(INA239_CONFIG, 2);
205-
return (value & INA239_CFG_ADCRANGE) > 0;
210+
_ADCRange = (value & INA239_CFG_ADCRANGE) > 0;
211+
return _ADCRange;
206212
}
207213

208214

209215
////////////////////////////////////////////////////////
210216
//
211217
// CONFIG ADC REGISTER 1
212218
//
213-
// PAGE 21 + 22 DONE
219+
// PAGE 21 + 22
214220
bool INA239::setMode(uint8_t mode)
215221
{
216222
if (mode > 0x0F) return false;
@@ -296,19 +302,19 @@ uint8_t INA239::getAverage()
296302
//
297303
// SHUNT CALIBRATION REGISTER 2
298304
//
299-
// PAGE 28 8.1.2
305+
// PAGE 28 + 8.1.2
300306
int INA239::setMaxCurrentShunt(float maxCurrent, float shunt)
301307
{
302308
// Shunt can be really small
303-
if (shunt < 0.0001) return -2; // error code
309+
if (shunt < 0.0001) return -2; // TODO error code
304310
_maxCurrent = maxCurrent;
305311
_shunt = shunt;
306312
_current_LSB = _maxCurrent * 3.0517578125e-5; // pow(2, -15);
307313

308314
// PAGE 31 (8.1.2)
309315
float shunt_cal = 819.2e6 * _current_LSB * _shunt;
310316
// depends on ADCRANGE in INA239_CONFIG register.
311-
if (getADCRange() == 1)
317+
if (_ADCRange == true)
312318
{
313319
shunt_cal *= 4;
314320
}
@@ -339,7 +345,7 @@ float INA239::getCurrentLSB()
339345
//
340346
// DIAGNOSE ALERT REGISTER 11
341347
//
342-
// PAGE 23 DONE
348+
// PAGE 23
343349
void INA239::setDiagnoseAlert(uint16_t flags)
344350
{
345351
_writeRegister(INA239_DIAG_ALERT, flags);
@@ -452,14 +458,14 @@ uint16_t INA239::getTemperatureOverLimitTH()
452458

453459
void INA239::setPowerOverLimitTH(uint16_t threshold)
454460
{
455-
// P 26
461+
// PAGE 26
456462
// Conversion factor: 256 × Power LSB.
457463
_writeRegister(INA239_POWER_LIMIT, threshold);
458464
}
459465

460466
uint16_t INA239::getPowerOverLimitTH()
461467
{
462-
// P 26
468+
// PAGE 26
463469
// Conversion factor: 256 × Power LSB.
464470
return _readRegister(INA239_POWER_LIMIT, 2);
465471
}
@@ -469,6 +475,7 @@ uint16_t INA239::getPowerOverLimitTH()
469475
//
470476
// MANUFACTURER and ID REGISTER 3E/3F
471477
//
478+
// PAGE 26
472479
uint16_t INA239::getManufacturer()
473480
{
474481
uint16_t value = _readRegister(INA239_MANUFACTURER, 2);
@@ -514,7 +521,7 @@ bool INA239::usesHWSPI()
514521

515522
////////////////////////////////////////////////////////
516523
//
517-
// SHOULD BE PROTECTED
524+
// PRIVATE
518525
//
519526
uint32_t INA239::_readRegister(uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
520527
{
@@ -576,11 +583,6 @@ uint16_t INA239::_writeRegister(uint8_t reg, uint16_t value)
576583
}
577584

578585

579-
/////////////////////////////////////////////////////////////////////////////
580-
//
581-
// PROTECTED
582-
//
583-
584586
uint8_t INA239::swSPI_transfer(uint8_t value)
585587
{
586588
uint8_t rv = 0;

libraries/INA239/INA239.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA239.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2024-12-05
66
// PURPOSE: Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA239
@@ -13,7 +13,7 @@
1313
#include "SPI.h"
1414

1515

16-
#define INA239_LIB_VERSION (F("0.1.0"))
16+
#define INA239_LIB_VERSION (F("0.1.1"))
1717

1818

1919
#ifndef __SPI_CLASS__
@@ -231,14 +231,15 @@ class INA239
231231
bool usesHWSPI();
232232

233233

234-
protected:
234+
private:
235235
// max 4 bytes
236236
uint32_t _readRegister(uint8_t reg, uint8_t bytes);
237237
uint16_t _writeRegister(uint8_t reg, uint16_t value);
238238

239239
float _current_LSB;
240240
float _shunt;
241241
float _maxCurrent;
242+
bool _ADCRange;
242243

243244
uint8_t _dataIn = 255;
244245
uint8_t _dataOut= 255;
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/INA239/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/INA239/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ works up to 85 Volts, which is more than twice the 36 volt of the INA226.
3333
The INA239 has a build in temperature sensor (±1°C) to be used for
3434
monitoring and temperature compensation.
3535

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

libraries/INA239/examples/INA239_demo/INA239_demo.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "INA239.h"
99

1010
// select, dataIn, dataOut, clock == SOFTWARE SPI
11-
// INA239 INA(5, 6, 7, &SPI);
11+
// INA239 INA(5, 6, 7, 8);
1212

1313
// select, &SPI === HW SPI
1414
INA239 INA(5, &SPI);

libraries/INA239/examples/INA239_performance/performance_0.1.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ BOARD: Arduino UNO
22
IDE: 1.8.19
33

44

5+
INA239_LIB_VERSION: 0.1.0
6+
57

68
========================================================
79
Speed: 1000000
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
BOARD: Arduino UNO
2+
IDE: 1.8.19
3+
4+
5+
INA239_LIB_VERSION: 0.1.1
6+
7+
8+
========================================================
9+
Speed: 1000000
10+
11+
test_core
12+
getBusVoltage: 60
13+
getShuntVoltage: 60
14+
getCurrent: 56
15+
getPower: 80
16+
getTemperature: 56
17+
18+
========================================================
19+
Speed: 2000000
20+
21+
test_core
22+
getBusVoltage: 48
23+
getShuntVoltage: 48
24+
getCurrent: 40
25+
getPower: 64
26+
getTemperature: 48
27+
28+
========================================================
29+
Speed: 4000000
30+
31+
test_core
32+
getBusVoltage: 44
33+
getShuntVoltage: 44
34+
getCurrent: 40
35+
getPower: 64
36+
getTemperature: 44
37+
38+
========================================================
39+
Speed: 8000000
40+
41+
test_core
42+
getBusVoltage: 44
43+
getShuntVoltage: 40
44+
getCurrent: 32
45+
getPower: 52
46+
getTemperature: 40
47+
48+
Done

libraries/INA239/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/INA239.git"
1717
},
18-
"version": "0.1.0",
18+
"version": "0.1.1",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/INA239/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA239
2-
version=0.1.0
2+
version=0.1.1
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.

0 commit comments

Comments
 (0)