Sampling an ac waveform on microcontroller

In summary: To do this, you need to know the time between two successive crossings. This can be determined by counting the number of ticks in a second. The waveform can be sampled at a rate faster than the number of zero crossings per second. So if you sample at 3.33kHz, you will get 100 samples per second. But you'll only get 4 samples per second if you sample at 5kHz. In summary, it is possible to calculate frequency, amplitude and phase if you sample at a faster rate than the number of zero crossings per second.
  • #1
cnh1995
Homework Helper
Gold Member
3,486
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
  • #2
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
  • #3
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
  • #4
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.
 
  • #5
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
  • #6
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

FAQ: Sampling an ac waveform on microcontroller

What is sampling an AC waveform on a microcontroller?

Sampling an AC waveform on a microcontroller refers to the process of measuring and recording the amplitude and frequency of an alternating current (AC) electrical signal using a microcontroller. This allows for the analysis and manipulation of the waveform for various applications.

Why is sampling an AC waveform on a microcontroller important?

Sampling an AC waveform on a microcontroller is important because it allows for the precise measurement and analysis of AC signals, which are commonly used in electronic devices and power systems. This information can then be used for various purposes such as control, monitoring, and troubleshooting.

What are the steps involved in sampling an AC waveform on a microcontroller?

The steps involved in sampling an AC waveform on a microcontroller include: 1. Connecting the AC signal to the microcontroller's analog input pin2. Setting up the microcontroller's analog-to-digital converter (ADC) to sample the signal at a specific frequency3. Reading the sampled data from the ADC4. Converting the digital data into a usable format (e.g. voltage or frequency)5. Processing and analyzing the data as needed.

What factors should be considered when sampling an AC waveform on a microcontroller?

When sampling an AC waveform on a microcontroller, factors such as the sampling rate, resolution, and accuracy of the ADC, as well as the frequency and amplitude of the AC signal, should be considered. Additionally, proper grounding and noise reduction techniques should be implemented to ensure accurate measurements.

Are there any challenges or limitations to sampling an AC waveform on a microcontroller?

Yes, there are some challenges and limitations to sampling an AC waveform on a microcontroller. These may include noise interference, aliasing, and limited resolution and accuracy of the ADC. Additionally, the frequency of the AC signal may be limited by the sampling rate of the microcontroller. Proper techniques and considerations should be implemented to minimize these challenges and limitations.

Back
Top