# Sampling an ac waveform on microcontroller

1. Aug 15, 2016

### cnh1995

I did a small project on ac power measurement using arduino a few months ago. But it was specifically for 50Hz supply only. I sampled the waveform in a 20ms interval and applied DFT to get the amplitude and phase. This works good when the frequency is between 49.5 Hz to 50.5 Hz. The aim was to demonstrate how dft can be used for measurement of the ac power, so we didn't care much about the frequency. I can use IC LM331 in frequency to voltage converter mode and get the frequency first and set the time for sampling accordingly. This will work for any frequency.
But is it possible to calculate the frequency, amplitude and phase if I just sampled 100 samples with a sampling frequency fs? Say a 50Hz sinusoidal waveform is applied to the ADC and I took 100 samples of the signal in 30ms, which gives a sampling frequency of 3.33k samples/sec. Is it possible to get the fundamental frequency, amplitude and phase using these 100 samples?

2. Aug 15, 2016

### Hesch

Assuming that you "know" it i s a sine wave you are sampling, the signal can be expressed:

Vn = A * sin( n*Δt * ω + φ ) + DCoffset ( Δt is the sampling period, n is the sample number )

You have 4 unknown which can be determined by 4 samples. So "just" make a program, adjusting the 4 unknown values, so that there will be a match between measured and theoretical values.

I will recommend that you spread out the 4 samples ( n = 1, 15, 35, 69 ) to achieve precision.

Hints: The time between two zero crossings will indicate the signal period.
The signal value of the sample in the middle of the crossings will indicate the amplitude.
In the same way you can guess φ.
Now fine tune.

Last edited: Aug 15, 2016
3. Aug 15, 2016

### Staff: Mentor

I recall struggling with this problem years ago when designing algorithms for digital devices to protect power systems. Obviously, a protection device should decide what it sees, and trigger corrective actions as rapidly as possible. On the other hand, it should have zero false positives where it jumps to the wrong conclusion.

If everything is ideal and perfect, you can do it with 4 samples as @Hesch said, (actually 3 samples if you assume the DC offset is zero). But real life waveforms are far from ideal, especially in transient states.

Bottom line, there is a trade-off between sampling rate, number of samples, and the quality of your calculated result. So unless you specify your accuracy/reliability requirements, and the details of what you are measuring and your apparatus, your question can't be answered.

Having said that, here's a hint. Start with one full cycle of samples taken at the fastest rate you can manage.

4. Aug 15, 2016

### cnh1995

That's what I did in my earlier project. I "knew" the frequency to be 50Hz and I took samples within 20ms using an on-board timer. The results were accurate. I guess I'll have to use the f to v converter IC for frequency measurement. Once the frequency is obtained, DFT gives amplitude and phase of the fundamental harmonic.

5. Aug 15, 2016

### Hesch

Of course you can specify accuracy requirement, so that the task will be impossible. The resolution of the AD-conversion alone, will limit the accuracy. Use of triacs/thyristors close to the measurement point will disturb the measurement quality enormous, etc. But in principle the use of just 4 samples may be used, though use of all 100 samples by fine tuning ( or use of a FFT ) will give a better and more reliable result ( and longer calculation time ).

As for the hardware ( AD-conversion ), an integrating AD-converter could be used. Have a look at the device: AD652 from Analog Devices. The signal is driving a Voltage Controlled Oscillator, which in turn drives a counter, integrating the mean value of the signal within a sample period. Thereby glitches in the signal will be ignored. The principle of the AD652 may be upgraded, forming a PWM-signal instead of a clock-signal.

6. Aug 15, 2016

### Staff: Mentor

What I had in mind is that power system waveforms can be very noisy, especially with switching surges.

The most reliable way to measure fundamental frequency via sampling is to measure the delay between zero crossings. You can make a band-pass filter by ignoring crossings except those which occur within a defined time window of the previous crossing.