I’m trying to measure the pulses coming from an LV-MaxSonar-EZ1 ultrasound rangefinder. The pulse width is proportional to the distance. Scoping the signal from the rangefinder verifies its presence and that it changes appropriately with distance. I’ve put comments in my code to indicate what is and isn’t happening. Here is my code:
#
# Use input capture to measure pulse width from LV-MaxSonar-EZ1.
#
from pyb import Pin, Timer
startTime = 0
pulseWidth = 0
ic_pin = Pin("P3", Pin.IN, Pin.PULL_NONE)
def sonarHandler(timer):
#
# Never gets called!
#
global startTime, pulseWidth, ic_pin
print("*") # debug
if ic_pin.value(): # Caught rising edge
print("R") # debug
startTime = ic.capture()
else: # Caught falling edge
print("F") # debug
pulseWidth = ic.capture() - startTime & 0x0FFFFFFF
timer1 = Timer(1, freq=10000) # 100 us period
ic = timer1.channel(1, Timer.IC, pin=ic_pin, polarity=Timer.BOTH)
ic.callback(sonarHandler)
while (True):
# P3 changes between 0 and 1 as expected. startTime and pulseWidth atre always zero.
print("P3: %d, Start: %d, Pulse Width: %d" % (ic_pin.value(), startTime, pulseWidth))
# ic.capture() always reads a constant value like 7306 or 695.
print(ic.capture())
TIA for any advice,
RonC