From cec0f529fd0294f9491c54c04aabfe16eb2e8e89 Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Sun, 30 May 2021 14:22:31 +0200 Subject: [PATCH] 0.1.0 INA226 --- libraries/INA226/.arduino-ci.yml | 7 + .../INA226/.github/workflows/arduino-lint.yml | 13 + .../.github/workflows/arduino_test_runner.yml | 13 + .../INA226/.github/workflows/jsoncheck.yml | 18 ++ libraries/INA226/INA226.cpp | 288 ++++++++++++++++++ libraries/INA226/INA226.h | 109 +++++++ libraries/INA226/LICENSE | 21 ++ libraries/INA226/README.md | 179 +++++++++++ .../examples/INA226_demo/INA226_demo.ino | 56 ++++ .../INA226_demo_alert/INA226_demo_alert.ino | 61 ++++ .../INA226_setBusVoltageConversionTime.ino | 66 ++++ .../INA226_setShuntVoltageConversionTime.ino | 67 ++++ .../INA226_set_average/INA226_set_average.ino | 66 ++++ libraries/INA226/keywords.txt | 46 +++ libraries/INA226/library.json | 22 ++ libraries/INA226/library.properties | 11 + libraries/INA226/test/unit_test_001.cpp | 59 ++++ 17 files changed, 1102 insertions(+) create mode 100644 libraries/INA226/.arduino-ci.yml create mode 100644 libraries/INA226/.github/workflows/arduino-lint.yml create mode 100644 libraries/INA226/.github/workflows/arduino_test_runner.yml create mode 100644 libraries/INA226/.github/workflows/jsoncheck.yml create mode 100644 libraries/INA226/INA226.cpp create mode 100644 libraries/INA226/INA226.h create mode 100644 libraries/INA226/LICENSE create mode 100644 libraries/INA226/README.md create mode 100644 libraries/INA226/examples/INA226_demo/INA226_demo.ino create mode 100644 libraries/INA226/examples/INA226_demo_alert/INA226_demo_alert.ino create mode 100644 libraries/INA226/examples/INA226_setBusVoltageConversionTime/INA226_setBusVoltageConversionTime.ino create mode 100644 libraries/INA226/examples/INA226_setShuntVoltageConversionTime/INA226_setShuntVoltageConversionTime.ino create mode 100644 libraries/INA226/examples/INA226_set_average/INA226_set_average.ino create mode 100644 libraries/INA226/keywords.txt create mode 100644 libraries/INA226/library.json create mode 100644 libraries/INA226/library.properties create mode 100644 libraries/INA226/test/unit_test_001.cpp diff --git a/libraries/INA226/.arduino-ci.yml b/libraries/INA226/.arduino-ci.yml new file mode 100644 index 00000000..ff5659b9 --- /dev/null +++ b/libraries/INA226/.arduino-ci.yml @@ -0,0 +1,7 @@ +compile: + # Choosing to run compilation tests on 2 different Arduino platforms + platforms: + - uno + - leonardo + - due + - zero diff --git a/libraries/INA226/.github/workflows/arduino-lint.yml b/libraries/INA226/.github/workflows/arduino-lint.yml new file mode 100644 index 00000000..b2ca058c --- /dev/null +++ b/libraries/INA226/.github/workflows/arduino-lint.yml @@ -0,0 +1,13 @@ + +name: Arduino-lint + +on: [push, pull_request] +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: arduino/arduino-lint-action@v1 + with: + library-manager: update + compliance: strict diff --git a/libraries/INA226/.github/workflows/arduino_test_runner.yml b/libraries/INA226/.github/workflows/arduino_test_runner.yml new file mode 100644 index 00000000..476456bb --- /dev/null +++ b/libraries/INA226/.github/workflows/arduino_test_runner.yml @@ -0,0 +1,13 @@ +--- +name: Arduino CI + +on: [push, pull_request] + +jobs: + arduino_ci: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + - uses: Arduino-CI/action@master + # Arduino-CI/action@v0.1.1 diff --git a/libraries/INA226/.github/workflows/jsoncheck.yml b/libraries/INA226/.github/workflows/jsoncheck.yml new file mode 100644 index 00000000..04603d08 --- /dev/null +++ b/libraries/INA226/.github/workflows/jsoncheck.yml @@ -0,0 +1,18 @@ +name: JSON check + +on: + push: + paths: + - '**.json' + pull_request: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: json-syntax-check + uses: limitusus/json-syntax-check@v1 + with: + pattern: "\\.json$" + diff --git a/libraries/INA226/INA226.cpp b/libraries/INA226/INA226.cpp new file mode 100644 index 00000000..f0f792a7 --- /dev/null +++ b/libraries/INA226/INA226.cpp @@ -0,0 +1,288 @@ +// FILE: INA266.h +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// DATE: 2021-05-18 +// PURPOSE: Arduino library for INA266 power sensor +// URL: https://github.com/RobTillaart/INA226 +// +// HISTORY: +// 0.1.0 2021-05-18 initial version + + + +#include "INA226.h" + +#define INA226_CONFIGURATION 0x00 +#define INA226_SHUNT_VOLTAGE 0x01 +#define INA226_BUS_VOLTAGE 0x02 +#define INA226_POWER 0x03 +#define INA226_CURRENT 0x04 +#define INA226_CALIBRATION 0x05 +#define INA226_MASK_ENABLE 0x06 +#define INA226_ALERT_LIMIT 0x07 +#define INA226_MANUFACTURER 0xFE +#define INA226_DIE_ID 0xFF + + +//////////////////////////////////////////////////////// +// +// Constructor +// +INA226::INA226(const int8_t address, TwoWire *wire) +{ + _address = address; + _wire = wire; + _current_LSB = 0; +} + + +#if defined (ESP8266) || defined(ESP32) +bool INA226::begin(const uint8_t sda, const uint8_t scl) +{ + _wire = &Wire; + _wire->begin(sda, scl); + if (! isConnected()) return false; + return true; +} +#endif + + +bool INA226::begin() +{ + _wire->begin(); + if (! isConnected()) return false; + return true; +} + + +bool INA226::isConnected() +{ + _wire->beginTransmission(_address); + return ( _wire->endTransmission() == 0); +} + + +//////////////////////////////////////////////////////// +// +// Core functions +// +float INA226::getShuntVoltage() +{ + uint16_t val = _readRegister(INA226_SHUNT_VOLTAGE); + if (val & 0x8000) + { + val = val & 0x7FFF; + val = val ^ 0x7FFF; + val++; + return val * -2.5e-6; + } + return val * 2.5e-6; // fixed 2.50 uV +} + + +float INA226::getBusVoltage() +{ + uint16_t val = _readRegister(INA226_BUS_VOLTAGE); + return val * 1.25e-3; // fixed 1.25 mV +} + + +float INA226::getPower() +{ + uint16_t val = _readRegister(INA226_POWER); + return val * 25 * _current_LSB; +} + + +float INA226::getCurrent() +{ + uint16_t val = _readRegister(INA226_CURRENT); + return val * _current_LSB; +} + + +//////////////////////////////////////////////////////// +// +// Configuration +// +void INA226::reset() +{ + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask |= 0x800; + _writeRegister(INA226_CONFIGURATION, mask); +} + + +void INA226::setAverage(uint8_t avg) +{ + if (avg > 7) return; + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask &= 0xF1FF; + mask |= (avg << 9); + _writeRegister(INA226_CONFIGURATION, mask); +} + + +uint8_t INA226::getAverage() +{ + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask >>= 9; + mask &= 7; + return mask; +} + + +void INA226::setBusVoltageConversionTime(uint8_t bvct) +{ + if (bvct > 7) return; + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask &= 0xFE3F; + mask |= (bvct << 6); + _writeRegister(INA226_CONFIGURATION, mask); +} + + +uint8_t INA226::getBusVoltageConversionTime() +{ + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask >>= 6; + mask &= 7; + return mask; +} + + +void INA226::setShuntVoltageConversionTime(uint8_t svct) +{ + if (svct > 7) return; + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask &= 0xFFC7; + mask |= (svct << 3); + _writeRegister(INA226_CONFIGURATION, mask); +} + + +uint8_t INA226::getShuntVoltageConversionTime() +{ + uint16_t mask = _readRegister(INA226_CONFIGURATION); + mask >>= 3; + mask &= 7; + return mask; +} + + +//////////////////////////////////////////////////////// +// +// Calibration +// +void INA226::setMaxCurrentShunt(float ampere, float ohm) +{ + _current_LSB = ampere * (1.0 / 32768.0); + // make the LSB a round number + float factor = 1; + // Serial.println(_current_LSB, 6); + while (_current_LSB < 1) + { + _current_LSB *= 10; + factor *= 10; + } + _current_LSB = 10.0 / factor; + // Serial.println(_current_LSB, 6); + uint16_t calib = round(0.00512 / (_current_LSB * ohm)); + // Serial.println(calib); + _writeRegister(INA226_CALIBRATION, calib); +} + + +//////////////////////////////////////////////////////// +// +// operating mode +// +void INA226::setMode(uint8_t mode) +{ + if (mode > 7) return; + uint16_t m = _readRegister(INA226_CONFIGURATION); + m &= 0xFFF8; + m |= mode; + _writeRegister(INA226_CONFIGURATION, m); +} + + +uint8_t INA226::getMode() +{ + return _readRegister(INA226_CONFIGURATION) & 0x0007; +} + + +//////////////////////////////////////////////////////// +// +// alert +// +void INA226::setAlertRegister(uint16_t mask) +{ + _writeRegister(INA226_MASK_ENABLE, (mask & 0xFC00)); +} + + +uint16_t INA226::getAlertFlag() +{ + return _readRegister(INA226_MASK_ENABLE) & 0x001F; +} + + +void INA226::setAlertLimit(uint16_t limit) +{ + _writeRegister(INA226_ALERT_LIMIT, limit); +} + + +uint16_t INA226::getAlertLimit() +{ + return _readRegister(INA226_ALERT_LIMIT); +} + + +//////////////////////////////////////////////////////// +// +// meta information +// +uint16_t INA226::getManufacturerID() +{ + return _readRegister(INA226_MANUFACTURER); +} + +uint16_t INA226::getDieID() +{ + return _readRegister(INA226_DIE_ID); +} + + +//////////////////////////////////////////////////////// +// +// PRIVATE +// +uint16_t INA226::_readRegister(uint8_t reg) +{ + _wire->beginTransmission(_address); + _wire->write(reg); + _wire->endTransmission(); + + _wire->requestFrom(_address, (uint8_t)2); + uint16_t value = _wire->read(); + value <<= 8; + value |= _wire->read(); + return value; +} + + +uint16_t INA226::_writeRegister(uint8_t reg, uint16_t value) +{ + _wire->beginTransmission(_address); + _wire->write(reg); + _wire->write(value >> 8); + _wire->write(value & 0xFF); + return _wire->endTransmission(); +} + + +// -- END OF FILE -- + diff --git a/libraries/INA226/INA226.h b/libraries/INA226/INA226.h new file mode 100644 index 00000000..b9949a57 --- /dev/null +++ b/libraries/INA226/INA226.h @@ -0,0 +1,109 @@ +#pragma once +// FILE: INA266.h +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// DATE: 2021-05-18 +// PURPOSE: Arduino library for INA266 power sensor +// URL: https://github.com/RobTillaart/INA226 +// +// Read the datasheet for the details +// + + +#include "Arduino.h" +#include "Wire.h" + + +#define INA226_LIB_VERSION (F("0.1.0")) + + +// set by setAlertRegister +#define INA226_SHUNT_OVER_VOLTAGE 0x8000 +#define INA226_SHUNT_UNDER_VOLTAGE 0x4000 +#define INA226_BUS_OVER_VOLTAGE 0x2000 +#define INA226_BUS_UNDER_VOLTAGE 0x1000 +#define INA226_POWER_OVER_LIMIT 0x0800 +#define INA226_CONVERSION_READY 0x0400 + +// returned by getAlertFlag +#define INA226_ALERT_FUNCTION_FLAG 0x0010 +#define INA226_CONVERSION_READY_FLAG 0x0008 +#define INA226_MATH_OVERFLOW_FLAG 0x0004 +#define INA226_ALERT_POLARITY_FLAG 0x0002 +#define INA226_ALERT_LATCH_ENABLE_FLAG 0x0001 + + +class INA226 +{ +public: + // address between 0x40 and 0x4F + explicit INA226(const int8_t address, TwoWire *wire = &Wire); + +#if defined (ESP8266) || defined(ESP32) + bool begin(const uint8_t sda, const uint8_t scl); +#endif + + bool begin(); + bool isConnected(); + + // Core functions + float getShuntVoltage(); + float getBusVoltage(); + float getPower(); + float getCurrent(); + + // Configuration + void reset(); + void setAverage(uint8_t avg = 0); + uint8_t getAverage(); + void setBusVoltageConversionTime(uint8_t bvct = 4); + uint8_t getBusVoltageConversionTime(); + void setShuntVoltageConversionTime(uint8_t svct = 4); + uint8_t getShuntVoltageConversionTime(); + + // Calibration + void setMaxCurrentShunt(float ampere = 10.0, float ohm = 0.1); + float getCurrentLSB() { return _current_LSB; }; + + + // Operating mode + void setMode(uint8_t mode = 7); + uint8_t getMode(); + void shutDown() { setMode(0); }; + void setModeShuntTrigger() { setMode(1); }; + void setModeBusTrigger() { setMode(2); }; + void setModeShuntBusTrigger() { setMode(3); }; + void setModeShuntContinuous() { setMode(5); }; + void setModeBusContinuous() { setMode(6); }; + void setModeShuntBusContinuous() { setMode(7); }; // default. + + + // Alert + // - separate functions per flag? + // - what is a reasonable limit? + // - which units to define a limit per mask ? + // same as voltage registers ? + // - how to test + void setAlertRegister(uint16_t mask); + uint16_t getAlertFlag(); + void setAlertLimit(uint16_t limit); + uint16_t getAlertLimit(); + + + // Meta information + uint16_t getManufacturerID(); // should return 0x5449 + uint16_t getDieID(); // should return 0x2260 + + +private: + + uint16_t _readRegister(uint8_t reg); + uint16_t _writeRegister(uint8_t reg, uint16_t value); + float _current_LSB; + + uint8_t _address; + TwoWire * _wire; + +}; + +// -- END OF FILE -- diff --git a/libraries/INA226/LICENSE b/libraries/INA226/LICENSE new file mode 100644 index 00000000..a8b455d0 --- /dev/null +++ b/libraries/INA226/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021-2021 Rob Tillaart + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/libraries/INA226/README.md b/libraries/INA226/README.md new file mode 100644 index 00000000..b8560d25 --- /dev/null +++ b/libraries/INA226/README.md @@ -0,0 +1,179 @@ + +[![Arduino CI](https://github.com/RobTillaart/INA226/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci) +[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/INA226/blob/master/LICENSE) +[![GitHub release](https://img.shields.io/github/release/RobTillaart/INA226.svg?maxAge=3600)](https://github.com/RobTillaart/INA226/releases) + + +# INA226 + +Arduino library for the INA226 power sensor + + +## Description + +Experimental library for the INA226 power sensor. +Not all functionality is tested / investigated. + +==> **USE WITH CARE** + +The INA226 is a voltage, current and power measurement device. a few important maxima. (See datasheet, ch. 6) + +| description | max | unit | +|:--------------|------:|-------:| +| bus voltage | 36 | Volt | +| shunt voltage | 80 | mVolt | +| current | ?? | Ampere | + + +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. + +TODO: elaborate. + + + +## Interface + +read datasheet for details + +### Constructor + +- **INA226(const int8_t address, TwoWire \*wire = Wire)** Constructor to set address and optional Wire interface. +- **bool begin(const uint8_t sda, const uint8_t scl)** for ESP32 and ESP8266; initializes the class. +sets I2C pins. Returns true if the INA226 address is on the I2C bus. +- **bool begin()** UNO ea. initializes the class. +returns true if the INA226 address is on the I2C bus. +- **bool isConnected()** returns true if the INA226 address is on the I2C bus. + + +### Core Functions + +Note the power and the current are not meaningful without calibrating +the sensor. Also the value is not meaningful if there is no shunt connected. + +- **float getShuntVoltage()** idem. +- **float getBusVoltage()** idem. Max 36 Volt. +- **float getCurrent()** is the current through the shunt. +- **float getPower()** is the current x BusVoltage + + +### Configuration + +to be tested. + +- **void reset()** software power on reset +- **void setAverage(uint8_t avg = 0)** see table below +(0 = default ==> 1 read) +- **uint8_t getAverage()** returns the value set. Note this is not the count of samples. +- **void setBusVoltageConversionTime(uint8_t bvct = 4)** see table below +(4 = default ==> 1.1 ms) +- **uint8_t getBusVoltageConversionTime()** return the value set. Note this is not a unit of time. +- **void setShuntVoltageConversionTime(uint8_t svct = 4)** see table below +(4 = default ==> 1.1 ms) +- **uint8_t getShuntVoltageConversionTime()** return the value set. Note this is not a unit of time. + + +| Average | # samples | notes | +|:-------:|----------:|--------:| +| 0 | 1 | default | +| 1 | 4 | | +| 2 | 16 | | +| 3 | 64 | | +| 4 | 128 | | +| 5 | 256 | | +| 6 | 512 | | +| 7 | 1024 | | + + + +| BVCT SVCT | time | notes | +|:---------:|----------:|--------:| +| 0 | 140 us | +| 1 | 204 us | +| 2 | 332 us | +| 3 | 588 us | +| 4 | 1.1 ms | default | +| 5 | 2.1 ms | +| 6 | 4.2 ms | +| 7 | 8.3 ms | + + +Note that total conversion time can take up to 1024 \* 8.3 ms ~ 10 seconds. + + +### Calibration + +See daatsheet, not tested yet. + +- **void setMaxCurrentShunt(float ampere = 10.0, float ohm = 0.1)** set the calibration register based upon the shunt and the max ampere. From this the LSB is derived. Note the function will round up the LSB to nearest round value. +- **float getCurrentLSB()** returns the LSB == precission of the calibration + + +### Operating mode + +See datasheet, partially tested. + +Mode = 4 is not used, is also a **shutdown()** unknown if there is a difference. + +- **void setMode(uint8_t mode = 7)** mode = 0 .. 7 +- **void shutDown()** mode 0 - not tested yet +- **void setModeShuntTrigger()** mode 1 - not tested yet - how to trigger to be investigated +- **void setModeBusTrigger()** mode 2 - not tested yet - +- **void setModeShuntBusTrigger()** mode 3 - not tested yet - +- **void setModeShuntContinuous()** mode 5 +- **void setModeBusContinuous()** mode 6 +- **void setModeShuntBusContinuous()** mode 7 - default +- **uint8_t getMode()** returns the mode (0..7) set by one of the functions above. + + +### Alert functions + +See datasheet, not tested yet. + +- **void setAlertRegister(uint16_t mask)** by setting the mask one of five an over- or underflow can be detected. Another feature that can be set si the conversion ready flag. +- **uint16_t getAlertFlag()** returns the mask set by **setAlertRegister()** +- **void setAlertLimit(uint16_t limit)** sets the limit that belongs to the chosen Alert Flag +- **uint16_t getAlertLimit()** returns the limit set by **setAlertLimit()** + + +| 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 | +| INA226_BUS_UNDER_VOLTAGE | 0x1000 | BUL | +| INA226_POWER_OVER_LIMIT | 0x0800 | POL | +| INA226_CONVERSION_READY | 0x0400 | | + + +| description alert flags | value | +|:-------------------------------|-------:| +| INA226_ALERT_FUNCTION_FLAG | 0x0010 | +| INA226_CONVERSION_READY_FLAG | 0x0008 | +| INA226_MATH_OVERFLOW_FLAG | 0x0004 | +| INA226_ALERT_POLARITY_FLAG | 0x0002 | +| INA226_ALERT_LATCH_ENABLE_FLAG | 0x0001 | + + +The alert line falls when alert is reached. + + + +### Meta information + +- **uint16_t getManufacturerID()** should return 0x5449 +- **uint16_t getDieID()** should return 0x2260 + + +## Operational + +See examples.. + +Not all examples are tested. + + +## TODO + +- testtestestest +- improve readme.md diff --git a/libraries/INA226/examples/INA226_demo/INA226_demo.ino b/libraries/INA226/examples/INA226_demo/INA226_demo.ino new file mode 100644 index 00000000..a395edca --- /dev/null +++ b/libraries/INA226/examples/INA226_demo/INA226_demo.ino @@ -0,0 +1,56 @@ +// +// FILE: INA226_demo.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2021-05-18 +// URL: https://github.com/RobTillaart/INA226 + + +#include "INA226.h" +#include "Wire.h" + +INA226 INA(0x40); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + if (!INA.begin() ) + { + Serial.println("could not connect. Fix and Reboot"); + } + + Serial.println(); + Serial.print("MAN:\t"); + Serial.println(INA.getManufacturerID(), HEX); + Serial.print("DIE:\t"); + Serial.println(INA.getDieID(), HEX); + delay(100); + INA.setMaxCurrentShunt(15, 0.002); + Serial.print("LSB:\t"); + Serial.println(INA.getCurrentLSB(), 6); + Serial.println("\n\n"); + + Serial.println("BUS\tSHUNT\tCURRENT\tPOWER"); +} + + +void loop() +{ + Serial.print(INA.getBusVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getShuntVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getCurrent(), 4); + Serial.print("\t"); + Serial.print(INA.getPower(), 4); + Serial.println(); + delay(1000); +} + + +// -- END OF FILE -- diff --git a/libraries/INA226/examples/INA226_demo_alert/INA226_demo_alert.ino b/libraries/INA226/examples/INA226_demo_alert/INA226_demo_alert.ino new file mode 100644 index 00000000..3de75dc1 --- /dev/null +++ b/libraries/INA226/examples/INA226_demo_alert/INA226_demo_alert.ino @@ -0,0 +1,61 @@ +// +// FILE: INA226_demo_alert.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2021-05-18 +// URL: https://github.com/RobTillaart/INA226 + + +// EXPERIMENTAL CODE - NOT TESTED. + + +#include "INA226.h" +#include "Wire.h" + +INA226 INA(0x40); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + if (!INA.begin() ) + { + Serial.println("could not connect. Fix and Reboot"); + } + + // to be tested.... + // measure POWER LIMIT ? + // assume milisamperes. + uint16_t limit = 1000; + INA.setAlertLimit(limit); + + // read back to verify. + uint16_t test_limit = INA.getAlertLimit(); + if (test_limit != limit) + { + Serial.print("Unexpected limit:\t"); + Serial.println(test_limit); + while(1); + } + + uint16_t alert_mask = INA226_POWER_OVER_LIMIT; + INA.setAlertRegister(alert_mask); +} + + +void loop() +{ + uint16_t flags = INA.getAlertFlag(); + if (flags ) + { + Serial.print("alarm:\t"); + Serial.println(INA.getShuntVoltage()); + } +} + + +// -- END OF FILE -- diff --git a/libraries/INA226/examples/INA226_setBusVoltageConversionTime/INA226_setBusVoltageConversionTime.ino b/libraries/INA226/examples/INA226_setBusVoltageConversionTime/INA226_setBusVoltageConversionTime.ino new file mode 100644 index 00000000..d6c7a748 --- /dev/null +++ b/libraries/INA226/examples/INA226_setBusVoltageConversionTime/INA226_setBusVoltageConversionTime.ino @@ -0,0 +1,66 @@ +// +// FILE: INA226_BusVoltageConversionTime.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2021-05-18 +// URL: https://github.com/RobTillaart/INA226 + + +#include "INA226.h" +#include "Wire.h" + +INA226 INA(0x40); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + if (!INA.begin() ) + { + Serial.println("could not connect. Fix and Reboot"); + } + + Serial.print("SHUNT:\t"); + Serial.println(INA.getShuntVoltage(), 2); + Serial.print(" BUS:\t"); + Serial.println(INA.getBusVoltage(), 2); + Serial.print("POWER:\t"); + Serial.println(INA.getPower(), 2); + Serial.print(" CURR:\t"); + Serial.println(INA.getCurrent(), 2); + + Serial.println(); + Serial.print("MAN:\t"); + Serial.println(INA.getManufacturerID(), HEX); + Serial.print("DIE:\t"); + Serial.println(INA.getDieID(), HEX); + + Serial.println("done..."); +} + + +void loop() +{ + for (int bvct = 0; bvct < 8; bvct++) + { + INA.setBusVoltageConversionTime(bvct); + Serial.print(bvct); + Serial.print(INA.getBusVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getShuntVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getCurrent(), 4); + Serial.print("\t"); + Serial.print(INA.getPower(), 4); + Serial.println(); + delay(1000); + } +} + + + +// -- END OF FILE -- diff --git a/libraries/INA226/examples/INA226_setShuntVoltageConversionTime/INA226_setShuntVoltageConversionTime.ino b/libraries/INA226/examples/INA226_setShuntVoltageConversionTime/INA226_setShuntVoltageConversionTime.ino new file mode 100644 index 00000000..3b906bcb --- /dev/null +++ b/libraries/INA226/examples/INA226_setShuntVoltageConversionTime/INA226_setShuntVoltageConversionTime.ino @@ -0,0 +1,67 @@ +// +// FILE: INA226_demoShuntVoltageConversionTime.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2021-05-18 +// URL: https://github.com/RobTillaart/INA226 + + +#include "INA226.h" +#include "Wire.h" + +INA226 INA(0x40); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + if (!INA.begin() ) + { + Serial.println("could not connect. Fix and Reboot"); + } + + Serial.print("SHUNT:\t"); + Serial.println(INA.getShuntVoltage(), 2); + Serial.print(" BUS:\t"); + Serial.println(INA.getBusVoltage(), 2); + Serial.print("POWER:\t"); + Serial.println(INA.getPower(), 2); + Serial.print(" CURR:\t"); + Serial.println(INA.getCurrent(), 2); + + Serial.println(); + Serial.print("MAN:\t"); + Serial.println(INA.getManufacturerID(), HEX); + Serial.print("DIE:\t"); + Serial.println(INA.getDieID(), HEX); + + Serial.println("done..."); +} + + +void loop() +{ + for (int svct = 0; svct < 8; svct++) + { + INA.setShuntVoltageConversionTime(svct); + Serial.print(svct); + Serial.print(INA.getBusVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getShuntVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getCurrent(), 4); + Serial.print("\t"); + Serial.print(INA.getPower(), 4); + Serial.println(); + delay(1000); + } +} + + + + +// -- END OF FILE -- diff --git a/libraries/INA226/examples/INA226_set_average/INA226_set_average.ino b/libraries/INA226/examples/INA226_set_average/INA226_set_average.ino new file mode 100644 index 00000000..ef451f21 --- /dev/null +++ b/libraries/INA226/examples/INA226_set_average/INA226_set_average.ino @@ -0,0 +1,66 @@ +// +// FILE: INA226_set_average.ino +// AUTHOR: Rob Tillaart +// VERSION: 0.1.0 +// PURPOSE: demo +// DATE: 2021-05-18 +// URL: https://github.com/RobTillaart/INA226 + + +#include "INA226.h" +#include "Wire.h" + +INA226 INA(0x40); + + +void setup() +{ + Serial.begin(115200); + Serial.println(__FILE__); + + Wire.begin(); + if (!INA.begin() ) + { + Serial.println("could not connect. Fix and Reboot"); + } + + Serial.print("SHUNT:\t"); + Serial.println(INA.getShuntVoltage(), 2); + Serial.print(" BUS:\t"); + Serial.println(INA.getBusVoltage(), 2); + Serial.print("POWER:\t"); + Serial.println(INA.getPower(), 2); + Serial.print(" CURR:\t"); + Serial.println(INA.getCurrent(), 2); + + Serial.println(); + Serial.print("MAN:\t"); + Serial.println(INA.getManufacturerID(), HEX); + Serial.print("DIE:\t"); + Serial.println(INA.getDieID(), HEX); + + Serial.println("now set average"); +} + + +void loop() +{ + for (int avg = 0; avg < 8; avg++) + { + INA.setAverage(avg); + Serial.print(avg); + Serial.print(INA.getBusVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getShuntVoltage(), 4); + Serial.print("\t"); + Serial.print(INA.getCurrent(), 4); + Serial.print("\t"); + Serial.print(INA.getPower(), 4); + Serial.println(); + delay(1000); + } +} + + + +// -- END OF FILE -- diff --git a/libraries/INA226/keywords.txt b/libraries/INA226/keywords.txt new file mode 100644 index 00000000..7ec5eb7d --- /dev/null +++ b/libraries/INA226/keywords.txt @@ -0,0 +1,46 @@ +# Syntax Coloring Map For INA226 + +# Datatypes (KEYWORD1) +INA226 KEYWORD1 + +# Methods and Functions (KEYWORD2) +begin KEYWORD2 +isConnected KEYWORD2 + +getShuntVoltage KEYWORD2 +getBusVoltage KEYWORD2 +getPower KEYWORD2 +getCurrent KEYWORD2 + +reset KEYWORD2 +setAverage KEYWORD2 +getAverage KEYWORD2 +setBusVoltageConversionTime KEYWORD2 +getBusVoltageConversionTime KEYWORD2 +setShuntVoltageConversionTime KEYWORD2 +getShuntVoltageConversionTime KEYWORD2 + +setMaxCurrentShunt KEYWORD2 +getCurrentLSB KEYWORD2 + +setMode KEYWORD2 +getMode KEYWORD2 +shutDown KEYWORD2 +setModeShuntTrigger KEYWORD2 +setModeBusTrigger KEYWORD2 +setModeShuntBusTrigger KEYWORD2 +setModeShuntContinuous KEYWORD2 +setModeBusContinuous KEYWORD2 +setModeShuntBusContinuous KEYWORD2 + +setAlertRegister KEYWORD2 +getAlertFlag KEYWORD2 +setAlertLimit KEYWORD2 +getAlertLimit KEYWORD2 + +getManufacturerID KEYWORD2 +getDieID KEYWORD2 + + +# Constants (LITERAL1) +INA226_LIB_VERSION LITERAL1 diff --git a/libraries/INA226/library.json b/libraries/INA226/library.json new file mode 100644 index 00000000..9c6ba4a8 --- /dev/null +++ b/libraries/INA226/library.json @@ -0,0 +1,22 @@ +{ + "name": "INA226", + "keywords": "power ampere voltage volts current", + "description": "Arduino library for INA226 power sensor", + "authors": + [ + { + "name": "Rob Tillaart", + "email": "Rob.Tillaart@gmail.com", + "maintainer": true + } + ], + "repository": + { + "type": "git", + "url": "https://github.com/RobTillaart/INA226.git" + }, + "version": "0.1.0", + "license": "MIT", + "frameworks": "arduino", + "platforms": "*" +} diff --git a/libraries/INA226/library.properties b/libraries/INA226/library.properties new file mode 100644 index 00000000..857c99c5 --- /dev/null +++ b/libraries/INA226/library.properties @@ -0,0 +1,11 @@ +name=INA226 +version=0.1.0 +author=Rob Tillaart +maintainer=Rob Tillaart +sentence=Arduino library for INA226 power sensor +paragraph=Voltage current Volt Ampere +category=Data Processing +url=https://github.com/RobTillaart/INA226 +architectures=* +includes=INA226.h +depends= diff --git a/libraries/INA226/test/unit_test_001.cpp b/libraries/INA226/test/unit_test_001.cpp new file mode 100644 index 00000000..d6bf4426 --- /dev/null +++ b/libraries/INA226/test/unit_test_001.cpp @@ -0,0 +1,59 @@ +// +// FILE: unit_test_001.cpp +// AUTHOR: Rob Tillaart +// DATE: 2021-05-18 +// PURPOSE: unit tests for the INA226 library +// https://github.com/RobTillaart/INA226 +// https://github.com/Arduino-CI/arduino_ci/blob/master/REFERENCE.md +// + +// supported assertions +// ---------------------------- +// assertEqual(expected, actual); // a == b +// assertNotEqual(unwanted, actual); // a != b +// assertComparativeEquivalent(expected, actual); // abs(a - b) == 0 or (!(a > b) && !(a < b)) +// assertComparativeNotEquivalent(unwanted, actual); // abs(a - b) > 0 or ((a > b) || (a < b)) +// assertLess(upperBound, actual); // a < b +// assertMore(lowerBound, actual); // a > b +// assertLessOrEqual(upperBound, actual); // a <= b +// assertMoreOrEqual(lowerBound, actual); // a >= b +// assertTrue(actual); +// assertFalse(actual); +// assertNull(actual); + +// // special cases for floats +// assertEqualFloat(expected, actual, epsilon); // fabs(a - b) <= epsilon +// assertNotEqualFloat(unwanted, actual, epsilon); // fabs(a - b) >= epsilon +// assertInfinity(actual); // isinf(a) +// assertNotInfinity(actual); // !isinf(a) +// assertNAN(arg); // isnan(a) +// assertNotNAN(arg); // !isnan(a) + +#include + + +#include "INA226.h" + + + +unittest_setup() +{ +} + +unittest_teardown() +{ +} + + +unittest(test_constructor) +{ + fprintf(stderr, "\nVERSION: %s\n", INA226_LIB_VERSION); + INA226 INA(); + + assertTrue(1); +} + + +unittest_main() + +// --------