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.
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?