Ultrasonic Unit
Support the following products:
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 I2C 9from hardware import Pin 10from unit import UltrasoundI2CUnit 11 12 13label0 = None 14i2c0 = None 15ultrasonic_0 = None 16 17 18def setup(): 19 global label0, i2c0, ultrasonic_0 20 21 M5.begin() 22 Widgets.fillScreen(0x222222) 23 label0 = Widgets.Label("label0", 132, 109, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18) 24 25 i2c0 = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000) 26 ultrasonic_0 = UltrasoundI2CUnit(i2c0) 27 28 29def loop(): 30 global label0, i2c0, ultrasonic_0 31 M5.update() 32 label0.setText(str(ultrasonic_0.get_target_distance(1))) 33 34 35if __name__ == "__main__": 36 try: 37 setup() 38 while True: 39 loop() 40 except (Exception, KeyboardInterrupt) as e: 41 try: 42 from utility import print_error_msg 43 44 print_error_msg(e) 45 except ImportError: 46 print("please update to latest firmware")
UIFLOW2 Example:
class UltrasoundI2CUnit
Constructors
Import with from unit import UltrasoundI2CUnit.
- class UltrasoundI2CUnit(i2c, address=0x57)
Create a ULTRASONIC I2C object.
- The parameters is:
i2cis an initialized I2C bus or PAHUB port.addressis the I2C address (default 0x57).
UIFLOW2:

Methods
- UltrasoundI2CUnit.get_target_distance()
Acquire transmitting distance
UIFLOW2:


