Skip to content

Commit c193303

Browse files
committed
0.4.1 INA3221_RT
1 parent 8aaa515 commit c193303

File tree

15 files changed

+106
-18
lines changed

15 files changed

+106
-18
lines changed

libraries/INA3221_RT/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.4.1] - 2025-03-06
10+
- add **getLastError()** low level (I2C) error handling
11+
- update readme.md
12+
913
## [0.4.0] - 2024-10-27
1014
- fix #8, config functions, kudos to joachymus
1115
- update readme.md

libraries/INA3221_RT/INA3221.cpp

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA3221.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.4.0
3+
// VERSION: 0.4.1
44
// DATE: 2024-02-05
55
// PURPOSE: Arduino library for the I2C INA3221 3 channel voltage and current sensor.
66
// URL: https://github.com/RobTillaart/INA3221_RT
@@ -49,6 +49,7 @@ INA3221::INA3221(const uint8_t address, TwoWire *wire)
4949
_shunt[0] = 0.1; // Ohm
5050
_shunt[1] = 0.1; // Ohm
5151
_shunt[2] = 0.1; // Ohm
52+
_error = 0;
5253
}
5354

5455
bool INA3221::begin()
@@ -67,7 +68,7 @@ bool INA3221::isConnected()
6768
uint8_t INA3221::getAddress()
6869
{
6970
return _address;
70-
};
71+
}
7172

7273

7374
////////////////////////////////////////////////////////
@@ -384,6 +385,18 @@ uint16_t INA3221::getDieID()
384385
}
385386

386387

388+
////////////////////////////////////////////////////////
389+
//
390+
// ERROR HANDLING
391+
//
392+
int INA3221::getLastError()
393+
{
394+
int e = _error;
395+
_error = 0;
396+
return e;
397+
}
398+
399+
387400
////////////////////////////////////////////////////////
388401
//
389402
// PRIVATE
@@ -392,12 +405,25 @@ uint16_t INA3221::_readRegister(uint8_t reg)
392405
{
393406
_wire->beginTransmission(_address);
394407
_wire->write(reg);
395-
_wire->endTransmission();
396-
397-
_wire->requestFrom(_address, (uint8_t)2);
398-
uint16_t value = _wire->read();
399-
value <<= 8;
400-
value |= _wire->read();
408+
int n = _wire->endTransmission();
409+
if (n != 0)
410+
{
411+
_error = -1;
412+
return 0;
413+
}
414+
415+
uint16_t value = 0;
416+
if (2 == _wire->requestFrom(_address, (uint8_t)2))
417+
{
418+
value = _wire->read();
419+
value <<= 8;
420+
value |= _wire->read();
421+
}
422+
else
423+
{
424+
_error = -2;
425+
return 0;
426+
}
401427
return value;
402428
}
403429

@@ -408,7 +434,12 @@ uint16_t INA3221::_writeRegister(uint8_t reg, uint16_t value)
408434
_wire->write(reg);
409435
_wire->write(value >> 8);
410436
_wire->write(value & 0xFF);
411-
return _wire->endTransmission();
437+
int n = _wire->endTransmission();
438+
if (n != 0)
439+
{
440+
_error = -1;
441+
}
442+
return n;
412443
}
413444

414445

libraries/INA3221_RT/INA3221.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA3221.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.4.0
4+
// VERSION: 0.4.1
55
// DATE: 2024-02-05
66
// PURPOSE: Arduino library for the I2C INA3221 3 channel voltage and current sensor.
77
// URL: https://github.com/RobTillaart/INA3221_RT
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define INA3221_LIB_VERSION (F("0.4.0"))
16+
#define INA3221_LIB_VERSION (F("0.4.1"))
1717

1818

1919
class INA3221
@@ -127,6 +127,12 @@ class INA3221
127127
};
128128

129129

130+
//
131+
// ERROR HANDLING
132+
//
133+
int getLastError();
134+
135+
130136
private:
131137

132138
uint16_t _readRegister(uint8_t reg);
@@ -135,6 +141,8 @@ class INA3221
135141

136142
uint8_t _address;
137143
TwoWire * _wire;
144+
145+
int _error;
138146
};
139147

140148

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 | 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 | - | - | - | - |
21+
22+
23+
https://www.ti.com/amplifier-circuit/current-sense/digital-power-monitors/products.html
24+

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,19 @@ As always feedback is welcome, please open an issue on GitHub.
8080

8181
### Related
8282

83-
- https://github.com/RobTillaart/INA219
84-
- https://github.com/RobTillaart/INA226
85-
- https://github.com/RobTillaart/INA228
86-
- https://github.com/RobTillaart/INA3221_RT
83+
- https://www.ti.com/product/INA3221#tech-docs
84+
- https://www.ti.com/product/INA3221#params
85+
- https://www.ti.com/document-viewer/INA3221/datasheet
86+
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
87+
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
88+
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
89+
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
90+
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
91+
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
92+
- https://www.adafruit.com/product/5832
93+
- https://www.mateksys.com/?portfolio=i2c-ina-bm
94+
- https://github.com/RobTillaart/printHelpers (for scientific notation)
95+
8796

8897

8998
## I2C
@@ -413,6 +422,11 @@ If your device returns other ManufacturerID or DieID, please let me know.
413422
- **uint16_t putRegister(uint8_t reg, uint16_t value)** load registers directly, for debugging only.
414423

415424

425+
### Error Handling
426+
427+
- **int getLastError()** returns last (I2C) error.
428+
429+
416430
## Future
417431

418432

libraries/INA3221_RT/examples/INA3221_array/INA3221_array.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ INA3221 INA[INADEVICES]
2020

2121
void setup()
2222
{
23+
while(!Serial);
2324
Serial.begin(115200);
2425
Serial.println(__FILE__);
2526
Serial.print("INA3221_LIB_VERSION: ");

libraries/INA3221_RT/examples/INA3221_demo/INA3221_demo.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ INA3221 INA(0x40);
1313

1414
void setup()
1515
{
16+
while(!Serial);
1617
Serial.begin(115200);
1718
Serial.println(__FILE__);
1819
Serial.print("INA3221_LIB_VERSION: ");

libraries/INA3221_RT/examples/INA3221_performance/INA3221_performance.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ uint32_t start, stop;
1313

1414
void setup()
1515
{
16+
while(!Serial);
1617
Serial.begin(115200);
1718
Serial.println(__FILE__);
1819
Serial.print("INA3221_LIB_VERSION: ");

libraries/INA3221_RT/examples/INA3221_test_BCVT/INA3221_test_BCVT.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ uint32_t start, stop;
1414

1515
void setup()
1616
{
17+
while(!Serial);
1718
Serial.begin(115200);
1819
Serial.println(__FILE__);
1920
Serial.print("INA3221_LIB_VERSION: ");

0 commit comments

Comments
 (0)