ACS712-5 Amp current sennsor code

I tried my best to optimize this code ,but at low currents the readings become messy(for ex at 0A -open circuit it shows 80~70 mA of current).Any fix or it's the maximum accuracy for 10 bit adc.
#include<math.h>
const float sens=0.185;//(V/A)
const byte in = 2;

void setup() {
pinMode(in, INPUT);
Serial.begin(9600);
}

void loop() {
float s=0;
for(byte i=1;i<=100;i++)
{int x = analogRead(in);
delay(10);
s=s+x;}
s=s/100;

Serial.println(adc_to_amps(s,sens),3);
delay(300);
}
float adc_to_amps(int x,float sens){
float y=float(x)-512;
if(y==0)
return 0;
else
{y=0.00488281*y/sens;
if(abs(y)<0.08)
return 0;
else return y;
}

}

amp.ino (488 Bytes)

Firstly you are limited to a resolution of about 30mA by the 10 bit ADC and the V/A figure,
secondly these sensors are noisy and have offset error - you should calibrate in setup() before
the current is applied to give a better zero value.

Make sure no strong magnets are anywhere near the sensor, its simply measuring magnetic
field so any background field will add to the result.

Thank you.
In the datasheet it specifies a filtercap so i'm going to increase it's value and see (and i'll implement your sugestion about calibration. :slight_smile: ).