Python machine.time_pulse_us() Examples

The following are 12 code examples of machine.time_pulse_us(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module machine , or try the search function .
Example #1
Source File: hcsr04.py    From micropython-hcsr04 with Apache License 2.0 6 votes vote down vote up
def _send_pulse_and_wait(self):
        """
        Send the pulse to trigger and listen on echo pin.
        We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
        """
        self.trigger.value(0) # Stabilize the sensor
        time.sleep_us(5)
        self.trigger.value(1)
        # Send a 10us pulse.
        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: # 110 = ETIMEDOUT
                raise OSError('Out of range')
            raise ex 
Example #2
Source File: hcsr04.py    From pysmartnode with MIT License 6 votes vote down vote up
def _pulse(self) -> int:
        """
        Send a pulse and wait for the echo pin using machine.time_pulse_us() to measure us.
        :return: int
        """
        tr = self._tr
        tr.value(0)
        time.sleep_us(5)
        tr.value(1)
        time.sleep_us(10)
        tr.value(0)
        try:
            return machine.time_pulse_us(self._ec, 1, self._to)
        except OSError as e:
            if e.args[0] == 100:  # TIMEOUT
                raise OSError("Object too far")
            raise e 
Example #3
Source File: esp_8266Full.py    From python_banyan with GNU Affero General Public License v3.0 5 votes vote down vote up
def distance_in_cm(self):
        self.trigger.value(1)
        sleep_us(10)
        self.trigger.value(0)
        try:
            time = time_pulse_us(self.echo, 1, 29000)
        except OSError:
            return None
        dist_in_cm = (time / 2.0) / 29
        return dist_in_cm 
Example #4
Source File: esp_8266.py    From python_banyan with GNU Affero General Public License v3.0 5 votes vote down vote up
def distance_in_cm(self):
  self.trigger.value(1)
  sleep_us(10)
  self.trigger.value(0)
  try:
   time=time_pulse_us(self.echo,1,29000)
  except OSError:
   return None
  dist_in_cm=(time/2.0)/29
  return dist_in_cm 
Example #5
Source File: demo.py    From microbit-lib with MIT License 5 votes vote down vote up
def distance(tp, ep):
    ep.read_digital()
    tp.write_digital(1)
    sleep_us(10)
    tp.write_digital(0)
    ts = time_pulse_us(ep, 1, 5000)
    if ts > 0: return ts * 17 // 100
    return ts 
Example #6
Source File: hcsr04.py    From microbit-lib with MIT License 5 votes vote down vote up
def distance(tp, ep):
    ep.read_digital()
    tp.write_digital(1)
    sleep_us(10)
    tp.write_digital(0)
    ts = time_pulse_us(ep, 1, 5000)
    if ts > 0: return ts * 17 // 100
    return ts 
Example #7
Source File: hcsr04.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta 
Example #8
Source File: hcsr04.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta 
Example #9
Source File: hcsr04.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta 
Example #10
Source File: hcsr04.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta 
Example #11
Source File: hcsr04.py    From ulnoiot-upy with MIT License 5 votes vote down vote up
def _send_pulse_and_wait(self):
        # Send the pulse to trigger and listen on echo pin.
        # We use the method `machine.time_pulse_us()` to
        # get the microseconds until the echo is received.
        self.trigger_pin.value(0)  # Stabilize the sensor
        sleep_us(5)
        self.trigger_pin.on()
        # Send a 10us pulse.
        sleep_us(10)
        self.trigger_pin.off()
        # try:
        #     pulse_time = machine.time_pulse_us(self.echo_pin, 1, self.echo_timeout_us)
        #     return pulse_time
        # except OSError as ex:
        #     if ex.args[0] == 110: # 110 = ETIMEDOUT
        #         return -1 # out of range
        #     raise ex

        start = ticks_us()
        while not self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        start = ticks_us()
        while self.echo_pin():
            t = ticks_us()
            if ticks_diff(t, start) > self.echo_timeout_us:
                print("HCR04: timeout")
                return -1
        delta = ticks_diff(ticks_us(), start)
        return delta 
Example #12
Source File: hcsr04.py    From uPySensors with Apache License 2.0 5 votes vote down vote up
def _send_pulse_and_wait(self):
        """
        Send the pulse to trigger and listen on echo pin.
        We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.
        """
        self.trigger.value(0) # Stabilize the sensor
        time.sleep_us(5)
        self.trigger.value(1)
        # Send a 10us pulse.
        time.sleep_us(10)
        self.trigger.value(0)
        try:
            if (uname().sysname == 'WiPy'):
                pulse_list = pulses_get(self.echo, self.echo_timeout_us)
                if(len(pulse_list) == 0):
                    pulse_time = -1
                else:
                    pulse_time = pulse_list[0][1]
            else:
                pulse_time = time_pulse_us(self.echo, 1, self.echo_timeout_us)

            return pulse_time
        except OSError as ex:
            if ex.args[0] == 110: # 110 = ETIMEDOUT
                raise OSError('Out of range')
            raise ex