ina219 data smoothing with AnalogSmooth possible?

hello,

I want to smooth current reading of ina219 module, but I get “too high” value, when I apply this library.
When no module is connected, I have cuurent value of -0.5mA, when I add smooth, it floats from 400 to 300 mA.
Maybe someone can point me to the right direction.

#include <AnalogSmooth.h>
#include <Wire.h>
#include <Adafruit_INA219.h>

Adafruit_INA219 ina219;

AnalogSmooth as = AnalogSmooth();

// Window size can range from 1 - 100
AnalogSmooth as40 = AnalogSmooth(40);


void setup() {
Serial.begin(9600);
delay(300); 


 uint32_t currentFrequency;
    
  Serial.println("Hello!");
  
  // Initialize the INA219.
  // By default the initialization will use the largest range (32V, 2A).  However
  // you can call a setCalibration function to change this range (see comments).
  ina219.begin();
  // To use a slightly lower 32V, 1A range (higher precision on amps):
  //ina219.setCalibration_32V_1A();
  // Or to use a lower 16V, 400mA range (higher precision on volts and amps):
  
  ina219.setCalibration_16V_400mA();

  Serial.println("Measuring voltage and current with INA219 ...");
}
void loop()
{
    float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

  shuntvoltage = ina219.getShuntVoltage_mV();
  busvoltage = ina219.getBusVoltage_V();
  current_mA = as.analogReadSmooth(ina219.getCurrent_mA());
  power_mW = ina219.getPower_mW();
  loadvoltage = busvoltage + (shuntvoltage / 1000);
  
  Serial.print("Bus Voltage:   "); Serial.print(busvoltage); Serial.println(" V");
  Serial.print("Shunt Voltage: "); Serial.print(shuntvoltage); Serial.println(" mV");
  Serial.print("Load Voltage:  "); Serial.print(loadvoltage); Serial.println(" V");
  Serial.print("Current:       "); Serial.print(current_mA); Serial.println(" mA");
  Serial.print("Power:         "); Serial.print(power_mW); Serial.println(" mW");
  Serial.println("");

  delay(2000);

When no module is connected, I have cuurent value of -0.5mA,

When no module is connected nothing will work correctly. Never expect any system to function correctly with floating inputs as they are called.

float shuntvoltage = 0;
  float busvoltage = 0;
  float current_mA = 0;
  float loadvoltage = 0;
  float power_mW = 0;

This means that all those values get reset on each loop iteration. Is that what you want?

I see ina219 supports averaging, then I don't need to use additional function.

How to configure the sample rate when using Adafruit_INA219.h library?

current_mA = as.analogReadSmooth(ina219.getCurrent_mA()); // seems to try to read the analogue pin
Which is wrong, because the INA219 is an I2C device.

current_mA = as40.smooth(ina219.getCurrent_mA()); // might do what you think you need

Leo..

Wawa, your tip has worked.

I see ina219 has a lot of configurations, hope to get well calibrated results as the first try with acs712 was very noisy.