How to program digital filter with ACS712

Hi everybody
I am having an assignment on HALL ACS712 sensor.
I want to calculate the current through the load RL=39. But the result is different from the theory because there is a lot of error. Theoretical current output 0.13A but reality is different.
Can anyone help me redo the low pass filter for the above circuit. The cutoff frequency here is around 1800-2000Hz.
Thanks everyone.
This is circut. RL=39


This is datasheet ACS712

This is code:

const int sensor1 = A0;
int value1=0;
float yn=0;
float yn1=0;
float dt=0.02;
float fc=2000.0;
float alpha=0;
float vq=2.52;
void setup()
{
  Serial.begin(115200);
  float k = 2*3.14*dt*fc;
  alpha = k/(k+1);  
}
void loop()
{
  value1  = analogRead(sensor1);
  yn = alpha*value1 + (1-alpha) *yn1 ;
  yn1 = yn;
  float vol = 5.03*(yn/1024.0);
  float I=((vol-vq)*1000.0)/185.0;##S=185mV/A
  Serial.println(I,4);
  //Current results are different from theory I=5/39=0.13A.
}

Hello Vicky

Use a logic analyzer to see what happens.

Insert Serial.println()´s at points of interrest and analyze the test results.

Have a nice day and enjoy coding in C++.

Thank you

Those sensors are inaccurate at low currents. 0.13A is only 2% of the full scale current, similar to the accuracy of the scale factor.

As you can see from the data sheet, the scale factor can be anywhere from 180 to 190, a 5% variation! So, you need to determine the best scale factor for that current range.

Oh! Sorry. But Read line from arduino

Error greater than 50%

Yes, in addition to the accuracy problem, you have a sensor noise problem. They are unrelated.

For accurate results, the sensor has to be recalibrated. No filter can correct systematic errors.

Simply average 100 or 1000 measurements to get mean values, and calibrate the sensor using those values.

Thank you for sharing. If I want to adjust the alpha coefficient of the filter, do you think it can be done

The alpha coefficient can be changed to anything you wish.

I tried but nothing good

If you want forum members to help, you need to clearly explain what should happen, and what happens instead.

"nothing good" means only that you are unhappy with the result.

alpha as you calculate in the code is 0.996.

This would make a low pass filter converge v e r y s l o o o o w l y.

What value have you tried instead?

What theory or observation informed the value you calculated in the original code?

a7