0.5.4 INA226

This commit is contained in:
Rob Tillaart 2024-04-04 15:37:14 +02:00
parent 501009165a
commit 8fcd95e523
9 changed files with 177 additions and 16 deletions

View File

@ -1,4 +1,3 @@
---
name: Arduino CI

View File

@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.5.4] - 2024-04-04
- add multi device and array example
- add address table to readme.md
- minor edits
## [0.5.3] - 2024-03-25
- add enum **ina226_average_enum** (Thanks to Henk Holdijk)
- add enum **ina226_timing_enum** for BVCT SVCT conversion timing
@ -15,7 +20,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- update unit test
- minor edits
## [0.5.2] - 2024-01-06
- Thanks to Henk Holdijk for his improvements.
- fix #35, add **bool isConversionReady()**

View File

@ -1,6 +1,6 @@
// FILE: INA226.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.5.3
// VERSION: 0.5.4
// DATE: 2021-05-18
// PURPOSE: Arduino library for INA226 power sensor
// URL: https://github.com/RobTillaart/INA226
@ -108,7 +108,7 @@ bool INA226::isConversionReady()
bool INA226::waitConversionReady(uint32_t timeout)
{
uint32_t start = millis();
while ( (millis() - start) <= timeout)
while ( (millis() - start) <= timeout)
{
if (isConversionReady()) return true;
delay(1); // implicit yield();
@ -228,18 +228,18 @@ int INA226::setMaxCurrentShunt(float maxCurrent, float shunt, bool normalize)
if (normalize)
{
/*
check if maxCurrent (normal) or shunt resistor
check if maxCurrent (normal) or shunt resistor
(due to unusual low resistor values in relation to maxCurrent) determines currentLSB
we have to take the upper value for currentLSB
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^16-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) ~= 2^9 * 1e-5 / ( shunt * 2^16 )
currentLSB(min) ~= 1e-5 / 2^7 / shunt
currentLSB(min) ~= 7.8125e-8 / shunt
currentLSB(min) ~= 7.8125e-8 / shunt
*/
if ( 7.8125e-8 / shunt > _current_LSB ) {
// shunt resistor determines currentLSB -> take this a starting point for currentLSB
@ -274,13 +274,13 @@ 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.
_current_LSB = 0;
return INA226_ERR_NORMALIZE_FAILED;
}
#ifdef printdebug
Serial.print("After scale current_LSB:\t");
Serial.print(_current_LSB * 1e+6, 1);

View File

@ -1,7 +1,7 @@
#pragma once
// FILE: INA226.h
// AUTHOR: Rob Tillaart
// VERSION: 0.5.3
// VERSION: 0.5.4
// 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.3"
#define INA226_LIB_VERSION "0.5.4"
// set by setAlertRegister

View File

@ -65,7 +65,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 datasheet - table 2 - datasheet.
See table - from datasheet table 2, page18.
| 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 |
#### Performance

View File

@ -0,0 +1,74 @@
//
// FILE: INA226_array.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo four INA226's, adjust to your sensor count
// URL: https://github.com/RobTillaart/INA226
//
// not tested with HW yet
#include "INA226.h"
#define INA_COUNT 4
INA226 INA[INA_COUNT] =
{
INA226(0x40),
INA226(0x41),
INA226(0x42),
INA226(0x43)
};
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("INA226_LIB_VERSION: ");
Serial.println(INA226_LIB_VERSION);
Wire.begin();
bool failed = false;
for (int ID = 0; ID < INA_COUNT; ID++)
{
if (!INA[ID].begin() )
{
failed = true;
Serial.println(ID);
}
INA[ID].setMaxCurrentShunt(1, 0.002);
}
if (failed)
{
Serial.println("One or more INA could not connect. Fix and Reboot");
while (1);
}
}
void loop()
{
Serial.println("\nID\tBUS\tSHUNT\tCURRENT\tPOWER");
for (int i = 0; i < 20; i++)
{
for (int ID = 0; ID < 4; ID++)
{
Serial.print(ID);
Serial.print("\t");
Serial.print(INA[ID].getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA[ID].getShuntVoltage_mV(), 3);
Serial.print("\t");
Serial.print(INA[ID].getCurrent_mA(), 3);
Serial.print("\t");
Serial.print(INA[ID].getPower_mW(), 3);
Serial.println();
delay(1000);
}
Serial.println();
}
}
// -- END OF FILE --

View File

@ -0,0 +1,65 @@
//
// FILE: INA226_multi_device.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo two INA226's
// URL: https://github.com/RobTillaart/INA226
//
// not tested with HW yet
#include "INA226.h"
INA226 INA0(0x40);
INA226 INA1(0x41);
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("INA226_LIB_VERSION: ");
Serial.println(INA226_LIB_VERSION);
Wire.begin();
if (!INA0.begin() )
{
Serial.println("INA0 could not connect. Fix and Reboot");
}
if (!INA1.begin() )
{
Serial.println("INA1 could not connect. Fix and Reboot");
}
INA0.setMaxCurrentShunt(1, 0.002);
INA1.setMaxCurrentShunt(1, 0.002);
}
void loop()
{
Serial.println("\nBUS\tSHUNT\tCURRENT\tPOWER\t\tBUS\tSHUNT\tCURRENT\tPOWER");
for (int i = 0; i < 20; i++)
{
Serial.print(INA0.getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA0.getShuntVoltage_mV(), 3);
Serial.print("\t");
Serial.print(INA0.getCurrent_mA(), 3);
Serial.print("\t");
Serial.print(INA0.getPower_mW(), 3);
Serial.print("\t");
Serial.print("\t");
Serial.print(INA1.getBusVoltage(), 3);
Serial.print("\t");
Serial.print(INA1.getShuntVoltage_mV(), 3);
Serial.print("\t");
Serial.print(INA1.getCurrent_mA(), 3);
Serial.print("\t");
Serial.print(INA1.getPower_mW(), 3);
Serial.println();
delay(1000);
}
}
// -- END OF FILE --

View File

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

View File

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