when attempting to run micropython code for an ultrasound sensor and teensy 4.1 i keep on running into the issue "ValueError: Pin doesn't exist" as seen in the screenshot any advice as to how to resolve this would be greatly appreciated thank you in advance please ask if any more detail is needed
Code:
(micropython)
import machine
import time
from machine import Pin
class Ultrasound:
def __init__(self, trigger_pin, echo_pin, echo_timeout_us=500 * 2 * 30):
self.echo_timeout_us = echo_timeout_us
self.trigger = machine.Pin(O-24,Pin.OUT,[Pin.PULL_DOWN])
self.echo = machine.Pin(O-25,Pin.IN,[Pin.PULL_DOWN])
def _send_pulse_and_wait(self):
"""Send ultrasonic pulse and measure the time for echo return."""
self.trigger.value(0)
time.sleep_us(5)
self.trigger.value(1)
time.sleep_us(10)
self.trigger.value(0)
try:
pulse_time = machine.time_pulse_us(self.echo, 1, self.echo_timeout_us)
return pulse_time
except OSError as ex:
if ex.args[0] == 110: # Timeout error
raise OSError("Out of range")
raise ex
# def distance_mm(self):
# """Calculate distance in millimeters."""
# pulse_time = self._send_pulse_and_wait()
# mm = pulse_time * 100 // 582
# return mm
def distance_cm(self):
"""Calculate distance in centimeters."""
pulse_time = self._send_pulse_and_wait()
distance = (pulse_time / 2) / 29.1
return distance
sensor = Ultrasound(24,25)
# Print measured distance in centimeters
while True:
print("Distance:", sensor.distance_cm(), "cm")
time.sleep(1)
Last edited: