ESP32 TOF10120 Laser Distance (Time of Flight) Sensor
The TOF10120 is an advanced, high-performance laser distance sensor that uses time-of-flight technology to measure distances with remarkable accuracy and speed. Its versatility in supporting both I2C and UART communication makes it ideal for a wide range of applications, including robotics, smart devices, and industrial automation.
Jump to Code Examples
Quick Links
TOF10120 Price
About TOF10120 Laser Distance (Time of Flight) Sensor
The TOF10120 is a compact, high-performance laser distance sensor designed for accurate distance measurements. Using time-of-flight (TOF) technology, it measures distances up to 180 cm with high precision and reliability. It supports both I2C and UART communication, making it versatile for integration into a variety of projects. The sensor is widely used in robotics, obstacle detection, and proximity sensing applications.TOF10120 Sensor Technical Specifications
Below you can see the TOF10120 Laser Distance (Time of Flight) Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C/UART
- Measurement Range: 10 cm to 180 cm
- Accuracy: ±2 cm
- Interface: I2C/UART
- Operating Voltage: 3.3V to 5V
- Power Consumption: <30mA
TOF10120 Sensor Pinout
Below you can see the pinout for the TOF10120 Laser Distance (Time of Flight) Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The TOF10120 pinout is straightforward, offering both I2C and UART communication options:
- VCC: Connect to a 3.3V or 5V power supply.
- GND: Ground connection to complete the circuit.
- SDA: Data line for I2C communication.
- SCL: Clock line for I2C communication.
- TX: Transmit pin for UART communication.
- RX: Receive pin for UART communication.
TOF10120 Wiring with ESP32
Below you can see the wiring for the TOF10120 Laser Distance (Time of Flight) Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
SDA
and SCL
to the respective pins on the microcontroller, along with VCC
and GND
. For UART, connect TX
and RX
to the corresponding UART pins on the microcontroller. Configure the communication protocol as required by your application.Code Examples
Below you can find code examples of TOF10120 Laser Distance (Time of Flight) Sensor with ESP32 in several frameworks:
ESP32 TOF10120 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the TOF10120 Laser Distance (Time of Flight) Sensor:
#include <Wire.h>
#define TOF10120_ADDR 0x52
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("TOF10120 Distance Sensor Example");
}
void loop() {
uint16_t distance = getDistance();
if (distance != 0) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" mm");
} else {
Serial.println("Sensor error or no object detected.");
}
delay(500);
}
uint16_t getDistance() {
Wire.beginTransmission(TOF10120_ADDR);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(TOF10120_ADDR, 2);
if (Wire.available() == 2) {
uint16_t distance = Wire.read() << 8 | Wire.read();
return distance;
}
return 0;
}
This Arduino sketch demonstrates how to use the TOF10120 sensor with I2C communication. It begins by setting up the I2C interface using the Wire library and defines the TOF10120 I2C address (0x52
). The getDistance()
function sends a request to the sensor to initiate a distance measurement and then reads the resulting two-byte data. The calculated distance (in millimeters) is displayed on the Serial Monitor, with a 500 ms delay between readings.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 TOF10120 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the TOF10120 Laser Distance (Time of Flight) Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/i2c.h"
#include "esp_log.h"
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_FREQ_HZ 100000
#define TOF10120_ADDR 0x52
static const char *TAG = "TOF10120";
void app_main() {
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &i2c_config));
ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER, 0, 0, 0));
uint8_t data[2];
while (1) {
i2c_master_write_to_device(I2C_MASTER_NUM, TOF10120_ADDR, NULL, 0, 1000 / portTICK_PERIOD_MS);
i2c_master_read_from_device(I2C_MASTER_NUM, TOF10120_ADDR, data, 2, 1000 / portTICK_PERIOD_MS);
uint16_t distance = (data[0] << 8) | data[1];
ESP_LOGI(TAG, "Distance: %d mm", distance);
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}
The code sets up I2C communication using the ESP-IDF framework. The sensor is configured with its I2C address (0x52
). A loop sends a command to read distance data from the sensor and parses the response into a 16-bit distance value (in millimeters). The distance is logged every 500 ms. Error handling ensures the loop continues if communication issues occur.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 TOF10120 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the TOF10120 Laser Distance (Time of Flight) Sensor
sensor:
- platform: tof10120
name: "TOF10120 Distance"
update_interval: 500ms
The ESPHome configuration uses the tof10120
platform to interface with the sensor. The name
assigns a user-friendly label ('TOF10120 Distance') to the sensor data, making it identifiable in smart home platforms. The update_interval
is set to 500 ms for frequent distance updates.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 TOF10120 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
ESP32 TOF10120 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the TOF10120 Laser Distance (Time of Flight) Sensor:
#include <Wire.h>
#define TOF10120_ADDR 0x52
void setup() {
Serial.begin(115200);
Wire.begin();
Serial.println("TOF10120 Distance Sensor Example");
}
void loop() {
uint16_t distance = getDistance();
if (distance != 0) {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" mm");
} else {
Serial.println("Sensor error or no object detected.");
}
delay(500);
}
uint16_t getDistance() {
Wire.beginTransmission(TOF10120_ADDR);
Wire.write(0x00);
Wire.endTransmission();
Wire.requestFrom(TOF10120_ADDR, 2);
if (Wire.available() == 2) {
uint16_t distance = Wire.read() << 8 | Wire.read();
return distance;
}
return 0;
}
This PlatformIO code demonstrates how to interface with the TOF10120 sensor using I2C communication. The code initializes the I2C bus and continuously requests distance measurements from the sensor. The getDistance()
function sends a command to the sensor and retrieves a 2-byte distance value, which is then displayed on the Serial Monitor every 500 ms. The 0x52
I2C address is used for communication. This example is compatible with Arduino-compatible ESP32 boards using the PlatformIO environment.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 TOF10120 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the TOF10120 Laser Distance (Time of Flight) Sensor with your ESP32.
from machine import I2C, Pin
from time import sleep
# Initialize I2C communication (SDA = GPIO21, SCL = GPIO22)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# TOF10120 I2C Address
tof_address = 0x52
def read_distance():
# Request distance measurement
i2c.writeto(tof_address, b'\x00')
data = i2c.readfrom(tof_address, 2)
# Parse two-byte response
distance = (data[0] << 8) | data[1]
return distance
print("TOF10120 Distance Sensor Example")
while True:
distance = read_distance()
print("Distance: {} mm".format(distance))
sleep(0.5)
The MicroPython script uses I2C communication to interact with the TOF10120 sensor. The read_distance()
function sends a command to the sensor and reads a 2-byte response, converting it to a 16-bit distance value. The distance is displayed on the console every 500 ms.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of TOF10120 Laser Distance (Time of Flight) Sensor, its pinout, connection with ESP32 and TOF10120 Laser Distance (Time of Flight) Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.