I'm trying to track down a clearly/simply articulated piece of code to take a reading from the ACS712 current sensor (link to download PDF data sheet).
I understand that these sensors are noisy, and that reading AC current is quite different to DC current.
After looking at a range of examples in a variety of forums etc., I've based the following on http://henrysbench.capnfatz.com/henrys-bench/acs712-arduino-ac-current-tutorial/
void ac_read() {
int rVal = 0;
int maxVal = 0;
int minVal = 1023;
int sampleDuration = 100; // 100ms
uint32_t startTime = millis();
// take samples for 100ms
while((millis()-startTime) < sampleDuration)
{
rVal = analogRead(A0);
if (rVal > maxVal)
maxVal = rVal;
if (rVal < minVal)
minVal = rVal;
}
// Subtract min from max to determine the peak to peak range
// 1023 = the max value we'll get on the input (1024, zero indexed)
// 5.0 = 5v total on adc input
double volt = ((maxVal - minVal) * (5.0/1023.0));
// div by 2 is to calculate RMS from peak to peak
// 0.35355 is factor to calculate RMS from peak to peak
// see http://www.learningaboutelectronics.com/Articles/Voltage-rms-calculator.php
double voltRMS = volt * 0.35355;
// x 1000 to convert volts to millivolts
// divide by the number of millivolts per amp to determine amps measured
// the 20A module 100 mv/A (so in this case ampsRMS = voltRMS
double ampsRMS = (voltRMS * 1000)/100;
Serial.println(ampsRMS);
}
I unfortunately don't have sufficient test equipment at present to validate this, but I'm getting a value of around 8.9 for a 2300W device, which afaict is roughly accurate (based on this calculator http://www.rapidtables.com/calc/electric/Watt_to_Amp_Calculator.htm).
Is anyone able to confirm the above makes sense/is logical/sane? Is there anything obviously incorrect?