Skip to content

Commit 4d0d47f

Browse files
authored
Fix getShuntVoltage() + add getMaxShuntVoltage() (#21)
- fix **float getShuntVoltage()** for negative values, kudos to aguilerabr - add **int getMaxShuntVoltage()**, depends on GAIN (Table 7). - removed default for **setGain()** as it was not sensors default. - update readme.md
1 parent 42985d1 commit 4d0d47f

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ 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.0] - 2024-08-14
10+
- fix **float getShuntVoltage()** for negative values, kudos to aguilerabr
11+
- add **int getMaxShuntVoltage()**, depends on GAIN (Table 7).
12+
- removed default for **setGain()** as it was not sensors default.
13+
- update readme.md
14+
15+
----
16+
917
## [0.3.1] - 2024-04-22
1018
- Bump version after Fix #17, Kudos to ChrisRed255
1119

INA219.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA219.h
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.3.1
3+
// VERSION: 0.4.0
44
// DATE: 2021-05-18
55
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
66
// URL: https://github.com/RobTillaart/INA219
@@ -167,6 +167,7 @@ bool INA219::setGain(uint8_t factor)
167167
}
168168
uint16_t config = _readRegister(INA219_CONFIGURATION);
169169
config &= ~INA219_CONF_PROG_GAIN;
170+
// factor == 1 ==> mask = 00
170171
if (factor == 2) config |= (1 << 11);
171172
else if (factor == 4) config |= (2 << 11);
172173
else if (factor == 8) config |= (3 << 11);
@@ -187,6 +188,13 @@ uint8_t INA219::getGain()
187188
}
188189

189190

191+
int INA219::getMaxShuntVoltage()
192+
{
193+
int gain = getGain(); // 1, 2, 4, 8
194+
return gain * 40; // 40, 80, 160, 320
195+
}
196+
197+
190198
////////////////////////////////////////////////////////
191199
//
192200
// BUS

INA219.h

Lines changed: 9 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.3.1
4+
// VERSION: 0.4.0
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.3.1"))
16+
#define INA219_LIB_VERSION (F("0.4.0"))
1717

1818

1919
class INA219
@@ -54,9 +54,14 @@ class INA219
5454
// voltage = 16, 32 (values below 32 are rounded to 16 or 32)
5555
bool setBusVoltageRange(uint8_t voltage = 16);
5656
uint8_t getBusVoltageRange(); // returns 16 or 32.
57-
// factor = 1, 2, 4, 8
58-
bool setGain(uint8_t factor = 1);
57+
// factor = 1, 2, 4, 8 (8 = sensor default)
58+
bool setGain(uint8_t factor); // removed default parameter.
5959
uint8_t getGain();
60+
// MaxShuntVoltagedepends on GAIN,
61+
// See Table 7. Shunt Voltage Register Format
62+
// default = 320.
63+
int getMaxShuntVoltage();
64+
6065

6166
// configuration BUS
6267
// use one of the next three

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ A few important maxima, see datasheet, chapter 7, esp 7.5
3333

3434

3535

36+
#### 0.4.0 Breaking change
37+
38+
Version 0.4.0 fixed negative values for **getShuntVoltage()**.
39+
Older versions are obsolete now.
40+
41+
3642
#### 0.2.0 Breaking change
3743

3844
Version 0.2.0 introduced a breaking change.
@@ -146,14 +152,16 @@ See section below.
146152
Returns false if it could not write settings to device.
147153
- **bool setBusVoltageRange(uint8_t voltage = 16)** set to 16 or 32.
148154
Values <= 16 map to 16 and values between 16 and 32 map to 32.
149-
Returns false if voltage is above 32..
155+
Returns false if voltage is above 32.
150156
Returns false if it could not write settings to device.
151157
- **uint8_t getBusVoltageRange()** returns 16 or 32. (Volts)
152-
- **bool setGain(uint8_t factor = 1)** factor = 1, 2, 4, 8.
158+
- **bool setGain(uint8_t factor)** factor = 1, 2, 4, 8 (default).
153159
Determines the shunt voltage range. 40, 80, 160 or 320 mV.
154160
Returns false if factor is not a valid value.
155161
Returns false if it could not write settings to device.
156162
- **uint8_t getGain()** returns set factor.
163+
- **int getMaxShuntVoltage()** returns 40, 80, 160 or 320 (mV).
164+
320 is the sensors default.
157165

158166

159167
#### Configuration BUS and SHUNT

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ setBusVoltageRange KEYWORD2
3232
getBusVoltageRange KEYWORD2
3333
setGain KEYWORD2
3434
getGain KEYWORD2
35+
getMaxShuntVoltage KEYWORD2
3536

3637
setBusResolution KEYWORD2
3738
setBusSamples KEYWORD2

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.3.1",
18+
"version": "0.4.0",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

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.3.1
2+
version=0.4.0
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)