# How to calculate the phase difference between current and voltage

In one pin of arduino receiving voltages form PT and one pin receiving current from CT. I wan to calculate the phase angle between both of them (Power factor). Is there any built-in function in Arduino to perform this task
Regards
Otoman

If you find the time (by using, say, millis()) between epk and ipk then you can infer the angle, assuming frequency is constant.

Presumably you are reading values off the A/D channels and stuffing into a two-dimensional array at timed intervals.

You will need to find the MAX or peak value of both E and I and relate that array location to a tick in time. Let's say you are taking 256 samples and your E-peak happens at 128. If your I-peak happens at 192, you are 90 degrees lagging, etc.

[quote author=Runaway Pancake link=msg=2245778 date=1432472222]
If you find the time (by using, say, millis()) between epk and ipk then you can infer the angle, assuming frequency is constant.
[/quote]It may give more accuracy if the timing is done from when the original signals cross zero volts.

Alternatively, don’t do any timing at all. Take a series of samples of the two signals and derive the RMS voltage and RMS current. For each pair of samples (taken very close to the same time) also multiply the instantaneous voltage and instantaneous current, then average the multiplication products to derive the average power. From those figures, calculate the power factor.