INA219 Shunt Module Erratic Readings PROBLEM SOLVED!

I recently built an LCD Current Meter with a INA219 Shunt Module with a sketch from GitHub. INA219_WE/examples at master · wollewald/INA219_WE · GitHub

The CONTINUOUS MODE sketch works perfect with resistive loads, but the current reading jumps around when the load is an active circuit like my metal detector.

Can anyone sugest a way to smooth or average this current reading?
current_mA = ina219.getCurrent_mA();
I tried the usual For Loop routine, EG: "for (int i = 0; i < avgSamples; i++)", but could not get that to work!!
Thanks, Mac VE7DEP


Now working with a Pro-Mini in LCD Case:

PROBLEM SOLVED! I did finally get the Averaging code to work.
int avgSamples = 2000; // Added for averaging
void loop() {
ina219.startSingleMeasurement();
for (int i = 0; i < avgSamples; i++) {
current_mA = current_mA + ina219.getCurrent_mA();
}
current_mA = current_mA / avgSamples;
Thanks Everyone. Mac VE7DEP

Hello OM
take a look here to find a filter function.
73

It sounds like it is working just right and the load is constantly changing. You can always slow down your sample rate.

You are correct gilshultz. The current is changing at the pulse rate of the detector's TX circuit even with a 4700uf buffer cap installed.

I did try delaying the readings, but even at 3 seconds if the display jumps up to an invalid reading, it stays displayed for the delay period which is irritating. It does display the correct reading most of the time.

Thanks for the information Paul.
I think your Exponential Moving Average Algorithm would solve my problem but with my limited coding experience I'm not sure how to add it to the sketch.

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;
  bool ina219_overflow = false;

  ina219.startSingleMeasurement(); // triggers single-shot measurement and waits until completed
  shuntVoltage_mV = ina219.getShuntVoltage_mV();
  busVoltage_V = ina219.getBusVoltage_V();
  current_mA = ina219.getCurrent_mA();
  power_mW = ina219.getBusPower();
  loadVoltage_V  = busVoltage_V + (shuntVoltage_mV / 1000);
  ina219_overflow = ina219.getOverflow();

  lcd.setCursor(1, 0);
  //lcd.print(current_mA);
  lcd.print(round(current_mA));  // Rounds off to whole number
  lcd.setCursor(6, 0);
  lcd.print(" MilliAmps  ");
  lcd.setCursor(0, 1);
  lcd.print(" MAX:1600mA +16V");
  delay(3000);  // New reading every 3 seconds.

Mac VE7DEP

YOUR EMA CODE:

//Global Variables

  • int sensorPin = 0; //pin number to use the ADC*
  • int sensorValue = 0; //initialization of sensor variable, equivalent to EMA Y*
  • float EMA_a = 0.6; //initialization of EMA alpha*
  • int EMA_S = 0; //initialization of EMA S*

void setup(){

  • Serial.begin(115200); //setup of Serial module, 115200 bits/second*
  • EMA_S = analogRead(sensorPin); //set EMA S for t=1*
    }

void loop(){

  • sensorValue = analogRead(sensorPin); //read the sensor value using ADC*
  • EMA_S = (EMA_a*sensorValue) + ((1-EMA_a)EMA_S); //run the EMA
  • Serial.println(EMA_S); //print digital value to serial*
  • delay(50); //50ms delay*
    }

Thanks for letting us know. Have a great fun filled year!

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.