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)