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 13title0 = None 14label_dis = None 15bus2 = None 16chain_tof_0 = None 17last_time = None 18distance = None 19 20 21def setup(): 22 global title0, label_dis, bus2, chain_tof_0, last_time, distance 23 24 M5.begin() 25 Widgets.setRotation(0) 26 Widgets.fillScreen(0x222222) 27 title0 = Widgets.Title("Chain ToF Example", 3, 0xFFFFFF, 0x0000FF, Widgets.FONTS.DejaVu24) 28 label_dis = Widgets.Label( 29 "Distance: --", 20, 92, 1.0, 0xFFFFFF, 0x222222, Widgets.FONTS.DejaVu24 30 ) 31 32 bus2 = ChainBus(2, tx=21, rx=22) 33 chain_tof_0 = ToFChain(bus2, 1) 34 chain_tof_0.set_rgb_color(0x33CCFF) 35 chain_tof_0.set_rgb_brightness(10, save=False) 36 chain_tof_0.set_measure_mode(ToFChain.MODE_CONTINUOUS) 37 38 39def loop(): 40 global title0, label_dis, bus2, chain_tof_0, last_time, distance 41 M5.update() 42 if (time.ticks_diff((time.ticks_ms()), last_time)) >= 200: 43 last_time = time.ticks_ms() 44 distance = chain_tof_0.get_distance() 45 label_dis.setText(str((str("Distance: ") + str((str(distance) + str(" mm")))))) 46 47 48if __name__ == "__main__": 49 try: 50 setup() 51 while True: 52 loop() 53 except (Exception, KeyboardInterrupt) as e: 54 try: 55 bus2.deinit() 56 from utility import print_error_msg 57 58 print_error_msg(e) 59 except ImportError: 60 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()


