UltrasoundIO Unit

UNIT SONIC IO 是一款 GPIO 接口的超声波测距传感器。该模块采用 RCWL-9620 超声波测距芯片并配备 16 mm 探头,测距范围可达 2 cm ~ 450 cm(精度最高可达 ±2%)。该传感器通过测量脉冲信号从发射到接收之间的时间差来确定目标距离,用户可通过 IO 控制模式直接获取距离值。它非常适用于机器人避障、液位检测以及其他需要进行测量的应用场景。

支持以下产品:

UltrasoundIOUnit

MicroPython 应用示例

 1# SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
 2#
 3# SPDX-License-Identifier: MIT
 4
 5import os, sys, io
 6import M5
 7from M5 import *
 8from hardware import *
 9from unit import UltrasoundIOUnit
10
11
12title1 = None
13label0 = None
14i2c0 = None
15sonic_io_0 = None
16
17
18def setup():
19    global title1, label0, i2c0, sonic_io_0
20
21    M5.begin()
22    Widgets.fillScreen(0x222222)
23    title1 = Widgets.Title(
24        "UltrasoundUnit CoreS3 Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18
25    )
26    label0 = Widgets.Label("label0", 2, 116, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
27
28    i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
29    sonic_io_0 = UltrasoundIOUnit(port=(8, 9))
30
31
32def loop():
33    global title1, label0, i2c0, sonic_io_0
34    M5.update()
35    label0.setText(str((str("Distance:") + str((sonic_io_0.get_target_distance(1))))))
36
37
38if __name__ == "__main__":
39    try:
40        setup()
41        while True:
42            loop()
43    except (Exception, KeyboardInterrupt) as e:
44        try:
45            from utility import print_error_msg
46
47            print_error_msg(e)
48        except ImportError:
49            print("please update to latest firmware")

UiFlow2 应用示例

example.png

ultrasonicio_cores3_example.m5f2

class UltrasoundIOUnit

Constructors

class UltrasoundIOUnit(port, echo_timeout_us)

使用指定端口和 echo 超时时间初始化 ultrasonic unit。

参数:
  • port – 一个用于表示触发引脚(输出)和回响引脚(输入)的端口引脚元组。

  • echo_timeout_us (int) – echo 信号的超时时间(单位:微秒),默认值为 1,000,000。

UiFlow2

init.png

Methods

UltrasoundIOUnit.tx_pulse_rx_echo()

发送触发脉冲,并等待接收回波响应。

UltrasoundIOUnit.get_target_distance(mode)

根据回波响应时间计算到目标的距离。

参数:

mode (int) – 距离的测量单位。使用 1 表示毫米,2 表示厘米。

返回:

以指定单位表示的到目标的距离。

UiFlow2

get_target_distance.png