Integrating INA 226 Module
The INA226 is a voltage and current measurement device with with I2C Bus

It can handle voltages up to 36 Volts, and currents up to 20 Amperes.
To connect and process data from the module I used an existing library: https://github.com/generationmake/stromsensor6mm
Example file:
/*************************************************************************** Example sketch for the INA226_WE library This sketch is based on the continuous mode example but uses the function setResistorRange to set a different resistor value. This setup uses a stromsensor6mm board with a 5 mOhm shunt resistor More information on this board can be found here: https://github.com/generationmake/stromsensor6mm More information on the INA226_WE library: https://wolles-elektronikkiste.de/en/ina226-current-and-power-sensor (English) https://wolles-elektronikkiste.de/ina226 (German) ***************************************************************************/ #include <Wire.h> #include <INA226_WE.h> #define I2C_ADDRESS 0x40 /* There are several ways to create your INA226 object: INA226_WE ina226 = INA226_WE() -> uses Wire / I2C Address = 0x40 INA226_WE ina226 = INA226_WE(ICM20948_ADDR) -> uses Wire / I2C_ADDRESS INA226_WE ina226 = INA226_WE(&wire2) -> uses the TwoWire object wire2 / I2C_ADDRESS INA226_WE ina226 = INA226_WE(&wire2, I2C_ADDRESS) -> all together Successfully tested with two I2C busses on an ESP32 */ INA226_WE ina226 = INA226_WE(I2C_ADDRESS); void setup() { Serial.begin(115200); while (!Serial); // wait until serial comes up on Arduino Leonardo or MKR WiFi 1010 Wire.begin(); ina226.init(); /* Set Number of measurements for shunt and bus voltage which shall be averaged Mode * * Number of samples AVERAGE_1 1 (default) AVERAGE_4 4 AVERAGE_16 16 AVERAGE_64 64 AVERAGE_128 128 AVERAGE_256 256 AVERAGE_512 512 AVERAGE_1024 1024 */ ina226.setAverage(AVERAGE_16); // choose mode and uncomment for change of default /* Set conversion time in microseconds One set of shunt and bus voltage conversion will take: number of samples to be averaged x conversion time x 2 Mode * * conversion time CONV_TIME_140 140 µs CONV_TIME_204 204 µs CONV_TIME_332 332 µs CONV_TIME_588 588 µs CONV_TIME_1100 1.1 ms (default) CONV_TIME_2116 2.116 ms CONV_TIME_4156 4.156 ms CONV_TIME_8244 8.244 ms */ ina226.setConversionTime(CONV_TIME_1100); //choose conversion time and uncomment for change of default /* Set measure mode POWER_DOWN - INA226 switched off TRIGGERED - measurement on demand CONTINUOUS - continuous measurements (default) */ ina226.setMeasureMode(CONTINUOUS); // choose mode and uncomment for change of default /* Set Resistor and Current Range resistor is 5.0 mOhm current range is up to 10.0 A default was 100 mOhm and about 1.3 A */ ina226.setResistorRange(0.002, 20.0); // choose resistor 5 mOhm and gain range up to 10 A /* If the current values delivered by the INA226 differ by a constant factor from values obtained with calibrated equipment you can define a correction factor. Correction factor = current delivered from calibrated equipment / current delivered by INA226 */ ina226.setCorrectionFactor(0.95); Serial.println("INA226 Current Sensor Example Sketch - Continuous"); } void loop() { float shuntVoltage_mV = 0.0; float loadVoltage_V = 0.0; float busVoltage_V = 0.0; float current_mA = 0.0; float power_mW = 0.0; ina226.readAndClearFlags(); shuntVoltage_mV = ina226.getShuntVoltage_mV(); busVoltage_V = ina226.getBusVoltage_V(); current_mA = ina226.getCurrent_mA(); power_mW = ina226.getBusPower(); loadVoltage_V = busVoltage_V + (shuntVoltage_mV / 1000); Serial.print("Shunt Voltage [mV]: "); Serial.println(shuntVoltage_mV); Serial.print("Bus Voltage [V]: "); Serial.println(busVoltage_V); Serial.print("Load Voltage [V]: "); Serial.println(loadVoltage_V); Serial.print("Current[mA]: "); Serial.println(current_mA); Serial.print("Bus Power [mW]: "); Serial.println(power_mW); if (!ina226.overflow) { Serial.println("Values OK - no overflow"); } else { Serial.println("Overflow! Choose higher current range"); } Serial.println(); delay(3000); }
Console Output

Calibration
Calibration

Integration into main script (snippet)
//***************************************************************************/ //INA 226 Integration #include <Wire.h> #include <INA226_WE.h> INA226_WE ina226 = INA226_WE(0x40); int voltage_28V_Line; int amperage_28V_Line; void setup() { Serial.begin(115200); while (!Serial); // wait until serial comes up on Arduino Leonardo or MKR WiFi 1010 Wire.begin(); ina226.init(); ina226.setAverage(AVERAGE_128); ina226.setConversionTime(CONV_TIME_1100); ina226.setMeasureMode(CONTINUOUS); ina226.setResistorRange(0.002, 20.0); ina226.setCorrectionFactor(1.031); // defined through calibration Serial.println("INA226 Current Sensor Example Sketch - Continuous"); } void loop() { float shuntVoltage_mV = 0.0; float loadVoltage_V = 0.0; float busVoltage_V = 0.0; ina226.readAndClearFlags(); shuntVoltage_mV = ina226.getShuntVoltage_mV(); if (!ina226.overflow) { busVoltage_V = ina226.getBusVoltage_V(); amperage_28V_Line = ina226.getCurrent_mA()/100; voltage_28V_Line = (busVoltage_V + (shuntVoltage_mV / 1000))*10; Serial.print("10 x 28V Line Voltage [V]: "); Serial.println(voltage_28V_Line); Serial.print("10 x 28V Line Current[A] : "); Serial.println(amperage_28V_Line); } Serial.println(); delay(3000); }