Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
INA226 Bi-directional Current/Power Monitor. Simple Example.
Read more: http://www.jarzebski.pl/arduino/czujniki-i-sensory/cyfrowy-czujnik-pradu-mocy-ina226.html
GIT: https://github.com/jarzebski/Arduino-INA226
Web: http://www.jarzebski.pl
(c) 2014 by Korneliusz Jarzebski
*/
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
#include <INA226.h>
#include <Wire.h>
const IPAddress WIFI_IP = { 192, 168, 0, 233 };
const IPAddress WIFI_GATEWAY = { 192, 168, 0, 23 };
const IPAddress WIFI_SUBNET = { 255, 255, 255, 0 };
const IPAddress WIFI_DNS = { 192, 168, 0, 23 };
const IPAddress WIFI_DNS_SECONDARY = { 8, 8, 8, 8 };
#define SSID "NSA"
#define PASSWORD "NSA"
INA226 ina;
void checkConfig()
{
Serial.print("Mode: ");
switch (ina.getMode()) {
case INA226_MODE_POWER_DOWN:
Serial.println("Power-Down");
break;
case INA226_MODE_SHUNT_TRIG:
Serial.println("Shunt Voltage, Triggered");
break;
case INA226_MODE_BUS_TRIG:
Serial.println("Bus Voltage, Triggered");
break;
case INA226_MODE_SHUNT_BUS_TRIG:
Serial.println("Shunt and Bus, Triggered");
break;
case INA226_MODE_ADC_OFF:
Serial.println("ADC Off");
break;
case INA226_MODE_SHUNT_CONT:
Serial.println("Shunt Voltage, Continuous");
break;
case INA226_MODE_BUS_CONT:
Serial.println("Bus Voltage, Continuous");
break;
case INA226_MODE_SHUNT_BUS_CONT:
Serial.println("Shunt and Bus, Continuous");
break;
default:
Serial.println("unknown");
}
Serial.print("Samples average: ");
switch (ina.getAverages()) {
case INA226_AVERAGES_1:
Serial.println("1 sample");
break;
case INA226_AVERAGES_4:
Serial.println("4 samples");
break;
case INA226_AVERAGES_16:
Serial.println("16 samples");
break;
case INA226_AVERAGES_64:
Serial.println("64 samples");
break;
case INA226_AVERAGES_128:
Serial.println("128 samples");
break;
case INA226_AVERAGES_256:
Serial.println("256 samples");
break;
case INA226_AVERAGES_512:
Serial.println("512 samples");
break;
case INA226_AVERAGES_1024:
Serial.println("1024 samples");
break;
default:
Serial.println("unknown");
}
Serial.print("Bus conversion time: ");
switch (ina.getBusConversionTime()) {
case INA226_BUS_CONV_TIME_140US:
Serial.println("140uS");
break;
case INA226_BUS_CONV_TIME_204US:
Serial.println("204uS");
break;
case INA226_BUS_CONV_TIME_332US:
Serial.println("332uS");
break;
case INA226_BUS_CONV_TIME_588US:
Serial.println("558uS");
break;
case INA226_BUS_CONV_TIME_1100US:
Serial.println("1.100ms");
break;
case INA226_BUS_CONV_TIME_2116US:
Serial.println("2.116ms");
break;
case INA226_BUS_CONV_TIME_4156US:
Serial.println("4.156ms");
break;
case INA226_BUS_CONV_TIME_8244US:
Serial.println("8.244ms");
break;
default:
Serial.println("unknown");
}
Serial.print("Shunt conversion time: ");
switch (ina.getShuntConversionTime()) {
case INA226_SHUNT_CONV_TIME_140US:
Serial.println("140uS");
break;
case INA226_SHUNT_CONV_TIME_204US:
Serial.println("204uS");
break;
case INA226_SHUNT_CONV_TIME_332US:
Serial.println("332uS");
break;
case INA226_SHUNT_CONV_TIME_588US:
Serial.println("558uS");
break;
case INA226_SHUNT_CONV_TIME_1100US:
Serial.println("1.100ms");
break;
case INA226_SHUNT_CONV_TIME_2116US:
Serial.println("2.116ms");
break;
case INA226_SHUNT_CONV_TIME_4156US:
Serial.println("4.156ms");
break;
case INA226_SHUNT_CONV_TIME_8244US:
Serial.println("8.244ms");
break;
default:
Serial.println("unknown");
}
Serial.print("Max possible current: ");
Serial.print(ina.getMaxPossibleCurrent());
Serial.println(" A");
Serial.print("Max current: ");
Serial.print(ina.getMaxCurrent());
Serial.println(" A");
Serial.print("Max shunt voltage: ");
Serial.print(ina.getMaxShuntVoltage());
Serial.println(" V");
Serial.print("Max power: ");
Serial.print(ina.getMaxPower());
Serial.println(" W");
}
void setup()
{
Serial.begin(115200);
Serial.println("Initialize INA226");
Serial.println("-----------------------------------------------");
// Default INA226 address is 0x40
ina.begin();
// Configure INA226
ina.configure(INA226_AVERAGES_1, INA226_BUS_CONV_TIME_1100US, INA226_SHUNT_CONV_TIME_1100US, INA226_MODE_SHUNT_BUS_CONT);
// Calibrate INA226. Rshunt = 0.01 ohm, Max excepted current = 4A
// ina.calibrate(0.01, 4);
// ina.calibrate(0.01, 0.4);
// ina.calibrate(0.47, 1);
ina.calibrate(4.7, 0.5);
// ina.calibrate(0.047, 1);
// Display configuration
checkConfig();
Serial.println("-----------------------------------------------");
WiFi.hostname("HOSTNAME"); // DHCP Hostname (useful for finding device for static lease)
WiFi.config(WIFI_IP, WIFI_SUBNET, WIFI_GATEWAY, WIFI_DNS);
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PASSWORD);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(50);
}
ArduinoOTA.setHostname("INA226_OTA");
ArduinoOTA.setPassword("");
ArduinoOTA.begin();
}
unsigned long lastMeasureMs;
void loop()
{
if (millis() - lastMeasureMs > 999)
{
Serial.print("Bus voltage: ");
Serial.print(ina.readBusVoltage(), 5);
Serial.println(" V");
Serial.print("Bus power: ");
Serial.print(ina.readBusPower(), 5);
Serial.println(" W");
Serial.print("Shunt voltage: ");
Serial.print(ina.readShuntVoltage() * 1000, 5);
Serial.println(" mV");
Serial.print("Shunt current: ");
Serial.print(ina.readShuntCurrent() * 1000, 5);
Serial.println(" mA");
Serial.println("");
lastMeasureMs = millis();
}
ArduinoOTA.handle();
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment