# Phase lock loop question

#### ronsimpson

##### Well-Known Member
i have a 100hz sinus signal
i have a sinus voltage witch 5volt peak, and want to know when its zero volt.
i want to use it for 120v ac
This is why we can't help.

Here we have 110VAC 50 or 60hz. Not 100hz. Not 5V.
The output is 0 or 5v to go to the computer.

I can build a circuit that works like this but you will get a (+)edge at one crossing and a (-)edge at the other.

This next circuit will give you a 200nS pulse (interrupt) at any zero crossing.

In software you will need to measure the frequency. (you need to know 50 or 60hz) Depends on where you live.
Because you get two pulses/cycle then you really will get numbers like 100 or 120 hz.
10mS or 8.3mS
I do not know what you want to do and you are not going to say.....but
I will pretend you want to turn on a SCR like a lamp dimmer.
Measure from one pulse to another = 10 (8.3mS).
If you want the lamp at 1/2 power them after the interrupt, delay 5mS (4.15ms) and turn on the SCR.
This is how you set the phase. (measure time)

#### ronsimpson

##### Well-Known Member
From MicroChip application note AN236:

From this you will get a square wave at 50/60hz. You will need to respond to each edge. (or be cleaver)
This will be slightly off from "zero". It will detect 4V and 1V not zero volts. But out of 120 volts that error is probably OK.

#### boro3

##### Member
Hello, sorry for the late answer,
thank you very much for your help.
the 5 volt peak schould just be an example,
like s(t)=5*sin(wt), or 120v*sin(wt);
and yes its for turning on an scr.
and i just wanted to use pll for this, becaus zcd is not good is the source has jitter.

#### MrAl

##### Well-Known Member
Hello, sorry for the late answer,
thank you very much for your help.
the 5 volt peak schould just be an example,
like s(t)=5*sin(wt), or 120v*sin(wt);
and yes its for turning on an scr.
and i just wanted to use pll for this, becaus zcd is not good is the source has jitter.
Hi,

You may be able to work this out in software. I'd have to give it some more thought but you can too.
We do this all the time for analog signals so there should be no reason why we cant do it for frequency too. We also do it in time once in a while, or at least it is possible.

A quick idea would be to measure the zero crossing using a standard method hopefully with low offset. A side question to be answered also is would the ADC be fast enough so you could get away with using that for detecting. The main idea though is to measure the zero crossing using a conventional method, then instead of using that data directly, use it to predict the next likely true zero crossing. I've actually done this but only over a single cycle. You may want to extend the prediction to over several cycles. After all, that's what a PLL would get you anyway...it would tell you where the average zero crossing point is.

So you measure the zero crossing over several half cycles, jitter or not, then average out the times so you get the mean and use that to predict the next likely zero crossing. So you end up using the data from the previous N half cycles to predict the next true zero crossing.

Like i said it will take a little more thought, but the circuit will then be quite simple with the code just a little more complex but not too bad.

The drawback to this or a PLL method is if the line should provide a helf cycle that is abnormally different, the zero crossing or PLL prediction will be way off. Lucky that doesnt happen too often.

#### dr pepper

##### Well-Known Member
I have done something similarish with a pic micro, but much less tech sounding.
I used the built in comparator with I think it was a 2.5v reference, the low voltage mains ac reference came via the power supply transformer for the pic.
An interrupt was generated when the ref crossed 2.5v, and I just pre loaded a timer with a value, this value was 'fudged' so that power level 0 gave zero power, it worked.
For controlling an scr or triac you dont have to detect zero cross, you can detect any other point in the power cycle so long as its repeatable and just correct with an offset value.
Beware transformers create phase shift, and if the load is to vary so does the shift.

Last edited:

#### MrAl

##### Well-Known Member
I have done something similarish with a pic micro, but much less tech sounding.
I used the built in comparator with I think it was a 2.5v reference, the low voltage mains ac reference came via the power supply transformer for the pic.
An interrupt was generated when the ref crossed 2.5v, and I just pre loaded a timer with a value, this value was 'fudged' so that power level 0 gave zero power, it worked.
For controlling an scr or triac you dont have to detect zero cross, you can detect any other point in the power cycle so long as its repeatable and just correct with an offset value.
Beware transformers create phase shift, and if the load is to vary so does the shift.
Hi,

Yes that is what i was talking about when i was talking about the single cycle prediction technique.
If we are detecting at 2.5v then the error for a 170v peak is less than 1 degree. As the line voltage varies the change in error time changes roughly the same as the line voltage itself because for small angles sin(A)=A. Thus a change in line voltage (low line, high line) of 15 percent causes a change in detection time of 15 percent.

If detecting at 2.5v with a 170v peak the error angle is very low but if detecting at 2.5v with a 5v peak then the error angle is quite large, and prediction would become necessary. However, the change in angle with line voltage will be larger so we'd have to look at that.

Detecting at very low voltage is always a good idea. That also allows us to detect at the end of the half cycle and predict to the beginning of the next half cycle. So we detect on the falling edge, but use that information to predict where the next half cycle rising edge is. This works pretty good and there will be little error. For a more constant angle though we would have to accumulate more data. The question thought then becomes is this going to do us any good. It will then start to become very dependent on the application where some apps will benefit and some wont. For the stuff i did in the past, it would not be a good idea because i wanted to stay nearly synced to the line even if it changed a little, and that was for triac control of loads like universal motors and resistive lamp dimmers.

So maybe it would be good to hear more about the actual application and why normal syncing is not good enough.

The formula for the detection time given the detect voltage Vss and line voltage peak Vp and frequency f is:
t=asin(Vss/Vp)/(2*pi*f)

#### dr pepper

##### Well-Known Member
Agreed that makes sense.
Although I probably didnt explain well, my system used a low voltage secondary output from the power transformer that supplied the pic & electronics, I think it was 6v ac, so 2.5v detection would have introduced a big delay, but because the delay was constant I was able to bodge it out by preloading a timer with a correction value.

#### Nigel Goodwin

##### Super Moderator
Just to add a little more confusion, some of the new enhanced PIC's have hardware zero-crossing detection built-in - such as the 16F18877 series, see pages 306-311 of the datasheet, which has zero-crossing detection on pin RB0.

#### MrAl

##### Well-Known Member
Agreed that makes sense.
Although I probably didnt explain well, my system used a low voltage secondary output from the power transformer that supplied the pic & electronics, I think it was 6v ac, so 2.5v detection would have introduced a big delay, but because the delay was constant I was able to bodge it out by preloading a timer with a correction value.
Hi,

That is what i had gathered from your post so i didnt have a problem with it.

There is an interesting side effect though. That is, what happens during low line and high line. The normal spec is 15 percent so using that we see that a detection voltage of 0.001v and peak of 5v leads to very small change in the detection time, but, using a detection voltage of 2.5v and a peak of 5v leads to a 20 percent change in time. That means detecting at 2.5v with a 5v peak should not really be considered "constant". During low line and high line there will be some differences, which may or may not make a difference, depending on how accurate we try to emulate the zero crossing. If we plan ahead we can avoid problems.

In other words, if we try to use 2.5v with a 5v peak we get a 30 degree error at nominal line but at low line we get a 36 degree error. For me that meant keeping the turn on time for the triac a little farther into the cycle just to make sure, or else i would get very small conduction angle.

So for 2.5v and 5v peak, the difference in timing could be as much as 6 degrees assuming normal line specs. In my area though it would sometimes be even worse than that. At high line it is just the opposite, the time gets smaller.

So beware the sliding time value :=)

One design i did used a comparator that could detect the zero crossing at very close to zero simply because i could set the trigger voltage very low. Using diode clamps, it was ensured that the comparator never got too high of ao voltage. And that was using the full peak 170v of the line. We often need a tiny bit of filtering too though to keep high frequency noise out.

Last edited: