Sampling an ac waveform on microcontroller

Click For Summary
Sampling an AC waveform on a microcontroller can effectively measure frequency, amplitude, and phase, but the accuracy depends on the sampling method and conditions. Using 100 samples at a sampling frequency of 3.33kHz can yield results, especially if the waveform is known to be sinusoidal, but ideal conditions are rarely met in practice. A trade-off exists between sampling rate, number of samples, and measurement quality, with recommendations to use at least one full cycle of samples at the highest rate possible for better accuracy. Employing techniques like zero-crossing detection and using devices like the AD652 can enhance measurement reliability. Ultimately, the precision of results is influenced by factors such as noise and the resolution of the ADC.
cnh1995
Homework Helper
Gold Member
Messages
3,487
Reaction score
1,165
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?
 
Engineering news on Phys.org
cnh1995 said:
But is it possible to calculate the frequency, amplitude and phase if I just sampled 100 samples with a sampling frequency fs?
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:
  • Like
Likes cnh1995 and jim mcnamara
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.
 
  • Like
Likes cnh1995
anorlunda said:
Having said that, here's a hint. Start with one full cycle of samples taken at the fastest rate you can manage.
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.
 
anorlunda said:
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.
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.
 
  • Like
Likes cnh1995
What I had in mind is that power system waveforms can be very noisy, especially with switching surges.

surge-effect-on-power-system.jpg


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.
 
  • Like
Likes cnh1995
Thread 'I thought it was only Amazon that sold unsafe junk'
I grabbed an under cabinet LED light today at a big box store. Nothing special. 18 inches in length and made to plug several lights together. Here is a pic of the power cord: The drawing on the box led me to believe that it would accept a standard IEC cord which surprised me. But it's a variation of it. I didn't try it, but I would assume you could plug a standard IEC cord into this and have a double male cord AKA suicide cord. And to boot, it's likely going to reverse the hot and...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 46 ·
2
Replies
46
Views
5K
  • · Replies 7 ·
Replies
7
Views
4K
Replies
7
Views
4K
Replies
9
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
4K