From 7042e70b03f27582f3a84a91909dd8fde5b27ac9 Mon Sep 17 00:00:00 2001 From: Asuk Kono Date: Sat, 12 Oct 2019 00:03:15 +0900 Subject: [PATCH] Initial commit --- README.md | 16 ++++ .../printVoltageAndCurrent.ino | 25 ++++++ library.properties | 9 ++ src/INA226_asukiaaa.cpp | 87 +++++++++++++++++++ src/INA226_asukiaaa.h | 33 +++++++ 5 files changed, 170 insertions(+) create mode 100644 README.md create mode 100644 examples/printVoltageAndCurrent/printVoltageAndCurrent.ino create mode 100644 library.properties create mode 100644 src/INA226_asukiaaa.cpp create mode 100644 src/INA226_asukiaaa.h diff --git a/README.md b/README.md new file mode 100644 index 0000000..06a4771 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# INA226_asukiaaa + +A library to communicate with INA226 voltage and current metor. + +# Usage +See [example](./examples/printVoltageAndCurrent/printVoltageAndCurrent.ino) or [header file](./src/INA226_asukiaaa.h). + +# License + +MIT + +# References + +- [INA226 datasheet (PDF)](http://www.ti.com/lit/ds/symlink/ina226.pdf) +- [電圧/電流計測 INA226をCCS Cで使う](http://denshi-kousaku.fan.coocan.jp/report030.html) +- [INA226 I2Cディジタル電流・電圧・電力計モジュール](https://strawberry-linux.com/catalog/items?code=12031) diff --git a/examples/printVoltageAndCurrent/printVoltageAndCurrent.ino b/examples/printVoltageAndCurrent/printVoltageAndCurrent.ino new file mode 100644 index 0000000..8366e91 --- /dev/null +++ b/examples/printVoltageAndCurrent/printVoltageAndCurrent.ino @@ -0,0 +1,25 @@ +#include +#define INA226_ADDR B1000000 + +INA226_asukiaaa voltCurrMeter(INA226_ADDR, INA226_asukiaaa::calcCalibByResisterMilliOhm(2)); + +void setup() { + Serial.begin(115200); + if (voltCurrMeter.begin() != 0) { + Serial.println("Failed to begin INA226"); + } +} + +void loop() { + int16_t ma, mv; + if (voltCurrMeter.readMV(&mv) == 0) { + Serial.println(String(mv) + "mV"); + } else { + Serial.println("Cannot read voltage."); + } + if (voltCurrMeter.readMA(&ma) == 0) { + Serial.println(String(ma) + "mA"); + } else { + Serial.println("Cannot read current."); + } +} diff --git a/library.properties b/library.properties new file mode 100644 index 0000000..29061ca --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=INA226_asukiaaa +version=1.0.0 +author=Asuki Kono +maintainer=Asuki Kono +sentence=It manages INA226 +paragraph=It can get voltage and current from INA226. +category=Sensors +url=https://github.com/asukiaaa/INA226_asukiaaa +architectures=* diff --git a/src/INA226_asukiaaa.cpp b/src/INA226_asukiaaa.cpp new file mode 100644 index 0000000..c437003 --- /dev/null +++ b/src/INA226_asukiaaa.cpp @@ -0,0 +1,87 @@ +#include "INA226_asukiaaa.h" + +#define INA226_ASUKIAAA_LSB_BUS 1.25 +#define INA226_ASUKIAAA_LSB_POWER 25 + +#define INA226_ASUKIAAA_REGISTER_CONFIG 0x00 +#define INA226_ASUKIAAA_REGISTER_SHUNTV 0x01 +#define INA226_ASUKIAAA_REGISTER_BUSV 0x02 +#define INA226_ASUKIAAA_REGISTER_POWER 0x03 +#define INA226_ASUKIAAA_REGISTER_CURRENT 0x04 +#define INA226_ASUKIAAA_REGISTER_CALIB 0x05 +#define INA226_ASUKIAAA_REGISTER_MASK 0x06 +#define INA226_ASUKIAAA_REGISTER_ALERTL 0x07 +#define INA226_ASUKIAAA_REGISTER_DIE_ID 0xff + +INA226_asukiaaa::INA226_asukiaaa(int address, uint16_t calib, uint16_t config) { + this->address = address; + this->calib = calib; + this->config = config; + wire = nullptr; +} + +void INA226_asukiaaa::setWire(TwoWire* wire) { + this->wire = wire; +} + +uint16_t INA226_asukiaaa::calcCalibByResisterMilliOhm(uint16_t resisterMilliOhm) { + return 5120 / resisterMilliOhm; +} + +int INA226_asukiaaa::begin() { + if (wire == nullptr) { + wire = &Wire; + wire->begin(); + delay(10); + } + int result = write(INA226_ASUKIAAA_REGISTER_CONFIG, config); + if (result != 0) return result; + return write(INA226_ASUKIAAA_REGISTER_CALIB, calib); +} + +int INA226_asukiaaa::readMV(int16_t* volt) { + uint16_t busVoltage; + int result = read(INA226_ASUKIAAA_REGISTER_BUSV, (uint16_t*) &busVoltage); + if (result != 0) return result; + *volt = busVoltage * INA226_ASUKIAAA_LSB_BUS; + return 0; +} + +int INA226_asukiaaa::readMA(int16_t* current) { + return read(INA226_ASUKIAAA_REGISTER_CURRENT, (uint16_t*) current); +} + +int INA226_asukiaaa::readMW(int16_t* watt) { + uint16_t powerVoltage; + int result = read(INA226_ASUKIAAA_REGISTER_POWER, (uint16_t*) &powerVoltage); + if (result != 0) return result; + *watt = powerVoltage * INA226_ASUKIAAA_LSB_POWER; + return 0; +} + +int INA226_asukiaaa::write(uint8_t reg, uint16_t val) { + wire->beginTransmission(address); + wire->write(reg); + wire->write(val >> 8); + wire->write(val & 0xff); + return wire->endTransmission(); +} + +int INA226_asukiaaa::read(uint8_t reg, uint16_t* val) { + uint16_t ret = 0; + // request the registor + wire->beginTransmission(address); + wire->write(reg); + int result = wire->endTransmission(); + if (result != 0) return result; + + // read + wire->requestFrom(address, 2); + + while (wire->available()) { + ret = (ret << 8) | wire->read(); + } + + *val = ret; + return 0; +} diff --git a/src/INA226_asukiaaa.h b/src/INA226_asukiaaa.h new file mode 100644 index 0000000..dc8066b --- /dev/null +++ b/src/INA226_asukiaaa.h @@ -0,0 +1,33 @@ +#ifndef _INA226_ASUKIAAA_H_ +#define _INA226_ASUKIAAA_H_ +#include +#include + +#define INA226_ASUKIAAA_DEFAULT_CONFIG 0x4127 + +class INA226_asukiaaa { + public: + INA226_asukiaaa(int address, uint16_t calib, uint16_t config = INA226_ASUKIAAA_DEFAULT_CONFIG); + // calib is used to calculate current value. + // define your calib by referencing shunt resister. + // calib = 5120 / R(mOhm) + // Ex: 0.002 Ohm -> 2 mOhm -> calib = 5120 / 2 = 2560 + void setWire(TwoWire* wire); + static uint16_t calcCalibByResisterMilliOhm(uint16_t resisterMilliOhm); // MAX 5120 mOhm + + int begin(); + + int readMV(int16_t* volt); + int readMA(int16_t* current); + int readMW(int16_t* watt); + + private: + int address; + uint16_t config; + uint16_t calib; + int write(uint8_t reg, uint16_t val); + int read(uint8_t reg, uint16_t* val); + TwoWire* wire; +}; + +#endif