UltrasoundIO Unit

UNIT SONIC IO is a GPIO interface ultrasonic range sensor. This module features an RCWL-9620 ultrasonic distance measurement chip with a 16mm probe, which the ranging accuracy can reach 2cm-450cm (accuracy up to ±2%). This sensor determines the distance to a target by measuring time lapses between the transmitting and receiving of the pulse signal, users can directly obtain the distance value through IO control mode. It is ideal to apply in robotics obstacle avoidance, fluid level detection, and other applications that require you to perform measurements.

Support the following products:

UltrasoundIOUnit

Micropython Example:

 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:

example.png

ultrasonicio_cores3_example.m5f2

class UltrasoundIOUnit

Constructors

class UltrasoundIOUnit(port, echo_timeout_us)

Initialize the ultrasonic unit with the specified port and echo timeout.

Parameters:
  • port – A tuple representing the port pins for trigger (output) and echo (input).

  • echo_timeout_us (int) – Timeout for the echo signal in microseconds, default is 1,000,000.

UIFLOW2:

init.png

Methods

UltrasoundIOUnit.tx_pulse_rx_echo()

Send a trigger pulse and wait to receive the echo response.

UltrasoundIOUnit.get_target_distance(mode)

Calculate the distance to the target based on echo response time.

Parameters:

mode (int) – The unit of measurement for the distance. Use 1 for millimeters, 2 for centimeters.

Returns:

The distance to the target in the specified unit.

UIFLOW2:

get_target_distance.png