Skip to content

Commit d5d1c8d

Browse files
committed
0.1.2 INA236
1 parent e411ea8 commit d5d1c8d

File tree

8 files changed

+89
-40
lines changed

8 files changed

+89
-40
lines changed

libraries/INA236/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ 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.2] - 2025-05-06
10+
- add **getLastError()** low level (I2C) error handling
11+
- update readme.md
12+
913
## [0.1.1] - 2025-01-30
1014
- fix #2, cache ADCRange to improve getShuntVoltage()
1115
- add **INA_comparison_table.md**

libraries/INA236/INA236.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// FILE: INA236.cpp
22
// AUTHOR: Rob Tillaart
33
// ported from INA226 to INA236 by Josef Tremmel
4-
// VERSION: 0.1.1
4+
// VERSION: 0.1.2
55
// DATE: 2024-05-27
66
// PURPOSE: Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA236
@@ -46,6 +46,7 @@ INA236::INA236(const uint8_t address, TwoWire *wire)
4646
_current_LSB = 0;
4747
_maxCurrent = 0;
4848
_shunt = 0;
49+
_error = 0;
4950
}
5051

5152

@@ -486,6 +487,18 @@ uint16_t INA236::getDieID()
486487
}
487488

488489

490+
////////////////////////////////////////////////////////
491+
//
492+
// ERROR HANDLING
493+
//
494+
int INA236::getLastError()
495+
{
496+
int e = _error;
497+
_error = 0;
498+
return e;
499+
}
500+
501+
489502
////////////////////////////////////////////////////////
490503
//
491504
// PRIVATE
@@ -494,12 +507,25 @@ uint16_t INA236::_readRegister(uint8_t reg)
494507
{
495508
_wire->beginTransmission(_address);
496509
_wire->write(reg);
497-
_wire->endTransmission();
510+
int n = _wire->endTransmission();
511+
if (n != 0)
512+
{
513+
_error = -1;
514+
return 0;
515+
}
498516

499-
_wire->requestFrom(_address, (uint8_t)2);
500-
uint16_t value = _wire->read();
501-
value <<= 8;
502-
value |= _wire->read();
517+
uint16_t value = 0;
518+
if (2 == _wire->requestFrom(_address, (uint8_t)2))
519+
{
520+
value = _wire->read();
521+
value <<= 8;
522+
value |= _wire->read();
523+
}
524+
else
525+
{
526+
_error = -2;
527+
return 0;
528+
}
503529
return value;
504530
}
505531

@@ -510,7 +536,12 @@ uint16_t INA236::_writeRegister(uint8_t reg, uint16_t value)
510536
_wire->write(reg);
511537
_wire->write(value >> 8);
512538
_wire->write(value & 0xFF);
513-
return _wire->endTransmission();
539+
int n = _wire->endTransmission();
540+
if (n != 0)
541+
{
542+
_error = -1;
543+
}
544+
return n;
514545
}
515546

516547

libraries/INA236/INA236.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// FILE: INA236.h
33
// AUTHOR: Rob Tillaart
44
// ported from INA226 to INA236 by Josef Tremmel
5-
// VERSION: 0.1.1
5+
// VERSION: 0.1.2
66
// DATE: 2024-05-27
77
// PURPOSE: Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.
88
// URL: https://github.com/RobTillaart/INA236
@@ -14,7 +14,7 @@
1414
#include "Wire.h"
1515

1616

17-
#define INA236_LIB_VERSION "0.1.1"
17+
#define INA236_LIB_VERSION (F("0.1.2"))
1818

1919

2020
// set by setAlertRegister
@@ -168,6 +168,10 @@ class INA236
168168
// DEBUG
169169
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
170170

171+
//
172+
// ERROR HANDLING
173+
//
174+
int getLastError();
171175

172176
private:
173177

@@ -180,6 +184,8 @@ class INA236
180184

181185
uint8_t _address;
182186
TwoWire * _wire;
187+
188+
int _error;
183189
};
184190

185191

libraries/INA236/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/INA236/README.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ A few important maxima, see datasheet, chapter 6.
3737
| current | 20 | Ampere | ?? TODO check
3838

3939

40-
#### Special characters
40+
### Special characters
4141

4242
- Ω == Ohm = ALT-234 (Windows)
4343
- µ == micro = ALT-0181 (Windows)
4444

4545

46-
#### Related
46+
### Related
4747

4848
- https://www.ti.com/product/INA236#tech-docs
4949
- https://www.ti.com/product/INA236#params
@@ -61,7 +61,7 @@ A few important maxima, see datasheet, chapter 6.
6161

6262
## I2C
6363

64-
#### Address
64+
### Address
6565

6666
The INA236 comes in two flavours, the INA236A and INA236B
6767
with two disjunct address ranges.
@@ -78,7 +78,7 @@ See table - from datasheet table 7.1, page 16.
7878
| SCL | 67 | 0x43 | 75 | 0x4B |
7979

8080

81-
#### Performance
81+
### Performance
8282

8383
To be elaborated, example sketch available.
8484

@@ -126,7 +126,7 @@ Also see INA226 issue 30 for another typical deviation problem.
126126
```
127127

128128

129-
#### Constructor
129+
### Constructor
130130

131131
- **INA236(const uint8_t address, TwoWire \*wire = Wire)** Constructor to set
132132
the address and optional Wire interface.
@@ -137,7 +137,7 @@ Note: one needs to set **Wire.begin()** before calling **begin()**.
137137
- **uint8_t getAddress()** returns the address set in the constructor.
138138

139139

140-
#### Core Functions
140+
### Core Functions
141141

142142
Note the power and the current are not meaningful without calibrating the sensor.
143143
Also the value is not meaningful if there is no shunt connected.
@@ -168,7 +168,7 @@ Helper functions for the micro scale.
168168
- **float getPower_uW()** idem, in microWatt.
169169

170170

171-
#### Configuration
171+
### Configuration
172172

173173
**Note:**
174174
The internal conversions runs in the background in the device.
@@ -237,7 +237,7 @@ Note: times are typical, check datasheet for operational range.
237237
Note: total conversion time can take up to 1024 \* 8.3 ms ~ 10 seconds.
238238

239239

240-
#### ADCRange
240+
### ADCRange
241241

242242
The INA236 can set the ADC range to 20 mV (adcRange == true)
243243
or to 80 mV (adcRange == false) to optimize the accuracy.
@@ -249,7 +249,7 @@ The function sets the voltage/LSB and returns false adcRange is out of range.
249249
Note: this function is not available on INA226.
250250

251251

252-
#### Calibration
252+
### Calibration
253253

254254
See datasheet.
255255

@@ -271,7 +271,7 @@ Value should not be zero.
271271
To print these values in scientific notation use https://github.com/RobTillaart/printHelpers
272272

273273

274-
#### About normalization
274+
### About normalization
275275

276276
**setMaxCurrentShunt()** will round the current_LSB to nearest round value (typical 0.001) by default (normalize == true).
277277
- The user **must** check the return value == 0x000, otherwise the calibration register is **not** set.
@@ -285,7 +285,7 @@ normalize flag was set to true.
285285
See https://github.com/RobTillaart/INA236/pull/29 for details of the discussion.
286286

287287

288-
#### Error codes setMaxCurrentShunt
288+
### Error codes setMaxCurrentShunt
289289

290290
| descriptive name error | value | meaning |
291291
|:-------------------------------|:--------:|:----------|
@@ -296,7 +296,7 @@ See https://github.com/RobTillaart/INA236/pull/29 for details of the discussion.
296296
| INA236_ERR_NORMALIZE_FAILED | 0x8003 | not possible to normalize.
297297

298298

299-
#### Operating mode
299+
### Operating mode
300300

301301
See datasheet, partially tested.
302302

@@ -317,7 +317,7 @@ Descriptive mode functions (convenience wrappers).
317317
- **bool setModeShuntBusContinuous()** mode 7 - default.
318318

319319

320-
#### Alert functions
320+
### Alert functions
321321

322322
See datasheet, not tested yet.
323323

@@ -353,17 +353,22 @@ Returns true if write to register successful.
353353
The alert line falls when alert is reached.
354354

355355

356-
#### Meta information
356+
### Meta information
357357

358358
- **uint16_t getManufacturerID()** should return 0x5449.
359359
- **uint16_t getDieID()** should return 0xA080.
360360

361361

362-
#### Debugging
362+
### Debugging
363363

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

366366

367+
### Error Handling
368+
369+
- **int getLastError()** returns last (I2C) error.
370+
371+
367372
## Adjusting the range of the INA236
368373

369374
**use at own risk**

libraries/INA236/keywords.txt

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

69+
getLastError KEYWORD2
6970

7071
# Constants (LITERAL1)
7172
INA236_LIB_VERSION LITERAL1

libraries/INA236/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"type": "git",
1919
"url": "https://github.com/RobTillaart/INA236.git"
2020
},
21-
"version": "0.1.1",
21+
"version": "0.1.2",
2222
"license": "MIT",
2323
"frameworks": "*",
2424
"platforms": "*",

libraries/INA236/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA236
2-
version=0.1.1
2+
version=0.1.2
33
author=Rob Tillaart <rob.tillaart@gmail.com>, Josef Tremmel
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for the INA236, I2C, 16 bit, voltage, current and power sensor.

0 commit comments

Comments
 (0)