I've been playing around with this sensor but the bus voltage readings are reading correctly.
I've tried the 2 libraries but still cannot get the correct reading. The code below is using library by RobTillaart here
https://github.com/RobTillaart/INA228
The voltage reading displaying on my meter is 12.56 and the INA228 busvoltage is showing 11.96V ?
Not sure if or how this can be calibrated considering the ADC is 20 Bits.
#include <TimedAction.h>
#include <Wire.h>
#include "M5Stack.h"
//#include <Adafruit_INA228.h>
//Adafruit_INA228 ina228 = Adafruit_INA228();
#include "INA228.h"
INA228 INA(0x40);
float value = 0;
float Bus_voltage = 0;
int bright[4] = {30, 60, 100, 200};
int b = 1;
bool d = 1;
void TimerService01();// Timer service routinte
TimedAction Timedact01 = TimedAction(80, TimerService01);// Timer1 for ADC'S, called every 80 Ms seconds
void TimerService02();// Timer service routinte
TimedAction Timedact02 = TimedAction(150, TimerService02);// Timer2 for LCD display, called every 80 Ms seconds
void setup(void) {
M5.begin();
M5.Power.begin();
M5.Lcd.setSwapBytes(true);
M5.Lcd.setBrightness(bright[b]);
Wire.begin();
// if (!ina228.begin()) {
// Serial.println("Couldn't find INA228 chip");
// while (1)
// ;
//}
Serial.println("Found INA228 chip");
// we need to set the resistance (default 0.1 ohm) and our max expected
// current (no greater than 3.2A)
if (!INA.begin() )
{
Serial.println("Could not connect. Fix and Reboot");
while (1);
}
INA.setMaxCurrentShunt(10, 0.015);
INA.setAccumulation(1); // clear registers?
// INA.setADCRange(1.23);
/*
ina228.setShunt(0.016,10);
// ina228.setAveragingCount(4);
INA.setAveragingCount(INA228_COUNT_128);
uint16_t counts[] = {1, 4, 16, 64, 128, 256, 512, 1024};
Serial.print("Averaging counts: ");
Serial.println(counts[ina228.getAveragingCount()]);
// set the time over which to measure the current and bus voltage
ina228.setVoltageConversionTime(INA228_TIME_150_us);
Serial.print("Voltage conversion time: ");
switch (ina228.getVoltageConversionTime()) {
case INA228_TIME_50_us:
Serial.print("50");
break;
case INA228_TIME_84_us:
Serial.print("84");
break;
case INA228_TIME_150_us:
Serial.print("150");
break;
case INA228_TIME_280_us:
Serial.print("280");
break;
case INA228_TIME_540_us:
Serial.print("540");
break;
case INA228_TIME_1052_us:
Serial.print("1052");
break;
case INA228_TIME_2074_us:
Serial.print("2074");
break;
case INA228_TIME_4120_us:
Serial.print("4120");
break;
}
Serial.println(" uS");
ina228.setCurrentConversionTime(INA228_TIME_540_us);
Serial.print("Current conversion time: ");
switch (ina228.getCurrentConversionTime()) {
case INA228_TIME_50_us:
Serial.print("50");
break;
case INA228_TIME_84_us:
Serial.print("84");
break;
case INA228_TIME_150_us:
Serial.print("150");
break;
case INA228_TIME_280_us:
Serial.print("280");
break;
case INA228_TIME_540_us:
Serial.print("540");
break;
case INA228_TIME_1052_us:
Serial.print("1052");
break;
case INA228_TIME_2074_us:
Serial.print("2074");
break;
case INA228_TIME_4120_us:
Serial.print("4120");
break;
}
Serial.println(" uS");
// default polarity for the alert is low on ready, but
// it can be inverted!
// ina228.setAlertPolarity(1);
*/
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setTextFont(2);
int start = 16;
}
void loop(void) {
M5.update();
Timedact01.check(); // Reads the ADC
Timedact02.check(); // Display the values
if (M5.BtnA.wasPressed())
d = !d;
}
void TimerService01() {// Read the values for the INA228
// value = ina228.readCurrent()/1000;
// Bus_voltage = ina228.readBusVoltage();
// Bus_voltage = Bus_voltage/10000;
value = INA.getCurrent_mA() / 1000;
Bus_voltage = INA.getBusVoltage();
}
void TimerService02() {// Display the values
M5.Lcd.setTextColor(WHITE, BLACK);
M5.Lcd.setCursor(10, 40);
M5.Lcd.setTextFont(7);
if (d == 0)
M5.Lcd.printf("%.2f \r\n", (value));
if (d == 1)
M5.Lcd.printf("%.3f \r\n", (value));
M5.Lcd.setCursor(10, 90);
M5.Lcd.printf("%.2f \r\n", (Bus_voltage));
}