Skip to content

Commit 20ce3a9

Browse files
committed
0.4.1 INA219
1 parent 488d1f3 commit 20ce3a9

File tree

8 files changed

+92
-16
lines changed

8 files changed

+92
-16
lines changed

libraries/INA219/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.4.1] - 2025-03-05
10+
- fix #23, add **getLastError()** low level (I2C) error handling
11+
- add INA_comparison_table.md
12+
13+
914
## [0.4.0] - 2024-08-14
1015
- fix **float getShuntVoltage()** for negative values, kudos to aguilerabr
1116
- add **int getMaxShuntVoltage()**, depends on GAIN (Table 7).

libraries/INA219/INA219.cpp

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA219.h
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.4.0
3+
// VERSION: 0.4.1
44
// DATE: 2021-05-18
55
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
66
// URL: https://github.com/RobTillaart/INA219
@@ -44,6 +44,7 @@ INA219::INA219(const uint8_t address, TwoWire *wire)
4444
_current_LSB = 0;
4545
_maxCurrent = 0;
4646
_shunt = 0;
47+
_error = 0;
4748
}
4849

4950

@@ -365,6 +366,18 @@ bool INA219::setMaxCurrentShunt(float maxCurrent, float shunt)
365366
}
366367

367368

369+
////////////////////////////////////////////////////////
370+
//
371+
// ERROR HANDLING
372+
//
373+
int INA219::getLastError()
374+
{
375+
int e = _error;
376+
_error = 0;
377+
return e;
378+
}
379+
380+
368381
////////////////////////////////////////////////////////
369382
//
370383
// PRIVATE
@@ -374,12 +387,25 @@ uint16_t INA219::_readRegister(uint8_t reg)
374387
{
375388
_wire->beginTransmission(_address);
376389
_wire->write(reg);
377-
_wire->endTransmission();
390+
int n = _wire->endTransmission();
391+
if (n != 0)
392+
{
393+
_error = -1;
394+
return 0;
395+
}
378396

379-
_wire->requestFrom(_address, (uint8_t)2);
380-
uint16_t value = _wire->read();
381-
value <<= 8;
382-
value |= _wire->read();
397+
uint16_t value = 0;
398+
if (2 == _wire->requestFrom(_address, (uint8_t)2))
399+
{
400+
value = _wire->read();
401+
value <<= 8;
402+
value |= _wire->read();
403+
}
404+
else
405+
{
406+
_error = -2;
407+
return 0;
408+
}
383409
return value;
384410
}
385411

@@ -390,7 +416,12 @@ uint16_t INA219::_writeRegister(uint8_t reg, uint16_t value)
390416
_wire->write(reg);
391417
_wire->write(value >> 8);
392418
_wire->write(value & 0xFF);
393-
return _wire->endTransmission();
419+
int n = _wire->endTransmission();
420+
if (n != 0)
421+
{
422+
_error = -1;
423+
}
424+
return n;
394425
}
395426

396427

libraries/INA219/INA219.h

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

1515

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

1818

1919
class INA219
@@ -57,11 +57,11 @@ class INA219
5757
// factor = 1, 2, 4, 8 (8 = sensor default)
5858
bool setGain(uint8_t factor); // removed default parameter.
5959
uint8_t getGain();
60-
// MaxShuntVoltagedepends on GAIN,
60+
// MaxShuntVoltagedepends on GAIN,
6161
// See Table 7. Shunt Voltage Register Format
6262
// default = 320.
6363
int getMaxShuntVoltage();
64-
64+
6565

6666
// configuration BUS
6767
// use one of the next three
@@ -110,17 +110,24 @@ class INA219
110110
// DEBUG
111111
uint16_t getRegister(uint8_t reg) { return _readRegister(reg); };
112112

113+
//
114+
// ERROR HANDLING
115+
//
116+
int getLastError();
113117

114118
private:
115119

116120
uint16_t _readRegister(uint8_t reg);
117121
uint16_t _writeRegister(uint8_t reg, uint16_t value);
122+
118123
float _current_LSB;
119124
float _shunt;
120125
float _maxCurrent;
121126

122127
uint8_t _address;
123128
TwoWire * _wire;
129+
130+
int _error;
124131
};
125132

126133

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,17 @@ before calling **begin()**.
5959
- https://www.ti.com/product/INA219#tech-docs
6060
- https://www.ti.com/product/INA219#params
6161
- https://www.ti.com/document-viewer/INA219/datasheet
62-
- https://github.com/RobTillaart/INA219
63-
- https://github.com/RobTillaart/INA226
62+
- https://github.com/RobTillaart/INA219 26 Volt, I2C, 12 bit
63+
- https://github.com/RobTillaart/INA226 36 Volt, I2C, 16 bit
64+
- https://github.com/RobTillaart/INA228 85 Volt, I2C, 20 bit
65+
- https://github.com/RobTillaart/INA236 48 Volt, I2C, 16 bit
66+
- https://github.com/RobTillaart/INA239 85 Volt, SPI, 16 bit
67+
- https://github.com/RobTillaart/INA260 36 Volt, SPI, 16 bit
68+
- https://github.com/RobTillaart/INA3221_RT 26 Volt, I2C, 13 bits (3 channel)
69+
- https://www.adafruit.com/product/5832
70+
- https://www.mateksys.com/?portfolio=i2c-ina-bm
71+
- https://github.com/RobTillaart/printHelpers (for scientific notation)
72+
6473

6574

6675
## I2C

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

libraries/INA219/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=INA219
2-
version=0.4.0
2+
version=0.4.1
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Arduino library for INA219 voltage, current and power sensor.

0 commit comments

Comments
 (0)