# mesure phase shift

**URL:** <https://forum.arduino.cc/t/mesure-phase-shift/385850>\
**Category:** Science and Measurement\
**Created:** [May 14, 2016, 10:39am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850 "2016-05-14T10:39:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![nabin1994](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nabin1994](https://forum.arduino.cc/u/nabin1994)\
**Post date:** [May 14, 2016, 10:39am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/1 "2016-05-14T10:39:28Z")

</div>

I want measure phase shift between two analog signal which will be feed to two analog pin of arduino uno.

 ![Capture.JPG](https://europe1.discourse-cdn.com/arduino/original/4X/9/6/7/9673ae7df816bf89cdb00050119ed37c00c51924.jpeg)

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [May 14, 2016, 3:47pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/2 "2016-05-14T15:47:30Z")

</div>

That is very difficult using the ADC.

There is only one ADC in the Arduino Uno, and it is connected in turn to each pin to make a measurement.

Two zero crossing detectors would help.

---

<div class="post-metadata">

**Author:** ![nabin1994](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nabin1994](https://forum.arduino.cc/u/nabin1994)\
**Post date:** [May 14, 2016, 4:26pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/3 "2016-05-14T16:26:30Z")

</div>

is it possible to measure it by using any other hardware device with arduino uno.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [May 14, 2016, 4:49pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/4 "2016-05-14T16:49:14Z")

</div>

See reply #1

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [May 15, 2016, 12:10am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/5 "2016-05-15T00:10:38Z")

</div>

Hi nabin, the '[zero crossing detector](https://www.google.com/#&q=zero+crossing+detector)' that jremington mentioned is a circuit that will send a pulse when a waveform crosses zero, so twice per period. Since the pulses are on or off, you don't need to use the adc. Then you can measure the time between pulses.

---

<div class="post-metadata">

**Author:** ![nabin1994](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nabin1994](https://forum.arduino.cc/u/nabin1994)\
**Post date:** [May 15, 2016, 4:17am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/6 "2016-05-15T04:17:39Z")

</div>

I'm a new Arduino new learner.can Anyone give me the arduino code to measure phase shift of two signal?

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [May 15, 2016, 4:23am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/7 "2016-05-15T04:23:14Z")

</div>

How is it hooked up?

---

<div class="post-metadata">

**Author:** ![nabin1994](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nabin1994](https://forum.arduino.cc/u/nabin1994)\
**Post date:** [May 18, 2016, 12:20pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/8 "2016-05-18T12:20:30Z")

</div>

I want to measure power factor using emonlib.h how can I do it?I can't understand the program related with emonlib.h which is in this forum.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [May 19, 2016, 3:52am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/9 "2016-05-19T03:52:42Z")

</div>

What happened to the phase shift measurement? Myself and Mr. Remington have been trying to follow you, and help, but you ignore our suggestions, and when asked questions, you restate the same question, or change the topic.

Good luck with your project.

---

<div class="post-metadata">

**Author:** ![nabin1994](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@nabin1994](https://forum.arduino.cc/u/nabin1994)\
**Post date:** [May 19, 2016, 5:31am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/10 "2016-05-19T05:31:11Z")

</div>

Sorry sir.I think If I can measure power factor with the help of Emonlib.h then I able to find phase shift by cos-1(power factor).and also this library can find Vrms and Irms and pf which I need.So please sir help me.

code:  
#include "EmonLib.h" // Include Emon Library  
EnergyMonitor emon1; // Create an instance

void setup()  
{  
Serial.begin(9600);

emon1.voltage(2, **?** , **?** ); // Voltage: input pin, **calibration** , **phase\_shift**  
emon1.current(1, **?** ); // Current: input pin, **calibration.**  
}

void loop()  
{  
emon1.calcVI(20,2000); // Calculate all. No.of crossings, time-out  
emon1.serialprint(); // Print out all variables  
}

How can I calculate those **"?"** marked in program by multimeter according to my system(attached as jpg).

 ![F6RIPAPHQF9H5IO.MEDIUM.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/2/d/f/2df23506544c2dc856b05e09150bad112faa07d0.jpeg)

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [May 19, 2016, 11:24am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/11 "2016-05-19T11:24:31Z")

</div>

> ChrisTenone:  
> What happened to the phase shift measurement? Myself and Mr. Remington have been trying to follow you, and help, but you ignore our suggestions, and when asked questions, you restate the same question, or change the topic.
> 
> Good luck with your project.

power factor is the cosine of the phase-shift, its the same measurement.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [May 20, 2016, 3:38am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/12 "2016-05-20T03:38:11Z")

</div>

> MarkT:  
> power factor is the cosine of the phase-shift, its the same measurement.

Ok, but the OP was not responding to suggestions or questions. Now that I throw up my hands, code gets posted, and a diagram is shown.

---

<div class="post-metadata">

**Author:** ![ChrisTenone](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/christenone/32/219804_2.png) [@ChrisTenone](https://forum.arduino.cc/u/ChrisTenone)\
**Post date:** [May 20, 2016, 6:54am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/13 "2016-05-20T06:54:58Z")

</div>

Hi Nabin,

I apologize for misunderstanding your intent. Thank you for posting more details about your project. I've never used that library, so hopefully someone here will be able to help.

> nabin1994:  
> Sorry sir.I think If I can measure power factor with the help of Emonlib.h then I able to find phase shift by cos-1(power factor).and also this library can find Vrms and Irms and pf which I need.So please sir help me.
> 
> code:  
> #include "EmonLib.h" // Include Emon Library  
> EnergyMonitor emon1; // Create an instance
> 
> void setup()  
> {  
> Serial.begin(9600);
> 
> emon1.voltage(2, **?** , **?** ); // Voltage: input pin, **calibration** , **phase\_shift**  
> emon1.current(1, **?** ); // Current: input pin, **calibration.**  
> }
> 
> void loop()  
> {  
> emon1.calcVI(20,2000); // Calculate all. No.of crossings, time-out  
> emon1.serialprint(); // Print out all variables  
> }
> 
> How can I calculate those **"?"** marked in program by multimeter according to my system(attached as jpg).

---

<div class="post-metadata">

**Author:** ![dwightthinker](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@dwightthinker](https://forum.arduino.cc/u/dwightthinker)\
**Post date:** [August 24, 2016, 5:39am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/14 "2016-08-24T05:39:46Z")

</div>

Hi  
I realize this was some time back.  
Lets assume you are going to measure 60 Hz. That is slow enough that  
you can measure about 10 samples of each per cycle. You know the frequency so  
it is relatively easy to generate a delay for 180 degrees of the voltage  
signal. Multiply the each of these times the current samples.  
Take the average peaks of the two. Use inverse tangent to determine  
the angle.  
Hope this helps some.  
Dwight

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [August 24, 2016, 11:45am UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/15 "2016-08-24T11:45:58Z")

</div>

> jremington:  
> That is very difficult using the ADC.
> 
> There is only one ADC in the Arduino Uno, and it is connected in turn to each pin to make a measurement.
> 
> Two zero crossing detectors would help.

Is it that difficult? You can determine the phase of each signal relative to the sample time for each,  
then correct for the sample time difference since delta phase = frequency x delta time. Indeed  
you can run two software phase-locked-loops in tandem referenced to micros() and get the phase  
difference directly.

So I reckon its the same problem as implementing a software PLL.

---

<div class="post-metadata">

**Author:** ![dwightthinker](https://avatars.discourse-cdn.com/v4/letter/d/a9a28c/32.png) [@dwightthinker](https://forum.arduino.cc/u/dwightthinker)\
**Post date:** [August 24, 2016, 3:39pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/16 "2016-08-24T15:39:38Z")

</div>

I think I forgot a square root in there.  
Before doing an arc tan you need to do a square root.  
Using off chip zero cross detectors is not as good as  
doing a correlation of the two signals. Still, for  
most things, it is probably good enough. It works good  
for nice things like motors but not so good for things  
like switcher power supplies.  
Dwight

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [August 24, 2016, 4:00pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/17 "2016-08-24T16:00:04Z")

</div>

> MarkT:  
> Is it that difficult?  
> So I reckon its the same problem as implementing a software PLL.

For a beginner, yes, it is very difficult.

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [August 27, 2016, 5:07pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/18 "2016-08-27T17:07:10Z")

</div>

But maybe a question in "science and measurement" isn't a beginner question?

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [August 27, 2016, 5:30pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/19 "2016-08-27T17:30:06Z")

</div>

Can't you judge for yourself?

> nabin1994:  
> I'm a new Arduino new learner.can Anyone give me the arduino code to measure phase shift of two signal?

---

<div class="post-metadata">

**Author:** ![allanhurst](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@allanhurst](https://forum.arduino.cc/u/allanhurst)\
**Post date:** [August 27, 2016, 5:36pm UTC](https://forum.arduino.cc/t/mesure-phase-shift/385850/20 "2016-08-27T17:36:04Z")

</div>

An old trick for finding an accurate zero crosssing on a signal ( +ve or -ve ) it to invert the signal and put a comparator beween the inverted and non-inverted signal.

Given fast devices, this works up to quite a high frequency - MHz.

Do this for each of your signals, and time between the two resultant square waves

this comparison even takes out the delay of the invertors and comparators ( which should be pretty small).

regards

Allan

[Next page](https://forum.arduino.cc/t/mesure-phase-shift/385850.md?page=2)
