ToF4M Unit

This is the driver library of ToF4M Unit, which is used to obtain distance data from the VL53L1CXV0FY sensor.

Support the following products:

ToF4M

UiFlow2 Example

get distance value

Open the tof4m_core_example.m5f2 project in UiFlow2.

This example gets the distance value of the ToF4M Unit and displays it on the screen.

UiFlow2 Code Block:

example.png

Example output:

None

MicroPython Example

get distance value

This example gets the distance value of the ToF4M Unit and displays it on the screen.

MicroPython Code Block:

 1# SPDX-FileCopyrightText: 2025 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 TOF4MUnit
11
12
13title0 = None
14label1 = None
15i2c1 = None
16tof4m_0 = None
17
18
19distance = None
20
21
22def setup():
23    global title0, label1, i2c1, tof4m_0, distance
24
25    M5.begin()
26    Widgets.fillScreen(0x222222)
27    title0 = Widgets.Title("ToF4MUnit CoreS3 Test", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu18)
28    label1 = Widgets.Label("label1", 1, 121, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu18)
29
30    i2c1 = I2C(1, scl=Pin(22), sda=Pin(21), freq=100000)
31    tof4m_0 = TOF4MUnit(i2c1, 0x29)
32    tof4m_0.set_distance_mode(2)
33    tof4m_0.set_measurement_timing_budget(500)
34    tof4m_0.set_continuous_start_measurement()
35
36
37def loop():
38    global title0, label1, i2c1, tof4m_0, distance
39    M5.update()
40    if tof4m_0.get_data_ready:
41        label1.setText(str((str("Distance:") + str((str(distance) + str("mm"))))))
42
43
44if __name__ == "__main__":
45    try:
46        setup()
47        while True:
48            loop()
49    except (Exception, KeyboardInterrupt) as e:
50        try:
51            from utility import print_error_msg
52
53            print_error_msg(e)
54        except ImportError:
55            print("please update to latest firmware")

Example output:

None

API

TOF4MUnit

unit.tof4m.TOF4MUnit

alias of VL53L1X

VL53L1CXV0FY

class driver.vl53l1x.VL53L1X(i2c, address=41)

Bases: object

Create a VL53L1X object.

Parameters:
  • i2c (I2C) – The I2C bus the ToF4M Unit is connected to.

  • address (int) – The I2C address of the device. Default is 0x29.

UiFlow2 Code Block:

init.png

MicroPython Code Block:

from hardware import I2C
from unit import TOFUnit

i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
tof_0 = TOFUnit(i2c0)
property get_data_ready

Returns true if new data is ready, otherwise false.

Returns:

True if new data is ready.

Return type:

bool

UiFlow2 Code Block:

get_data_ready.png

MicroPython Code Block:

if tof_0.get_data_ready:
    distance = tof_0.get_distance
property get_distance

The distance in units of millimeters.

Returns:

Distance in millimeters or None if measurement is invalid.

Return type:

int or None

UiFlow2 Code Block:

get_distance.png

MicroPython Code Block:

distance = tof_0.get_distance
property get_distance_mode

Get the distance mode.

Returns:

distance mode(1=short, 2=long).

Return type:

int

UiFlow2 Code Block:

get_distance_mode.png

MicroPython Code Block:

mode = tof_0.get_distance_mode
property get_measurement_timing_budget

Get measurement duration in milliseconds.

Returns:

The timing budget in milliseconds.

Return type:

int

UiFlow2 Code Block:

get_measurement_timing_budget.png

MicroPython Code Block:

budget = tof_0.get_measurement_timing_budget
set_continuous_start_measurement()

Starts continuous measure operation.

UiFlow2 Code Block:

set_continuous_start_measurement.png

MicroPython Code Block:

tof_0.set_continuous_start_measurement()
set_continuous_stop_measurement()

Stops measure operation.

UiFlow2 Code Block:

set_continuous_stop_measurement.png

MicroPython Code Block:

tof_0.set_continuous_stop_measurement()
set_distance_mode(mode)

Set the distance mode.

Parameters:

mode (int) – 1=short, 2=long.

UiFlow2 Code Block:

set_distance_mode.png

MicroPython Code Block:

tof_0.set_distance_mode(2)  # Long distance mode
set_i2c_address(new_address)

Set a new I2C address to the instantiated object.

Parameters:

new_address (int) – The new I2C address.

UiFlow2 Code Block:

set_i2c_address.png

MicroPython Code Block:

tof_0.set_i2c_address(42)
set_measurement_timing_budget(val)

Set the measurement timing budget in milliseconds.

Parameters:

val (int) – Timing budget in milliseconds.

UiFlow2 Code Block:

set_measurement_timing_budget.png

MicroPython Code Block:

tof_0.set_measurement_timing_budget(100)