0

I am new in programming Raspberry Pi Pico and I have a project to measure the period of the square wave signal. This square wave signal is generated by function generator with 3.3V amplitude. Here is my code.

import utime
from machine import Pin

startTime=0
stopTime=0

inPin = machine.Pin(2, machine.Pin.IN)
while True:
    while inPin.value() != 0:
        pass
    while inPin.value() == 0:
        pass
    startTime=utime.ticks_us()
    while inPin.value() != 0:
        pass
    while inPin.value() == 0:
        pass
    stopTime=utime.ticks_us()
    elapsedTime=utime.ticks_diff(stopTime,startTime)
    print(elapsedTime)

I have tried to measure three different frequency. The pictures below are the measurement result of wave period.

1 Hz frequency

10 Hz frequency

100 Hz frequency

As we can see, there are several microseconds deviation on the measurement result. Is there a way to get the accurate value, for example 1 Hz = 1000000 us period?

thoriq
  • 1
  • See this for some info about jitter: Renesas AN-840 Jitter Specifications for Timing Signals = https://www.renesas.com/us/en/document/apn/840-jitter-specifications-timing-signals – aMike Oct 01 '22 at 02:43
  • Several thoughts: Is your source really stable within 5 PPM? If not, then "fixing" the measurement won't help. I'm assuming the jitter is from MP interpreting the commands, and dealing with interrupts behind the scenes. To tighten up the MP code, look at "Maximising Micropython Speed" at https://docs.micropython.org/en/latest/reference/speed_python.html Especially the 'viper' part. After that, I would try to use the PIO to measure the period. There's probably examples that do that. And if that wasn't good enough, then some @arm_thumb code in MP, or even a C extension. – aMike Oct 03 '22 at 18:22

0 Answers0