-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7042e70
Showing
5 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
25 changes: 25 additions & 0 deletions
25
examples/printVoltageAndCurrent/printVoltageAndCurrent.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include <INA226_asukiaaa.h> | ||
#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."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef _INA226_ASUKIAAA_H_ | ||
#define _INA226_ASUKIAAA_H_ | ||
#include <Arduino.h> | ||
#include <Wire.h> | ||
|
||
#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 |