PageSourceSearch

https://ide.robocore.net/ui/pylibsBlobs/hcsr04.js

js robocore.net collected 2026-09-24 09:00:17 UTC 3,748 bytes, 75 lines download raw bytes

1var hcsr04Blob = new Blob([
2"from machine import Pin, time_pulse_us\n" +
3"from utime import sleep_us\n" +
4"\n" +
5"__version__ = '0.2.1'\n" +
6"__author__ = 'Roberto Sánchez'\n" +
7"__license__ = 'Apache License 2.0. https://www.apache.org/licenses/LICENSE-2.0'\n" +
8"\n" +
9"# Driver to use the untrasonic sensor HC-SR04.\n" +
10"# The sensor range is between 2cm and 4m.\n" +
11"\n" +
12"# The timeouts received listening to echo pin are converted to OSError('Out of range')\n" +
13"\n" +
14"class HCSR04:\n" +
15"    # echo_timeout_us is based in chip range limit (400cm)\n" +
16"    # trigger_pin: Output pin to send pulses\n" +
17"    # echo_pin: Readonly pin to measure the distance. The pin should be protected with 1k resistor\n" +
18"    # echo_timeout_us: Timeout in microseconds to listen to echo pin. \n" +
19"    # By default is based in sensor limit range (4m)\n" +
20"\n" +
21"    def __init__(self, trigger_pin, echo_pin, echo_timeout_us=500*2*30):\n" +
22"        self.echo_timeout_us = echo_timeout_us\n" +
23"        # Init trigger pin (out)\n" +
24"        self.trigger = Pin(trigger_pin, mode=Pin.OUT, pull=None)\n" +
25"        self.trigger.value(0)\n" +
26"\n" +
27"        # Init echo pin (in)\n" +
28"        self.echo = Pin(echo_pin, mode=Pin.IN, pull=None)\n" +
29"\n" +
30"    # Send the pulse to trigger and listen on echo pin.\n" +
31"    # We use the method `machine.time_pulse_us()` to get the microseconds until the echo is received.\n" +
32"    def _send_pulse_and_wait(self):\n" +
33"        self.trigger.value(0) # Stabilize the sensor\n" +
34"        sleep_us(5)\n" +
35"        self.trigger.value(1)\n" +
36"        # Send a 10us pulse.\n" +
37"        sleep_us(10)\n" +
38"        self.trigger.value(0)\n" +
39"        try:\n" +
40"            pulse_time = time_pulse_us(self.echo, 1, self.echo_timeout_us)\n" +
41"            # time_pulse_us returns -2 if there was timeout waiting for condition; and -1 if there was timeout during the main measurement. It DOES NOT raise an exception\n" +
42"            # ...as of MicroPython 1.17: http://docs.micropython.org/en/v1.17/library/machine.html#machine.time_pulse_us\n" +
43"            if pulse_time < 0:\n" +
44"                MAX_RANGE_IN_CM = const(500) # it's really ~400 but I've read people say they see it working up to ~460\n" +
45"                pulse_time = int(MAX_RANGE_IN_CM * 29.1) # 1cm each 29.1us\n" +
46"            return pulse_time\n" +
47"        except OSError as ex:\n" +
48"            if ex.args[0] == 110: # 110 = ETIMEDOUT\n" +
49"                raise OSError('Out of range')\n" +
50"            raise ex\n" +
51"\n" +
52"    # Get the distance in milimeters without floating point operations.\n" +
53"    def distance_mm(self):\n" +
54"        pulse_time = self._send_pulse_and_wait()\n" +
55"\n" +
56"        # To calculate the distance we get the pulse_time and divide it by 2 \n" +
57"        # (the pulse walk the distance twice) and by 29.1 becasue\n" +
58"        # the sound speed on air (343.2 m/s), that It's equivalent to\n" +
59"        # 0.34320 mm/us that is 1mm each 2.91us\n" +
60"        # pulse_time // 2 // 2.91 -> pulse_time // 5.82 -> pulse_time * 100 // 582 \n" +
61"        mm = pulse_time * 100 // 582\n" +
62"        return mm\n" +
63"\n" +
64"    # Get the distance in centimeters with floating point operations.\n" +
65"    # It returns a float\n" +
66"    def distance_cm(self):\n" +
67"        pulse_time = self._send_pulse_and_wait()\n" +
68"\n" +
69"        # To calculate the distance we get the pulse_time and divide it by 2 \n" +
70"        # (the pulse walk the distance twice) and by 29.1 becasue\n" +
71"        # the sound speed on air (343.2 m/s), that It's equivalent to\n" +
72"        # 0.034320 cm/us that is 1cm each 29.1us\n" +
73"        cms = (pulse_time / 2) / 29.1\n" +
74"        return cms\n"
75], {type: 'text'});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.