## Phase Mesurement with Pyboard up to 100 kHz

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Benjamin
Posts: 4
Joined: Wed May 29, 2019 5:45 pm

### Phase Mesurement with Pyboard up to 100 kHz

Hello i want to build an impedance spectroscopy device. The main idea is to measure the impedance of a test object to gain information about the materials. I would like to use it for solid investigation.
For this i have to measure phase and amplitude of some sine waves with known frequencies between 1 Hz and 100 kHz.
for the generation of the wave i was thinking to use an AD9850. For the measurement i could use an ADC but i guess the Internal ADC is not good enough to measure the phase exactly. And as i know Micropython does not support DMA for the ADC right now.
So my idea was to use the Analog Comparator to Measure the Phase and max. Amplitude of the Signal. The Phase i could measure with comparing with 0V and also comparing the reverence with 0V and Count the difference with a timer. The Problem is that in Micropython also the Analog Comparators not Supported right now.
My Question is, how much work is it to Implement the Analog Comparators, can somebody help me with that?
Can somebody Help me with that? Also what do you think about my concept for Measuring?

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

### Re: Phase Mesurement with Pyboard up to 100 kHz

There is the following article on the MicroPython Wiki:

Benjamin
Posts: 4
Joined: Wed May 29, 2019 5:45 pm

### Re: Phase Mesurement with Pyboard up to 100 kHz

Thanks, this Link is very Interesting. But i had another Idea with comparing just the time when the signals reach 0. I was thinking to use the build in comparators. now i read the documentation again and found out the microcontroller of the pyboard has no. My idea is now to use the timer with external interrupt and two schmitt triggers to detect the zero passage.
A little about my background. I'm a master student in Argentina and here is a big economic crisis so it's very difficult to buy hardware. I will ask my professor if we can organise schmitt triggers and op amps to realise my idea, also for the ADC solution are some op amps needed i guess.

pythoncoder
Posts: 4021
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

### Re: Phase Mesurement with Pyboard up to 100 kHz

You need to consider the effect of noise. Any noise at frequencies not harmonically related to that of the sinewave will cause imprecision in the timing of zero crossings. There is also the issue of quantisation noise especially if your signal is small. The algorithm in my article involves lowpass filtering on the extracted phase value, which confers a degree of noise immunity.
Peter Hinch

Benjamin
Posts: 4
Joined: Wed May 29, 2019 5:45 pm

### Re: Phase Mesurement with Pyboard up to 100 kHz

Thanks for your nice work Peter. You are right the noise will disturb my measurement, my idea was to measure several times and calculate the median. I only want to use it for frequencies witch are to high for the ADC.
Yesterday i tested your code from the link. It works fine and the implementation is much faster than C. It's amazing to work with python on Microcontrollers. There are just two thinks. First you forgot to import the method of sin and cos from math in your example. Second, the frequency for phase correction dosn't work well. First of all I'm not sure witch frequency you mean. The sample rate or the real frequency. I guess the sample rate but it produces a big error. Finally the best result was to comment it out.

Also i was a little confused when i used the write timed methods of the DAC and ADC together with the same timer the sample rate is always determined by the DAC.

pythoncoder
Posts: 4021
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

### Re: Phase Mesurement with Pyboard up to 100 kHz

Benjamin wrote:
Sat Sep 28, 2019 3:16 pm
... you forgot to import the method of sin and cos from math in your example.
Well spotted: I've fixed that.
Second, the frequency for phase correction dosn't work well. First of all I'm not sure witch frequency you mean. The sample rate or the real frequency. I guess the sample rate but it produces a big error.
It should be the sample rate used by the ADC's. I'm puzzled as to why you're seeing a big error if you're running on a Pyboard 1.x. The figure of 1.8μs was an empirical measurement of the delay between samples of the two ADC's. To measure this, with the line of code commented out, feed the same signal into both ADC's. The code should report a small phase error which can be converted to a time delay. The line of code merely rotates the phasor to trim out that delay.

I've updated the doc to try to clarify these issues.
Peter Hinch