Below is my circuit diagram for ACS712 5A current sensor using peak detector circuit.
Currently I facing 2 problems.
i)zero cross dc voltage keep changing
ii)The current scale is non linear and not accurate
For problem (i), I verified by measuring the voltage from VIOUT every time power on tend to be different even the input voltage stay almost the same(5.01V to 4.95V), I be getting output lets say in the range (2.57V to 2.43V). I replaced a new current sensor and got almost the same result. Each time after taking load current measurement, I turned off supply to the load and recheck the VIout, it tend to be different from the voltage during power on which mean the zero cross voltage changed. I'm using a 4 channel relay module and aware relay module can be power consuming so I using dual supply, one for relay module and another one for microcontroller, reference voltage and Opamp. As a result, the input voltage feeding to microcontroller, voltage reference and Opamp voltage is more stable as compare to using single supply to power all unit but that does not solve my zero cross dc voltage shifting issue. Below picture shown the relay module I currently using.
For problem(ii) my peak detector voltage does not have much changes but the measured current can be quite different? It does not scale linearly according to the 0.185mV/A formula.In term of accuracy, it is bad but reading stability is good.
Below are the pseudocode I use to calculate the current.
pseudocode
{ void init()
__delay_ms(8000); \\ delay 8 sec on each power on for the capacitor to reach stable value
InitialCurrent=ADRead(1); \\store this startup reading for calculation since VIOUT can be different on each power on
void Current()
{
PeakCurrent=0;
AveragePeakCurrent=0;
for(int u=0;u<300;u++)
{
PeakCurrent=ADRead(1);
AveragePeakCurrent=AveragePeakCurrent+PeakCurrent;
}
AveragePeakCurrent=(AveragePeakCurrent/300);
PeakCurrent=((AveragePeakCurrent-InitialCurrent)*0.009775)*4.2;
\\initialcurrent=(ADC reading)*5/1023*2(due to voltage divider),AveragePeakCurrent=(ADC reading)*5/1023*2
\\lastly, the differeces is times by (1/0.185)*0.7071=3.822 but i put 4.2 to offset it to match a load measured at around 1A current but it scale non linear when measure load with different current,example 0.3A,0.8A.
PeakCurrent=fabs(PeakCurrent);
}