Hi Wolle.
Excellent library. Probably the most comprehensive that I found. Thank you very much for having written it and sharing it.
I am working on a project and I needed the system to find the shunt offset voltage and the optimal gain settings, so I developed the following (2 functions) that you might find a way to integrate to the INA219_WE library, if you so desire.
The 1st function : It simply requires that the output of the INA219 does not draw any current (open output).
The second function : Establishes the optimal "setPGain()" dynamically (...the function can be called on a regular basis by the application program) in other to take advantage of the optimal sensitivity. Here they are.
First function.
/**************************************************************************************************************************/
void Diagnostics::findINA219OffsetVoltage(){
Serial.println(F("\nFinding the MEAN of of the INA219 sensor and store the correction value"));
float shuntVoltageSmaller = 10.0f; // Make it such that a INA219 value will always be smaller
float shuntVoltageLarger = 0.0f; // Make it such that a INA219 value will always be larger if not 0
float shuntVoltageBuffer = 0.0f;
for(int i = 0; i < 1000; i ++) {
activityWheel();
shuntVoltageBuffer = fp.diagnosticsGetShuntVoltage_mV();
if (shuntVoltageBuffer < shuntVoltageSmaller) {
shuntVoltageSmaller = shuntVoltageBuffer;
}
if (shuntVoltageBuffer > shuntVoltageLarger) {
shuntVoltageLarger = shuntVoltageBuffer;
}
MY_MILLI_DELAY(5); // Same as delay(), but using millis()
}
Serial.print("\bShunt Voltage Smaller : "); Serial.println(shuntVoltageSmaller);
Serial.print("Shunt Voltage Larger : "); Serial.println(shuntVoltageLarger);
shuntVoltageBuffer = (shuntVoltageSmaller + shuntVoltageLarger) /2.0f;
Serial.print("Average of the Smaller and Larger Shunt Voltage Values : "); Serial.println(shuntVoltageBuffer,3);
ds.variousData.INA219ShuntOffsetVoltageCompensation = shuntVoltageBuffer;
Serial.print(F("\b\nShunt OFFSET Voltage MEAN of Smaller/Larger of the INA219 : ")); Serial.println(ds.variousData.INA219ShuntOffsetVoltageCompensation,3);
/******************************************/
// The following method does not give an enough precise result ! (Kept for documentation)
// Serial.println(F("\b \nFinding the AVERAGE of and of the INA219 sensor"));
// shuntVoltageBuffer = 0;
// for (int i = 0; i <500; i++) {
// activityWheel();
// shuntVoltageBuffer += fp.diagnosticsGetShuntVoltage_mV();
// MY_MILLI_DELAY(5);
// }
// Serial.print(F("\b \nShunt OFFSET Voltage AVERAGE of the INA219 : ")); Serial.println((shuntVoltageBuffer) /1000.0f,3);
EEPROM.put(ds.eepromPointers.eepromINA219OffsetVoltageCompensation, ds.variousData.INA219ShuntOffsetVoltageCompensation);
Serial.print("INA219 Shunt Offset Voltage Value from EEPROM : "); Serial.println(EEPROM.get(ds.eepromPointers.eepromINA219OffsetVoltageCompensation, ds.variousData.INA219ShuntOffsetVoltageCompensation),3);
Serial.print("INA219 Shunt Offset Voltage Value from VARIABLE: " ); Serial.print(ds.variousData.INA219ShuntOffsetVoltageCompensation);
}
/**************************************************************************************************************************/
void Diagnostics::activityWheel() {
static uint32_t timer = millis();
static uint8_t counter = 0;
if (millis() - timer >= 200) {
timer = millis();
Serial.print(F("\b"));
if (counter == 0) {
Serial.print(F("|"));
}
else if (counter == 1) {
Serial.print(F("-"));
}
else if (counter == 2) {
Serial.print("\\");
}
else if (counter == 3) {
Serial.print("|");
}
else if (counter == 4) {
Serial.print("/");
}
else if (counter == 5) {
Serial.print(F("-"));
}
counter ++;
if (counter >= 6) {
counter = 0;
}
}
}
/**************************************************************************************************************************/
Second function.
void FunctionProcessing::adjustINA219Gain() { // Function organize in such a way that the gain is not continuously reprogrammed if not needed
if (ds.variousData.currentOutputValue >= 1.600f) { // Between 1.6 A and 3.2 A
if (ds.variousData.INA219PresetGain != ds.INA219Gain::PG320) {
ina219.setPGain(PG_320);
ds.variousData.INA219PresetGain = ds.INA219Gain::PG320;
Serial.println("PG set to PG320");
}
}
else if (ds.variousData.currentOutputValue >= 0.800f) { // Between 0.8 A and 1.6 A
if (ds.variousData.INA219PresetGain != ds.INA219Gain::PG160) {
ina219.setPGain(PG_160);
ds.variousData.INA219PresetGain = ds.INA219Gain::PG160;
Serial.println("PG set to PG160");
}
}
else if (ds.variousData.currentOutputValue >= 0.400f) { // Between 0.4 A and 0.8 A
if (ds.variousData.INA219PresetGain != ds.INA219Gain::PG80) {
ina219.setPGain(PG_80);
ds.variousData.INA219PresetGain = ds.INA219Gain::PG80;
Serial.println("PG set to PG80");
}
}
else if (ds.variousData.currentOutputValue > 0.000f) { // Between 0.0 A and 0.4 A
if (ds.variousData.INA219PresetGain != ds.INA219Gain::PG40) {
ina219.setPGain(PG_40);
ds.variousData.INA219PresetGain = ds.INA219Gain::PG40;
Serial.println("PG set to PG40");
}
}
else if (ds.variousData.currentOutputValue <= 0.000f) { // 0.0 A
if (ds.variousData.INA219PresetGain != ds.INA219Gain::PG40) {
ina219.setPGain(PG_40);
ds.variousData.INA219PresetGain = ds.INA219Gain::PG40;
Serial.println("PG set to PG40"); Serial.println(F("No current detected in the INA219 Sensor. Check VOLTAGE REGULATOR !!!"));
}
}
}
/*****************************************************************/
Finally while in the initialization (setup():
/* The shunt offset voltage is found by the diagnostics function with the voltage regulator removed (see Diagnostics.cpp) */
if (ds.variousData.INA219ShuntOffsetVoltageCompensation > 0.0f) {
ina219.setShuntVoltOffset_mV(ds.variousData.INA219ShuntOffsetVoltageCompensation); // insert the shunt voltage (millivolts) you detect at zero current (i.e INA219 open output)
Regards,
Rene-Jean Mercier