Skip to main content
ESPBoards

ESP32 VL53L0X Time-of-Flight Distance Sensor

The VL53L0X is an advanced Time-of-Flight distance sensor offering accurate, laser-based measurements over a range of 30 mm to 2,000 mm. It features low power consumption, compact size, and fast response times, making it ideal for integration into various IoT and robotics applications. The I2C communication protocol simplifies its use with microcontrollers and SBCs like Arduino and Raspberry Pi.

Jump to Code Examples

Arduino Core Image
ESP-IDF Image
ESPHome Image
PlatformIO Image
MicroPython Image

Quick Links

VL53L0X Time-of-Flight Distance Sensor Datasheet ButtonVL53L0X Time-of-Flight Distance Sensor Specs ButtonVL53L0X Time-of-Flight Distance Sensor Specs ButtonVL53L0X Time-of-Flight Distance Sensor Specs Button

VL53L0X Price

Normally, the VL53L0X Time-of-Flight Distance Sensor costs around 5$ per Psc.
The prices are subject to change. Check current price:

Amazon com

Amazon de logo

Aliexpress logo

About VL53L0X Time-of-Flight Distance Sensor

The VL53L0X is a state-of-the-art Time-of-Flight (ToF) distance sensor from STMicroelectronics. It uses laser-based technology to accurately measure distances up to 2 meters. With its compact size, low power consumption, and fast measurement capability, this sensor is ideal for robotics, gesture recognition, obstacle detection, and other applications. It communicates via the I2C protocol, making it easy to interface with microcontrollers like Arduino and ESP32.

VL53L0X Sensor Technical Specifications

Below you can see the VL53L0X Time-of-Flight Distance 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
  • Measurement Range: 30 mm to 2000 mm
  • Accuracy: ±3% under ideal conditions
  • Interface: I2C
  • Operating Voltage: 2.6V to 5.5V (typical 3.3V or 5V)
  • Power Consumption: <20mA during operation

VL53L0X Sensor Pinout

Below you can see the pinout for the VL53L0X Time-of-Flight Distance 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 VL53L0X pinout is as follows:

  • VIN: Connect to a 3.3V or 5V power supply.
  • GND: Connect to the ground of your microcontroller.
  • SCL: I2C clock line, connect to the SCL pin of your microcontroller.
  • SDA: I2C data line, connect to the SDA pin of your microcontroller.
  • XSHUT: Shutdown pin, can be used to reset the sensor (active low).
  • GPIO1: Interrupt pin, optional for advanced configurations.

VL53L0X Wiring with ESP32

Below you can see the wiring for the VL53L0X Time-of-Flight Distance 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.

To connect the VL53L0X to a microcontroller like the ESP32, use the I2C protocol. Connect the SCL and SDA pins to the respective I2C pins on the microcontroller. The VIN pin should be connected to a 3.3V or 5V power source, and GND to ground. If needed, the XSHUT pin can be used to reset the sensor, and GPIO1 can provide interrupt signals for advanced use cases.

Code Examples

Below you can find code examples of VL53L0X Time-of-Flight Distance Sensor with ESP32 in several frameworks:

Arduino Core Image

ESP32 VL53L0X Arduino IDE Code Example

Example in Arduino IDE

Fill in your main Arduino IDE sketch file with the following code to use the VL53L0X Time-of-Flight Distance Sensor:

#include <Wire.h>
#include <Adafruit_VL53L0X.h>

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
Serial.begin(115200);
Wire.begin();

if (!lox.begin()) {
Serial.println("Failed to initialize VL53L0X! Check connections.");
while (1);
}
Serial.println("VL53L0X Initialized.");
}

void loop() {
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);

if (measure.RangeStatus != 4) { // Status 4 means no object detected
Serial.print("Distance: ");
Serial.print(measure.RangeMilliMeter);
Serial.println(" mm");
} else {
Serial.println("Out of range.");
}

delay(500);
}

This Arduino code demonstrates how to use the VL53L0X sensor with the Adafruit library. It initializes the sensor using the lox.begin() method. In the loop(), the rangingTest() method measures the distance to an object in millimeters, which is printed to the Serial Monitor every 500 ms. If no object is detected or the object is out of range, a corresponding message is displayed. The I2C protocol facilitates communication with the sensor.

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.

ESP-IDF Image

ESP32 VL53L0X ESP-IDF Code Example
Example in Espressif IoT Framework (ESP-IDF)

If you're using ESP-IDF to work with the VL53L0X Time-of-Flight Distance 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 VL53L0X_ADDR 0x29

static const char *TAG = "VL53L0X";

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));

ESP_LOGI(TAG, "VL53L0X initialized.");
while (1) {
// Placeholder for distance measurement implementation
ESP_LOGI(TAG, "Reading distance...");
vTaskDelay(500 / portTICK_PERIOD_MS);
}
}

This ESP-IDF example configures the VL53L0X sensor for use with I2C. The I2C_MASTER_NUM defines the I2C port, and GPIO21 and GPIO22 are used as the SDA and SCL lines. While the provided code sets up the I2C communication, additional functions for sending measurement commands and reading data from the VL53L0X would need to be implemented to perform distance measurements. The example demonstrates how to initialize the sensor and prepare for measurements.

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.

ESPHome Image

ESP32 VL53L0X ESPHome Code Example

Example in ESPHome (Home Assistant)

Fill in this configuration in your ESPHome YAML configuration file (example.yml) to integrate the VL53L0X Time-of-Flight Distance Sensor

sensor:
- platform: vl53l0x
name: "VL53L0X Distance"
update_interval: 500ms

This ESPHome configuration uses the vl53l0x platform to interface with the sensor. The sensor's name, 'VL53L0X Distance,' makes it easily identifiable in home automation platforms like Home Assistant. The update_interval of 500 ms ensures frequent updates for applications requiring real-time distance measurements.

Upload this code to your ESP32 using the ESPHome dashboard or the esphome run command.

PlatformIO Image

ESP32 VL53L0X PlatformIO Code Example

Example in PlatformIO Framework

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
lib_deps =
adafruit/Adafruit VL53L0X @ ^1.2.0
monitor_speed = 115200

ESP32 VL53L0X PlatformIO Example Code

Write this code in your PlatformIO project under the src/main.cpp file to use the VL53L0X Time-of-Flight Distance Sensor:

#include <Wire.h>
#include <Adafruit_VL53L0X.h>

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
Serial.begin(115200);
Wire.begin();

if (!lox.begin()) {
Serial.println("Failed to initialize VL53L0X! Check connections.");
while (1);
}
Serial.println("VL53L0X Initialized.");
}

void loop() {
VL53L0X_RangingMeasurementData_t measure;
lox.rangingTest(&measure, false);

if (measure.RangeStatus != 4) { // Status 4 means no object detected
Serial.print("Distance: ");
Serial.print(measure.RangeMilliMeter);
Serial.println(" mm");
} else {
Serial.println("Out of range.");
}

delay(500);
}

The PlatformIO code uses the Adafruit VL53L0X library to interface with the sensor. After initializing I2C communication with Wire.begin(), the sensor is configured using lox.begin(). The rangingTest() function retrieves distance measurements in millimeters, which are printed to the Serial Monitor every 500 ms. A status check ensures meaningful data is displayed, and the code gracefully handles 'out of range' cases.

Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload command.

MicroPython Image

ESP32 VL53L0X MicroPython Code Example

Example in Micro Python Framework

Fill in this script in your MicroPython main.py file (main.py) to integrate the VL53L0X Time-of-Flight Distance Sensor with your ESP32.

from machine import I2C, Pin
from vl53l0x import VL53L0X

# Initialize I2C (SDA=21, SCL=22)
i2c = I2C(0, scl=Pin(22), sda=Pin(21))

# Initialize VL53L0X sensor
sensor = VL53L0X(i2c)

print("VL53L0X Distance Sensor Example")

while True:
distance = sensor.read() # Distance in mm
print("Distance: {} mm".format(distance))
time.sleep(0.5)

This MicroPython script uses the VL53L0X library to interface with the sensor. The I2C pins (GPIO21 for SDA and GPIO22 for SCL) are initialized first. The sensor object is created, and the read() method is called in a loop to fetch distance measurements in millimeters. The measurements are displayed on the console every 500 ms. The library simplifies communication with the sensor, handling low-level I2C details internally.

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 VL53L0X Time-of-Flight Distance Sensor, its pinout, connection with ESP32 and VL53L0X Time-of-Flight Distance Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.