Pin doesn't exist teensy micropython

caleb123

New member
1741535918770.png


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:
yes, please provide full program (using the </> code tag), as the error is not in the python snippet
 
Are you sure that 'O-24' and 'O-25' is the right syntax. Never have seen such in python. Assume you have the latest firmware you should have something like 'D24' and 'D25'
 
Sorry that was the last one I tried but I did just the number then D-n then O-n as given as options in the help menu
 
Hyphen in python is an operator, namely subtraction. So D_n, O_n are what you need, not D-24 or O-24
 
To see the available pins go to "micropython\ports\mimxrt\boards\TEENSY4\pins.csv" (for example on github) where in column A you see the names you can use (here D24 and D25 without 'minus' or 'underscore')
 
Back
Top