Skip to content

Commit

Permalink
Fix getShuntVoltage() + add getMaxShuntVoltage() (#21)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
RobTillaart authored Aug 14, 2024
1 parent 42985d1 commit 4d0d47f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 9 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.4.0] - 2024-08-14
- 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

----

## [0.3.1] - 2024-04-22
- Bump version after Fix #17, Kudos to ChrisRed255

Expand Down
10 changes: 9 additions & 1 deletion INA219.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// VERSION: 0.4.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
Expand Down Expand Up @@ -167,6 +167,7 @@ bool INA219::setGain(uint8_t factor)
}
uint16_t config = _readRegister(INA219_CONFIGURATION);
config &= ~INA219_CONF_PROG_GAIN;
// factor == 1 ==> mask = 00
if (factor == 2) config |= (1 << 11);
else if (factor == 4) config |= (2 << 11);
else if (factor == 8) config |= (3 << 11);
Expand All @@ -187,6 +188,13 @@ uint8_t INA219::getGain()
}


int INA219::getMaxShuntVoltage()
{
int gain = getGain(); // 1, 2, 4, 8
return gain * 40; // 40, 80, 160, 320
}


////////////////////////////////////////////////////////
//
// BUS
Expand Down
13 changes: 9 additions & 4 deletions INA219.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once
// FILE: INA219.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.1
// VERSION: 0.4.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA219 voltage, current and power sensor
// URL: https://github.com/RobTillaart/INA219
Expand All @@ -13,7 +13,7 @@
#include "Wire.h"


#define INA219_LIB_VERSION (F("0.3.1"))
#define INA219_LIB_VERSION (F("0.4.0"))


class INA219
Expand Down Expand Up @@ -54,9 +54,14 @@ class INA219
// voltage = 16, 32 (values below 32 are rounded to 16 or 32)
bool setBusVoltageRange(uint8_t voltage = 16);
uint8_t getBusVoltageRange(); // returns 16 or 32.
// factor = 1, 2, 4, 8
bool setGain(uint8_t factor = 1);
// factor = 1, 2, 4, 8 (8 = sensor default)
bool setGain(uint8_t factor); // removed default parameter.
uint8_t getGain();
// MaxShuntVoltagedepends on GAIN,
// See Table 7. Shunt Voltage Register Format
// default = 320.
int getMaxShuntVoltage();


// configuration BUS
// use one of the next three
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ A few important maxima, see datasheet, chapter 7, esp 7.5



#### 0.4.0 Breaking change

Version 0.4.0 fixed negative values for **getShuntVoltage()**.
Older versions are obsolete now.


#### 0.2.0 Breaking change

Version 0.2.0 introduced a breaking change.
Expand Down Expand Up @@ -146,14 +152,16 @@ See section below.
Returns false if it could not write settings to device.
- **bool setBusVoltageRange(uint8_t voltage = 16)** set to 16 or 32.
Values <= 16 map to 16 and values between 16 and 32 map to 32.
Returns false if voltage is above 32..
Returns false if voltage is above 32.
Returns false if it could not write settings to device.
- **uint8_t getBusVoltageRange()** returns 16 or 32. (Volts)
- **bool setGain(uint8_t factor = 1)** factor = 1, 2, 4, 8.
- **bool setGain(uint8_t factor)** factor = 1, 2, 4, 8 (default).
Determines the shunt voltage range. 40, 80, 160 or 320 mV.
Returns false if factor is not a valid value.
Returns false if it could not write settings to device.
- **uint8_t getGain()** returns set factor.
- **int getMaxShuntVoltage()** returns 40, 80, 160 or 320 (mV).
320 is the sensors default.


#### Configuration BUS and SHUNT
Expand Down
1 change: 1 addition & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ setBusVoltageRange KEYWORD2
getBusVoltageRange KEYWORD2
setGain KEYWORD2
getGain KEYWORD2
getMaxShuntVoltage KEYWORD2

setBusResolution KEYWORD2
setBusSamples KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA219.git"
},
"version": "0.3.1",
"version": "0.4.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=INA219
version=0.3.1
version=0.4.0
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for INA219 voltage, current and power sensor.
Expand Down

0 comments on commit 4d0d47f

Please sign in to comment.