Closed RobTillaart closed 1 year ago
Core formula is : sum += sqrt(sumSquared / samples);
Assuming that both halves are equal, sampling for half a period means sum += sqrt((sumSquared/2) / (samples/2));
These formulas are equal, so sampling half a period gives same value.
The only difference is that sampling for whole period can have a positive effect on cancelling noise, so it might be more accurate.
A test with a simulated AC signal of 50 Hz gave the following output.
ACS712_LIB_VERSION: 0.3.6
MidPoint: 507. Noise mV: 21
mA: 13974. Form factor: 0.71 time: 10288
mA: 13860. Form factor: 0.71 time: 10136
mA: 13747. Form factor: 0.71 time: 10136
mA: 13957. Form factor: 0.71 time: 10168
ACS712_LIB_VERSION: 0.3.6
MidPoint: 507. Noise mV: 21
mA: 13851. Form factor: 0.71 time: 20280
mA: 13847. Form factor: 0.71 time: 20212
mA: 13835. Form factor: 0.71 time: 20228
mA: 13802. Form factor: 0.71 time: 20268
mA: 13765. Form factor: 0.71 time: 20240
mA: 13747. Form factor: 0.71 time: 20292
Some more playing resulted in this trick. (to be added to readme.md)
A trick to sample faster is to set the frequency to 2 times the actual frequency 100 or 120 Hz. This results in sampling half a period, and on average the same current will be measured, while the function only blocks for ~10 ms @ 50Hz (8.5 @ 60Hz). The drawback is that there is about 4x as many variation, but performance might be needed.
In a similar way one can increase the accuracy by setting the frequency a factor 2 lower. Drawback is a far longer blocking time.
Use with care!
Added the sampling trick to the readme.md of 0.3.7 Will not be a specific function or so. Close this issue for now.
Current implementation of mA_AC_sampling() takes ~20 ms per call (50 Hz assumed) as it samples a whole period.
If the wave is symmetric (fair assumption for AC) code could sample half the period and double that value. Should reduce the duration to ~10 ms (50 Hz). For 60 Hz it would save about ~8.4 ms.
As the sampling period is shorter, there is more time for other tasks.
Might also work for some other AC functions.