What is ToF (Time-of-Flight) Technology?
Time-of-Flight (ToF) is a method of distance measurement that calculates how long it takes for a signal (usually a light or laser pulse) to travel to a target and back to the sensor. The ToF HAT uses this principle with a laser light source and highly sensitive detectors to calculate the distance with high accuracy.
-
Light Emission: The sensor emits a laser pulse.
-
Reflection: The pulse reflects off a target object.
-
Time Measurement: The sensor records the time it took for the light to return.
-
Distance Calculation: Using the speed of light and the time it takes for the pulse to return, the sensor calculates the distance to the object.

This method of distance measurement is highly accurate and operates independently of surface reflectivity, a significant advantage over traditional ultrasonic sensors.
How to Use The ToF Hat
Materials required
-
M5StickCPlus2: Acts as the core controller and is responsible for displaying data
-
M5StickC ToF HAT: High-precision laser distance sensors for distance measurement
Code Part
import os, sys, io
import M5
from M5 import *
from hardware import *
from hat import ToFHat
label0 = None
label1 = None
i2c0 = None
pin19 = None
pwm2 = None
hat_tof_0 = None
def setup():
global label0, label1, i2c0, pin19, pwm2, hat_tof_0
M5.begin()
label0 = Widgets.Label("label0", 0, 0, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
label1 = Widgets.Label("label1", 100, 0, 1.0, 0xffffff, 0x222222, Widgets.FONTS.DejaVu18)
Widgets.setRotation(1)
i2c0 = I2C(0, scl=Pin(26), sda=Pin(0), freq=100000)
hat_tof_0 = ToFHat(i2c0)
pin19 = Pin(19, mode=Pin.OUT)
pwm2 = PWM(Pin(2), freq=5000, duty=512)
def loop():
global label0, label1, i2c0, pin19, pwm2, hat_tof_0
M5.update()
label0.setColor(0x33ff33, 0x000000)
label1.setColor(0x33ff33, 0x000000)
label0.setText(str('distance'))
label1.setText(str(hat_tof_0.get_distance()))
if (hat_tof_0.get_distance()) < 10:
pin19.value(1)
pwm2.duty(512)
else:
pin19.value(0)
pwm2.duty(0)
if __name__ == '__main__':
try:
setup()
while True:
loop()
except (Exception, KeyboardInterrupt) as e:
try:
from utility import print_error_msg
print_error_msg(e)
except ImportError:
print("please update to latest firmware")
M5Stack UIflow
Initialize the IIC, hat, set GPIO19 to output mode to control the light on/off, and set GPIO2 to PWM frequency, and duty cycle to control the buzzer.

Display the distance measured on the M5stickCplus2 and set the distance at which you would like to have a warning.

Key Components of M5StickC ToF HAT
Laser Light Source:
The laser pulse is sent out and reflects back from the object, enabling distance measurement.
Sensor Array:
The sensor contains photodetectors (often an APD - Avalanche Photodiode) that measure the time it takes for the laser to return.These detectors are designed to be extremely sensitive to the incoming reflected light.I2C Interface:
I2C is a simple, low-power communication protocol that enables the M5StickC to communicate with the sensor with just two wires: data (SDA) and clock (SCL), alongside power and ground.
Power Supply:
The module typically operates on 3.3V or 5V, which is supplied through the I2C connection to the M5StickC.This allows the sensor to run efficiently, with low power consumption suitable for battery-powered applications.
Technical Specifications in Detail
Here’s a more comprehensive list of specifications for the M5StickC ToF HAT:
-
Distance Range: The sensor can measure distances from 0.05 meters (5 cm) to 2 meters with high accuracy.
-
Measurement Accuracy: The sensor offers an accuracy of ± 3% of the measured distance. This means, for instance, if an object is detected at 1 meter, the distance measurement can vary by approximately 3 cm.
-
Response Time: The sensor can provide distance readings approximately every 50 milliseconds, depending on the distance. It’s fast enough for real-time applications like obstacle detection and robotic navigation.
-
Operating Voltage: The operating voltage is between 3.3V and 5V, making it compatible with the M5StickC and other microcontrollers that operate in this voltage range.
-
Power Consumption: The power consumption is quite low, typically consuming around 15-20mA during operation. This makes it ideal for battery-powered or low-power systems.
-
Operating Temperature: The sensor operates effectively in a temperature range of -10°C to 60°C, allowing it to be used in a wide variety of environments.
-
Output Data: The sensor outputs the distance data through I2C communication, which can be easily read by any compatible microcontroller, including the M5StickC.
-
Detection Mode: The ToF HAT can be used in both continuous measurement mode and triggered measurement mode, depending on the specific application.
How the M5StickC ToF HAT Works
The sensor works by emitting a laser pulse and then waiting for the pulse to bounce back from a nearby object. Here's a breakdown of how this process happens:Laser Pulse Emission:The sensor emits a short laser pulse that travels at the speed of light. The laser beam reflects off objects that it encounters.Reflected Light Detection:The reflected light is received by the sensor’s photodetector. This light is then used to measure the travel time between the emission and reception of the pulse.
Distance Calculation:
The sensor calculates the distance by using the formula:
Distance=c×t2\text{Distance} = \frac{c \times t}{2}Distance=2c×t
where:
-
CCC is the speed of light in the medium (approx. 3×1083 \times 10^83×108 meters per second).
-
it is the time taken for the light pulse to travel to the target and back.
-
Data Output: Once the distance is calculated, the sensor outputs the value via I2C. The M5StickC can then read the value and process it for display, actions, or further computation.
Using M5StickC ToF HAT in Projects
-
Proximity Sensing for Smart Home Systems: The sensor can be used in smart home applications to detect when someone is near a door or entrance. For example, you could set up the system to turn on lights when someone approaches or triggers an action when someone stands within a certain range.
-
Measuring Objects in Industrial Applications: The ToF HAT can also be used in industrial automation to measure the size or position of objects on a production line or to track parts in motion.
-
Virtual Reality (VR) and Augmented Reality (AR): In VR/AR setups, accurate depth and distance sensing are critical for immersion and interaction. The ToF HAT can be used to track the distance between the user and objects within the VR/AR environment.
Integration with M5StickC
- Hardware Setup:
- Plug the ToF HAT into the I2C port of the M5StickC. The M5StickC will automatically power and communicate with the ToF sensor through the I2C bus.
- Programming:
- Alternatively, you can use Arduino IDE or MicroPython to write custom code, allowing for more control and flexibility in your projects.
- You can use UIFlow, the graphical programming platform, to interact with the sensor and read the distance values. UIFlow makes it easy to program the M5StickC and access the ToF sensor's data with just a few blocks.
- Example UIFlow Block Program:
- In UIFlow, you can use blocks to initialize the sensor, read the distance, and display the result on the screen or take action based on the measured distance.
- Data Processing:
- Once you’ve retrieved the distance data, you can process it and trigger actions. For example:
- If the distance is less than a threshold, activate a buzzer or sound an alarm.
- If you're building a robot, change its direction when an obstacle is detected.
Conclusion
