Acs712 current sensor zero point problem

I am using Acs712 sensor to measure Ac current. But the problem is I have to adjust it's zero point after every few seconds (about 50 seconds sometimes later).

The second problem is :

I am using 5V relays. so that whenever current value crosses the limit, the relay should operate. But the problem is whenever the relay operates, the sensor values automatically get change.

I am using this code to measure the current.

current.ino (1.03 KB)

What makes you believe the zero point is changing? Have you thought of digitally high-pass filtering
the the readings to eliminate the DC offset automatically?

Your code is only measuring the maximum value, not the true rms value, so will be very sensitive to
spikes/noise/interference.

Could you provide schematic for your ACS712 connection?

MarkT:
What makes you believe the zero point is changing? Have you thought of digitally high-pass filtering
the the readings to eliminate the DC offset automatically?

Your code is only measuring the maximum value, not the true rms value, so will be very sensitive to
spikes/noise/interference.

#MarkT yes it keeps changing. I am using a module. So do you still think I should use Filter Circuit?
And I am measuring RMS value. You can look in the code again.

Can you tell me which DC pass filter should I use? (Specifications of the components )
Here is the link of that module which I am using.

https://www.aliexpress.com/item/NEW-20A-Hall-Current-Sensor-Module-ACS712-model-20A-ACS712-20A/32313281754.html

alesam:
Could you provide schematic for your ACS712 connection?

#alesma I am using a circuit almost like this.

with just a little change that I am using 220v Ac supply and there is no switch.

Boyssuper93:
#MarkT yes it keeps changing. I am using a module. So do you still think I should use Filter Circuit?
And I am measuring RMS value. You can look in the code again.

You are measuring the peak value and dividing by 1.414, that is not measuring the RMS value.

To measure the RMS value you have to take the mean of the squares of many samples and
square-root it. root-mean-square...

The common way of measuring AC current with the ACS712 is to use two variables.
One set to 0 and one set to 1023.
Then update the 0 variable with maximum measurements, and the 1023 variable with minimum measurements.
That way both variables hold the same value (~512) without current, and that subtracts to 0Amp without drift.
Peak/peak (and RMS) current also can be calculated from that difference.
Lots of code out there that uses this principle.
Leo..

The problem with measuring peak values is its very sensitive to noise spikes, and give false values
if there are spikes.

Measuring true rms isn't difficult, and it will be less sensitive to spikes.

Sum the samples, divide by the number of samples, gives the mean.
Sum the squares of the samples, divide by the number of samples, then subtract the
square of the mean. This gives the mean-square.
Take square root of mean-square.

int N = ??;  /* the number of samples taken */
long sum = 0;
long sum_squares = 0;

float calc_rms()
{
  for (int i = 0 ; i < N ; i++)
  {
    sum += sample[i];
    sum_squares += ((long)sample[i])*sample[i] ; // careful not to overflow
  }
  float mean = 1.0 * sum / N ;
  float mean_square = 1.0 * sum_squares / N - mean*mean ;
  float rms = sqrt(mean_square);
  // now convert from samples to actual voltage
  return rms * 5.0 / 1024;
}

You can do the summing on the fly too of course, you don't need to store the individual samples.
Note use of long to prevent overflow (default on many Arduinos is 16 bit integers, the
squaring must be 32 bit).

void do_summing(int sample)
{
  N ++ ;
  sum += sample ;
  sum_squares += ((long)sample)*sample ;
}