Chain ToF
ToFChain is the helper class for ToF (Time of Flight) sensor devices on the Chain bus. It provides methods to read distance measurements, configure measurement parameters (time, mode, status), and check measurement completion status.
Support the following products:
UiFlow2 Example
Distance measurement display
Open the m5core_chain_tof_basic_example.m5f2 project in UiFlow2.
This example demonstrates how to read distance measurements from the Chain ToF sensor and display them on screen. The example configures the sensor for continuous measurement mode and updates the distance value in real-time.
UiFlow2 Code Block:
Example output:
None
MicroPython Example
Distance measurement display
This example demonstrates how to read distance measurements from the Chain ToF sensor and display them on screen. The example configures the sensor for continuous measurement mode and updates the distance value in real-time.
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 chain import ChainBus 9from chain import ToFChain 10import time 11 12 13 14title0 = None 15label_dis = None 16bus2 = None 17chain_tof_0 = None 18last_time = None 19distance = None 20 21 22def setup(): 23 global title0, label_dis, bus2, chain_tof_0, last_time, distance 24 25 M5.begin() 26 Widgets.setRotation(1) 27 Widgets.fillScreen(0x222222) 28 title0 = Widgets.Title("Chain ToF Example", 3, 0xffffff, 0x0000FF, Widgets.FONTS.DejaVu24) 29 label_dis = Widgets.Label("Distance: --", 20, 92, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu24) 30 31 bus2 = ChainBus(2, tx=21, rx=22) 32 chain_tof_0 = ToFChain(bus2, 1) 33 chain_tof_0.set_rgb_color(0x33ccff) 34 chain_tof_0.set_rgb_brightness(10, save=False) 35 chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS) 36 37 38def loop(): 39 global title0, label_dis, bus2, chain_tof_0, last_time, distance 40 M5.update() 41 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 200: 42 last_time = time.ticks_ms() 43 distance = chain_tof_0.get_distance() 44 label_dis.setText(str((str('Distance: ') + str(((str(distance) + str(' mm'))))))) 45 46 47if __name__ == '__main__': 48 try: 49 setup() 50 while True: 51 loop() 52 except (Exception, KeyboardInterrupt) as e: 53 try: 54 bus2.deinit() 55 from utility import print_error_msg 56 print_error_msg(e) 57 except ImportError: 58 print("please update to latest firmware")
Example output:
None
API
ToFChain
- class chain.tof.ToFChain(bus, device_id)
Bases:
KeyChainToF Chain class for interacting with ToF (Time of Flight) devices over Chain bus.
- Parameters:
UiFlow2 Code Block:

MicroPython Code Block:
from chain import ChainBus from chain import ToFChain bus2 = ChainBus(2, tx=21, rx=22) chain_tof_0 = ToFChain(bus2, 1)
- get_distance()
Get the distance measurement.
- Returns:
Distance in millimeters, or None if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
distance = chain_tof_0.get_distance()
- set_measure_time(time_ms=33)
Set the measurement time.
- Parameters:
time_ms (int) – Measurement time in milliseconds. Range: 20-200, default: 33.
- Returns:
True if the operation was successful, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = chain_tof_0.set_measure_time(33)
- get_measure_time()
Get the measurement time.
- Returns:
Measurement time in milliseconds, or None if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
time = chain_tof_0.get_measure_time()
- set_measure_mode(mode=2)
Set the measurement mode.
- Parameters:
mode (int) – Measurement mode. Use
ToFChain.MODE_STOP(0),ToFChain.MODE_SINGLE(1), orToFChain.MODE_CONTINUOUS(2). Default: 2.- Returns:
True if the operation was successful, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS)
- get_measure_mode()
Get the measurement mode.
- Returns:
Measurement mode.
ToFChain.MODE_STOP(0),ToFChain.MODE_SINGLE(1), orToFChain.MODE_CONTINUOUS(2). Returns None if failed.- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
mode = chain_tof_0.get_measure_mode()
- set_measure_status(status)
Set the measurement status.
- Parameters:
status (int) – Measurement status. 0 means not measuring, 1 means measuring.
- Returns:
True if the operation was successful, False otherwise.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
success = chain_tof_0.set_measure_status(1)
- get_measure_status()
Get the measurement status.
- Returns:
Measurement status. 0 means not measuring, 1 means measuring. Returns None if failed.
- Return type:
UiFlow2 Code Block:

MicroPython Code Block:
status = chain_tof_0.get_measure_status()


