0.6.0 INA226

This commit is contained in:
Rob Tillaart 2024-05-28 10:00:28 +02:00
parent 4c9894669e
commit c5d841b493
7 changed files with 90 additions and 83 deletions

View File

@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.6.0] - 2024-05-27
- Fix #47, calibration register is 15 bit, not 16 bit.
- minor edits
----
## [0.5.5] - 2024-04-22
- Fix possible overflow in **getPower()**

View File

@ -1,6 +1,6 @@
// FILE: INA226.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.5
// VERSION: 0.6.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA226 power sensor
// URL: https://github.com/RobTillaart/INA226
@ -111,7 +111,7 @@ bool INA226::waitConversionReady(uint32_t timeout)
while ( (millis() - start) <= timeout)
{
if (isConversionReady()) return true;
delay(1); // implicit yield();
delay(1); // implicit yield();
}
return false;
}
@ -214,14 +214,14 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
_current_LSB = maxCurrent * 3.0517578125e-5; // maxCurrent / 32768;
#ifdef printdebug
Serial.println();
Serial.print("normalize:\t");
Serial.println(normalize ? " true":" false");
Serial.print("initial current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
#ifdef printdebug
Serial.println();
Serial.print("normalize:\t");
Serial.println(normalize ? " true" : " false");
Serial.print("initial current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
// normalize the LSB to a round number
// LSB will increase
@ -232,25 +232,27 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
(due to unusual low resistor values in relation to maxCurrent) determines currentLSB
we have to take the upper value for currentLSB
(adjusted in 0.6.0)
calculation of currentLSB based on shunt resistor and calibration register limits (2 bytes)
cal = 0.00512 / ( shunt * currentLSB )
cal(max) = 2^16-1
cal(max) = 2^15-1
currentLSB(min) = 0.00512 / ( shunt * cal(max) )
currentLSB(min) ~= 0.00512 / ( shunt * 2^16 )
currentLSB(min) ~= 2^9 * 1e-5 / ( shunt * 2^16 )
currentLSB(min) ~= 1e-5 / 2^7 / shunt
currentLSB(min) ~= 7.8125e-8 / shunt
currentLSB(min) ~= 0.00512 / ( shunt * 2^15 )
currentLSB(min) ~= 2^9 * 1e-5 / ( shunt * 2^15 )
currentLSB(min) ~= 1e-5 / 2^6 / shunt
currentLSB(min) ~= 1.5625e-7 / shunt
*/
if ( 7.8125e-8 / shunt > _current_LSB ) {
// shunt resistor determines currentLSB -> take this a starting point for currentLSB
_current_LSB = 7.8125e-8 / shunt;
if ( 1.5625e-7 / shunt > _current_LSB ) {
// shunt resistor determines current_LSB
// => take this a starting point for current_LSB
_current_LSB = 1.5625e-7 / shunt;
}
#ifdef printdebug
Serial.print("Pre-scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
#ifdef printdebug
Serial.print("Pre-scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
// normalize _current_LSB to a value of 1, 2 or 5 * 1e-6 to 1e-3
// convert float to int
@ -274,24 +276,25 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
factor *= 10;
i++;
}
} while( (i < 4) && (!result) ); // factor < 10000
} while ( (i < 4) && (!result) ); // factor < 10000
if (result == false) { // not succeeded to normalize.
if (result == false) // not succeeded to normalize.
{
_current_LSB = 0;
return INA226_ERR_NORMALIZE_FAILED;
}
#ifdef printdebug
Serial.print("After scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
#ifdef printdebug
Serial.print("After scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
#endif
// done
}
// auto scale calibration if needed.
uint32_t calib = round(0.00512 / (_current_LSB * shunt));
while (calib > 65535)
while (calib > 32767)
{
_current_LSB *= 2;
calib >>= 1;
@ -301,22 +304,22 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
_maxCurrent = _current_LSB * 32768;
_shunt = shunt;
#ifdef printdebug
Serial.print("Final current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
Serial.print("Calibration:\t");
Serial.println(calib);
Serial.print("Max current:\t");
Serial.print(_maxCurrent, 3);
Serial.println(" A");
Serial.print("Shunt:\t");
Serial.print(_shunt, 4);
Serial.println(" Ohm");
Serial.print("ShuntV:\t");
Serial.print(shuntVoltage, 4);
Serial.println(" Volt");
#endif
#ifdef printdebug
Serial.print("Final current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);
Serial.println(" uA / bit");
Serial.print("Calibration:\t");
Serial.println(calib);
Serial.print("Max current:\t");
Serial.print(_maxCurrent, 3);
Serial.println(" A");
Serial.print("Shunt:\t");
Serial.print(_shunt, 4);
Serial.println(" Ohm");
Serial.print("ShuntV:\t");
Serial.print(shuntVoltage, 4);
Serial.println(" Volt");
#endif
return INA226_ERR_NONE;
}

View File

@ -1,7 +1,7 @@
#pragma once
// FILE: INA226.h
// AUTHOR: Rob Tillaart
// VERSION: 0.5.5
// VERSION: 0.6.0
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA226 power sensor
// URL: https://github.com/RobTillaart/INA226
@ -13,7 +13,7 @@
#include "Wire.h"
#define INA226_LIB_VERSION "0.5.5"
#define INA226_LIB_VERSION "0.6.0"
// set by setAlertRegister

View File

@ -55,6 +55,7 @@ before calling **begin()**.
- https://www.ti.com/document-viewer/INA226/datasheet
- https://github.com/RobTillaart/INA219
- https://github.com/RobTillaart/INA226
- https://github.com/RobTillaart/INA236
## I2C
@ -65,26 +66,26 @@ The sensor can have 16 different I2C addresses,
which depends on how the A0 and A1 address lines
are connected to the SCL, SDA, GND and VCC pins.
See table - from datasheet table 2, page18.
See table - from datasheet table 2, page 18.
| A1 | A0 | ADDRESS |
|:-----:|:-----:|:----------:|
| GND | GND | 1000000 |
| GND | VS | 1000001 |
| GND | SDA | 1000010 |
| GND | SCL | 1000011 |
| VS | GND | 1000100 |
| VS | VS | 1000101 |
| VS | SDA | 1000110 |
| VS | SCL | 1000111 |
| SDA | GND | 1001000 |
| SDA | VS | 1001001 |
| SDA | SDA | 1001010 |
| SDA | SCL | 1001011 |
| SCL | GND | 1001100 |
| SCL | VS | 1001101 |
| SCL | SDA | 1001110 |
| SCL | SCL | 1001111 |
| A1 | A0 | Addr | HEX |
|:-----:|:-----:|:------:|:------:|
| GND | GND | 64 | 0x40 |
| GND | VS | 65 | 0x41 |
| GND | SDA | 66 | 0x42 |
| GND | SCL | 67 | 0x43 |
| VS | GND | 68 | 0x44 |
| VS | VS | 69 | 0x45 |
| VS | SDA | 70 | 0x46 |
| VS | SCL | 71 | 0x47 |
| SDA | GND | 72 | 0x48 |
| SDA | VS | 73 | 0x49 |
| SDA | SDA | 74 | 0x4A |
| SDA | SCL | 75 | 0x4B |
| SCL | GND | 76 | 0x4C |
| SCL | VS | 77 | 0x4D |
| SCL | SDA | 78 | 0x4E |
| SCL | SCL | 79 | 0x4F |
#### Performance
@ -230,7 +231,7 @@ Note the value returned is not a unit of time.
| enum description | BVCT SVCT | time | notes |
|:------------------:|:---------:|----------:|--------:|
|:------------------:|:---------:|:---------:|--------:|
| INA226_140_us | 0 | 140 us |
| INA226_204_us | 1 | 204 us |
| INA226_332_us | 2 | 332 us |
@ -285,7 +286,7 @@ See https://github.com/RobTillaart/INA226/pull/29 for details of the discussion.
#### Error codes setMaxCurrentShunt
| descriptive name error | value | meaning |
|:-------------------------------|---------:|:----------|
|:-------------------------------|:--------:|:----------|
| INA226_ERR_NONE | 0x0000 | OK
| INA226_ERR_SHUNTVOLTAGE_HIGH | 0x8000 | maxCurrent \* shunt > 80 mV
| INA226_ERR_MAXCURRENT_LOW | 0x8001 | maxCurrent < 0.001
@ -329,7 +330,7 @@ Returns true if write to register successful.
| description alert register | value | a.k.a. |
|:-----------------------------|---------:| -------:|
|:-----------------------------|:--------:| -------:|
| INA226_SHUNT_OVER_VOLTAGE | 0x8000 | SOL |
| INA226_SHUNT_UNDER_VOLTAGE | 0x4000 | SUL |
| INA226_BUS_OVER_VOLTAGE | 0x2000 | BOL |
@ -339,7 +340,7 @@ Returns true if write to register successful.
| description alert flags | value |
|:---------------------------------|---------:|
|:---------------------------------|:--------:|
| INA226_ALERT_FUNCTION_FLAG | 0x0010 |
| INA226_CONVERSION_READY_FLAG | 0x0008 |
| INA226_MATH_OVERFLOW_FLAG | 0x0004 |
@ -381,11 +382,6 @@ Be aware that
- you do this at your own risk.
## Operational
See examples..
## Future

View File

@ -33,12 +33,14 @@ void setup()
delay(100);
INA.setMaxCurrentShunt(1, 0.002);
int x = INA.setMaxCurrentShunt(1, 0.002);
Serial.println("normalized = true (default)");
Serial.println(x);
printConfig();
INA.setMaxCurrentShunt(1, 0.002, false);
x = INA.setMaxCurrentShunt(1, 0.002, false);
Serial.println("normalized = false");
Serial.println(x);
printConfig();
@ -54,10 +56,10 @@ void setup()
void loop()
{
INA.setMaxCurrentShunt(1, 0.002);
INA.setMaxCurrentShunt(1, 0.100);
measure(20);
INA.setMaxCurrentShunt(1, 0.002, false);
INA.setMaxCurrentShunt(1, 0.100, false);
measure(20);
}

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/INA226.git"
},
"version": "0.5.5",
"version": "0.6.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=INA226
version=0.5.5
version=0.6.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for INA226 power sensor