I am building a Arduino based AC power monitor with analogue inputs (A0) and (A1) measuring voltage and current respectively. I need to sample the voltage and current waveforms at a constant rate in order to obtain accurate results when calculating power.
I am aware that the Arduino ADC clock is set to 16MHz/128 = 125KHz. Each conversion takes 13 ADC clocks so 125KHz/13 = 9615Hz. However i have soon discovered this is not an accurate representation of the actual sample rate as it depends on the interval between successive conversions calls, basically the sample rate constantly changes as I add and remove things from the code.
I need a constant sample rate for accurate results when calculating power, i have found the 'blinkwithoutdleay' example online and thought this was along the right tracks however struggled to incorporate into my code. Any ideas what i need to do to obtain a constant sample rate? My code is shown below.
int voltage, current, P, S;
float realPower,IRMS, APower, Average, AvPower, RPower, PF, Sumsqr = 0;
void setup() {
Serial.begin(115200);
}
void loop() {
for(int i=0;i<2000;i++) {
voltage = (analogRead(A1) - 511.5);
current = (analogRead(A0) - 511.5);
P = voltagecurrent;
S = currentcurrent;
realPower=realPower+abs(P);
Sumsqr = Sumsqr+abs(S);
Average = Sumsqr / 2000;
IRMS = (sqrt(Average)/167);
APower = IRMS*230;
AvPower=(realPower/2000);
RPower = AvPower*0.00682635;
PF = (RPower/APower); // Power Factor calculated by doing Real power divided by Apparant Power
delay(0.5);
Serial.println(i);
Serial.print("Real Power: ");Serial.print(RPower);Serial.print(" W ");
Serial.print("Apparant Power: ");Serial.print(APower);Serial.print(" VA ");
Serial.print("Power Factor: ");Serial.println(PF);
}
}