EmbeddedRelated.com
Forums
Memfault Beyond the Launch

Phase Difference measurement of AC using A/D Microcontroller

Started by Rohan August 28, 2006
How do you measure the difference in phase of two AC signals using an
A/D type microcontroller?

Can't think of any concrete logic for doing this. Any one who has
implemented this?
Does anyone have any good links or material for reading up on similar
topics.

I am working on a Holtek 46 series. 

Thanks in advance

If the wave forms are identical  but different in phase  then...

You can compare the difference in timing between the +peaks  OR -peaks  or 
the zero crossings.

This time difference is proportionally related to phase.


JG

"Rohan" <rohanshar@gmail.com> wrote in message 
news:1156751501.465565.71430@m73g2000cwd.googlegroups.com...
> How do you measure the difference in phase of two AC signals using an > A/D type microcontroller? > > Can't think of any concrete logic for doing this. Any one who has > implemented this? > Does anyone have any good links or material for reading up on similar > topics. > > I am working on a Holtek 46 series. > > Thanks in advance >
"Rohan" <rohanshar@gmail.com> wrote in message 
news:1156751501.465565.71430@m73g2000cwd.googlegroups.com...
> How do you measure the difference in phase of two AC signals using an > A/D type microcontroller? > > Can't think of any concrete logic for doing this. Any one who has > implemented this? > Does anyone have any good links or material for reading up on similar > topics. > > I am working on a Holtek 46 series. > > Thanks in advance >
The way I do it is to use an insolated probe for each phase. The probe is a piece of RG-174 with about 2 inches of shield removed. A probe is taped to each phase of the power to be monitored. The other ends are connected to the pins of a PIC. To measure the phase, time the difference in rising edges. This is much easier than using an A/D. The probe I used were about 5 feet long. The output at the PIC pins was about 2 volts peak on a 115 volt wire, and clipped by the internal diodes for higher voltages. Don
Rohan wrote:

> How do you measure the difference in phase of two AC signals using an > A/D type microcontroller? > > Can't think of any concrete logic for doing this. Any one who has > implemented this? > Does anyone have any good links or material for reading up on similar > topics. > > I am working on a Holtek 46 series. > > Thanks in advance >
What frequency are your signals? How well do you know this frequency? How quickly do you need a measurement? How precise does your measurement need to be? How accurate are your ADCs? How good is the circuitry preceding them? If your accuracy requirements are modest, the frequency is small, and you want an easy measurement, then use the timing methods Joe & Donald already gave you. For the phase measurements that I have been doing of late I've had a really good idea of the frequency, my accuracy requirements were fairly stringent, and I had good circuitry leading up to good ADCs. For these I recorded multiple samples of the signal in question over one or more cycles, then took a one-frequency DFT (this sounds way more complex than it is). The math looked like this: xi = sum from n = 0 to N-1 cos(f * t[n]) * x[n], xq = sum from n = 0 to N-1 sin(f * t[n]) * x[n] phase = atan2(xi, xq) Note that this gets you the phase of _one_ channel w.r.t a reference, which is what I was trying to do. You can do this to each of your signals, and compare the two phases. Note also that this will get you a really good measurement in a small amount of time (the best in the least, if you choose your theory right, in fact). Note also that you can make an easy trade off between noise and acquisition time by your choice of N: the more cycles of x that you average over, the less noise you'll see, but the more time it'll take. There are tricks you can play if you have some specific phase differences you're trying to maintain, but I don't know what those are in your case, so I'll leave you with this. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ "Applied Control Theory for Embedded Systems" came out in April. See details at http://www.wescottdesign.com/actfes/actfes.html
Hi All,

I am really sorry about providing so little information about my signal
types.
The signal is 240V AC Mains. The frequency is around unstable between
45 to 55 Hz. I already have a circuit using current transformers which
gives a seperate sinusoidal voltage and current signals. I have to
measure the phase difference between these two signals.
The variation in phase between the two signals will be max from about
60 degrees lag to 60 degrees lead.

Donald: I have to measure the amplitude also so can't afford to clip
the signal. But your method is really awesome and handy.

Tim:

What frequency are your signals?
about 45 to 55 hz

How well do you know this frequency?
It varies. and is unstable. But both my signals will be in sync as they
are from the same power line.

How quickly do you need a measurement?
not very quickly. once every 10 cycles would be great.

How precise does your measurement need to be?
Again not very precise. a variation + or - 2 degrees should be
acceptable

How accurate are your ADCs?
ADC's are 9 bit.

How good is the circuitry preceding them?
Its will be inside a PCB with SMD components. So the circuitry will be
stable and noise free. The design of the circuit is still in the intial
stages and will depend on what logic is used for measurement of phase.

Your method is excellent. Thanks for letting us know. I believe I have
a modest processor working for me. So the method will be too heavy for
it.

I had these approches in mind. Kindly have a look.
1. A seperate circuitry giving interrupts at zero crossings. Keep
measuring the difference between these interrupts.
2. Have a method to measure the peaks. This will save me a lot of
outside circuit as my requirement includes measuring the amplitude
also. Else I will have to convert them into DC and phase into square
wave. I would prefer feeding the controller with sinusoidal waves.
3. Take random samples after some time. difference between the
amplitudes is the phase difference. But i think the negative half
cycles will be a problem here.

Thanks for all your help guys.

Really appritiated.


Tim Wescott wrote:
> Rohan wrote: > > > How do you measure the difference in phase of two AC signals using an > > A/D type microcontroller? > > > > Can't think of any concrete logic for doing this. Any one who has > > implemented this? > > Does anyone have any good links or material for reading up on similar > > topics. > > > > I am working on a Holtek 46 series. > > > > Thanks in advance > > > What frequency are your signals? > How well do you know this frequency? > How quickly do you need a measurement? > How precise does your measurement need to be? > How accurate are your ADCs? > How good is the circuitry preceding them? > > If your accuracy requirements are modest, the frequency is small, and > you want an easy measurement, then use the timing methods Joe & Donald > already gave you. > > For the phase measurements that I have been doing of late I've had a > really good idea of the frequency, my accuracy requirements were fairly > stringent, and I had good circuitry leading up to good ADCs. For these > I recorded multiple samples of the signal in question over one or more > cycles, then took a one-frequency DFT (this sounds way more complex than > it is). > > The math looked like this: > > xi = sum from n = 0 to N-1 cos(f * t[n]) * x[n], > xq = sum from n = 0 to N-1 sin(f * t[n]) * x[n] > > phase = atan2(xi, xq) > > Note that this gets you the phase of _one_ channel w.r.t a reference, > which is what I was trying to do. You can do this to each of your > signals, and compare the two phases. > > Note also that this will get you a really good measurement in a small > amount of time (the best in the least, if you choose your theory right, > in fact). Note also that you can make an easy trade off between noise > and acquisition time by your choice of N: the more cycles of x that you > average over, the less noise you'll see, but the more time it'll take. > > There are tricks you can play if you have some specific phase > differences you're trying to maintain, but I don't know what those are > in your case, so I'll leave you with this. > > -- > > Tim Wescott > Wescott Design Services > http://www.wescottdesign.com > > Posting from Google? See http://cfaj.freeshell.org/google/ > > "Applied Control Theory for Embedded Systems" came out in April. > See details at http://www.wescottdesign.com/actfes/actfes.html
On 29 Aug 2006 00:49:57 -0700, "Rohan" <rohanshar@gmail.com> wrote:

>The signal is 240V AC Mains. The frequency is around unstable between >45 to 55 Hz. I already have a circuit using current transformers which >gives a seperate sinusoidal voltage and current signals. I have to >measure the phase difference between these two signals. >The variation in phase between the two signals will be max from about >60 degrees lag to 60 degrees lead.
What kind of load current are you trying to measure ? Is the load a simple combination of resistance, inductance and capacitance or something more complicated, such as rectifiers, saturating transformers etc. With complicated loads the current waveform is far from a sine wave and it can be hard to detect zero crossings or maximum values. By the way, are you trying to measure the actual power using the classical P=Urms*Irms*cos(phi) principle ? With distorted waveforms this not give very usable results. I would suggest sampling both voltages and currents at a reasonably high sample rate (at least 1 kHz), multiply each voltage sample with the current sample to get the instantaneous power and integrate these instantaneous power readings over at least a single mains cycle. Paul
On 29 Aug 2006 00:49:57 -0700, "Rohan" <rohanshar@gmail.com> wrote:

>The signal is 240V AC Mains. The frequency is around unstable between >45 to 55 Hz.
Unless you have a big problem with noise, I recommend timing the zero crossings or a clipped version of the signal as others have already described. If there is a little noise problem, try a bandpass filter ahead of the zero-crossing detector. As for measuring amplitude, a Peak Detector might register noise peaks and give an unreliable result. Instead you can use the A/D in the PIC. Take the absolute value, either with a diode rectifier or in software. The average absolute value is easy to caluculate and has a linear relationship with the sinusoidal amplitude, assuming the signal really is a sinsoid. Robert Scott Ypsilanti, Michigan
Rohan wrote:

(top posting fixed)
> > Tim Wescott wrote: > >>Rohan wrote: >> >>
-- snip --
>>
> Hi All, > > I am really sorry about providing so little information about my > signal types. > > The signal is 240V AC Mains. The frequency is around unstable between > 45 to 55 Hz. I already have a circuit using current transformers which > gives a separate sinusoidal voltage and current signals. I have to > measure the phase difference between these two signals. > The variation in phase between the two signals will be max from about > 60 degrees lag to 60 degrees lead. > > Donald: I have to measure the amplitude also so can't afford to clip > the signal. But your method is really awesome and handy. > > Tim: > > What frequency are your signals? > about 45 to 55 hz > > How well do you know this frequency? > It varies. and is unstable. But both my signals will be in sync as > they are from the same power line. > > How quickly do you need a measurement? > not very quickly. once every 10 cycles would be great. > > How precise does your measurement need to be? > Again not very precise. a variation + or - 2 degrees should be > acceptable > > How accurate are your ADCs? > ADC's are 9 bit. > > How good is the circuitry preceding them? > Its will be inside a PCB with SMD components. So the circuitry will be > stable and noise free. The design of the circuit is still in the > initial stages and will depend on what logic is used for measurement of > phase. > > Your method is excellent. Thanks for letting us know. I believe I have > a modest processor working for me. So the method will be too heavy for > it. Double check, though. With care given to the way you treat multiplication even a modest processor could do this at Paul's recommended 1kHz. If you had good reason to believe that the waveform would be steady over a 1/2 second interval you could even sample at sub-harmonics the way the old sampling scopes did. Or if you have the RAM for it you could collect a cycle or two of data at a time, then do the math afterward. > > I had these approaches in mind. Kindly have a look. > 1. A separate circuitry giving interrupts at zero crossings. Keep > measuring the difference between these interrupts. This has the problems with noise and distortion already mentioned. Consider the cost of this circuitry vs. a bigger processor, and the fact that if you do it in circuitry it's locked in, while if you go with a bigger processor you can make changes on the fly during engineering or even manufacture. > 2. Have a method to measure the peaks. This will save me a lot of > outside circuit as my requirement includes measuring the amplitude > also. Else I will have to convert them into DC and phase into square > wave. I would prefer feeding the controller with sinusoidal waves. Ditto noise and distortion problems. Once again, if you spend the money on a bigger processor you can get 'real' RMS values by calculating x^2. I _think_ you could do all of this with a PIC 18xx with plenty of RAM. With a 40MHz clock it's certainly fast enough. You may even be able to do it with one of those ridiculous 50MHz 8051s that are out there. If you free up your processor choice, I'm 99.44% sure than for the money, you could do this cheaper by taking your zero-crossing and peak-detecting circuitry off the board and putting a larger microprocessor on. You need one of two things: enough speed to do 3000 multiply-accumulates per second (based on getting v^2, i^2 and v*i), or enough memory to record at least 88 bytes of data (one cycle of 45Hz data at 1kHz, two channels, two bytes per channel), although recording 10 cycles would be better. > 3. Take random samples after some time. difference between the > amplitudes is the phase difference. But i think the negative half > cycles will be a problem here. If you trust your zero crossing you can take samples at increasing intervals after the zero crossing (this is the 'sampling scope' idea I mention earlier). You can use this to build up a picture of your wave, but _only_ if your wave repeats well from one time to the next -- if it doesn't you'll read noise that isn't there, and mess up your RMS calculations. > > Thanks for all your help guys. > > Really appreciated. > -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Posting from Google? See http://cfaj.freeshell.org/google/ "Applied Control Theory for Embedded Systems" came out in April. See details at http://www.wescottdesign.com/actfes/actfes.html
On Tue, 29 Aug 2006 10:35:50 -0700, Tim Wescott <tim@seemywebsite.com>
wrote:

>Rohan wrote:
> > How accurate are your ADCs? > > ADC's are 9 bit.
Assuming 1 bit sign and 8 bit magnitude.
>You need one of two things: enough speed to do 3000 >multiply-accumulates per second (based on getting v^2, i^2 and v*i),
This is not very much even for an 8 bitter without a multiplication instruction. Since the ADC only produces sign+8 data bits, it would be sufficient to perform an unsigned 8x8=16 bit multiply and separately handle the sign before accumulation. Assuming that 8x1 bit multiplication would require 5-6 instructions, the 8x8 multiplication would require 40-50 instructions, adding a few instructions for sign handling and accumulation, a multiply-accumulate would take well below 100 instructions and the required rate would be below 300000 instructions/s. Even with 12 cycles/instruction, the clock frequency would be 3.6 MHz for just the multiply-accumulation, which would not be a problem, unless the processor must run on flea power. Paul

Memfault Beyond the Launch