VL53L0X: measure distance with the flight time of light
The VL53L0X from ST sends very short pulses of invisible 940 nm laser light from a VCSEL (a small surface-emitting laser). A grid of single-photon detectors (SPADs) receives the light that comes back. The chip measures the time of flight. Light travels 1 m in about 3.3 ns, so the chip must measure picoseconds. It gives the distance in millimeters on I²C (address 0x29). The range is up to about 2 m. The light is eye-safe (Class 1).

Photo: Adafruit (Kattni Rembor), CC BY-SA 3.0
Drag the target, or use the slider. Each dot below is one reading. The gray line is the real distance. The band shows ±1 standard deviation.
How it works
Time of flight
Distance = speed of light × time ÷ 2. The chip does not time a single pulse. It sends many pulses and builds a histogram of the photon arrival times. More photons give a more accurate result. The field of view is a cone of about 25°.
Signal: reflectance and distance
The returned light decreases with the square of the distance. A dark surface returns less light than a white surface. When the signal rate is less than the limit (0.25 MCPS by default), the sensor reports “out of range”. A black target can disappear at 0.3–0.5 m.
Ambient light
Sunlight contains much 940 nm infrared. The sensor counts these photons as noise. Outdoors in the sun, the noise increases and the range decreases a lot. Indoors under LED or fluorescent lights, the effect is small.
Timing budget
The timing budget is the time for one measurement (20 ms minimum, 33 ms default). A 200 ms budget gives less noise but only 5 readings per second. A 20 ms budget gives fast readings with more noise. The noise decreases with the square root of the time.
Many sensors: XSHUT
All VL53L0X chips start at address 0x29. The address is not stored after power-off. Hold all sensors in reset with their XSHUT pins.
Then release one sensor at a time and give it a new address with setAddress().
Related sensors
The VL53L1X measures up to 4 m and has a selectable region of interest. The VL53L4CD measures from 1 mm up to about 1.3 m and is better at short range. Both use the same I²C address and a different library. An ultrasonic sensor costs less and sees glass and black objects better, but it has a much wider beam and is slower.
Wiring
Connections
| Breakout pin | ESP32 | Notes |
|---|---|---|
| VIN | 3V3 | 2.6–5.5 V on boards with a regulator. |
| GND | GND | Common ground. |
| SCL | GPIO 22 | I²C clock. The breakout has pull-ups. |
| SDA | GPIO 21 | I²C data. |
| XSHUT | GPIO 25 | LOW = shutdown. Pulled HIGH on the breakout. Needed only for more than one sensor. |
| GPIO1 | — | “Data ready” interrupt output. Optional. |
Build steps
- Remove the filmRemove the orange or yellow protective film from the sensor window.
- Connect four wiresConnect VIN, GND, SCL and SDA. You can also use a STEMMA QT / Qwiic cable.
- Install the libraryIn Library Manager, search for “VL53L0X”. Install “VL53L0X” by Pololu.
- MeasureUpload sketch 1. Point the sensor at a wall. Compare the value with a tape measure.
- TuneSet the timing budget for your speed and noise needs. For long distances, use long-range mode.
Code
These sketches use the VL53L0X library by Pololu. The Adafruit library Adafruit_VL53L0X is a port of the full ST API and is also a good choice. Its function names are different.
1 · Continuous readings
vl53l0x_basic.ino
#include <Wire.h>
#include <VL53L0X.h>
VL53L0X tof;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
tof.setTimeout(500);
if (!tof.init()) {
Serial.println("No VL53L0X found. Examine the wiring.");
while (true) delay(1000);
}
tof.setMeasurementTimingBudget(33000); // microseconds (min 20000)
tof.startContinuous(); // back-to-back readings
}
void loop() {
uint16_t mm = tof.readRangeContinuousMillimeters();
if (tof.timeoutOccurred()) {
Serial.println("Timeout");
} else if (mm >= 8000) {
Serial.println("Out of range"); // no valid target
} else {
Serial.printf("%u mm\n", mm);
}
}
For long range, add tof.setSignalRateLimit(0.1);, tof.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
and tof.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); before startContinuous().
2 · Two sensors with XSHUT
vl53l0x_two.ino
#include <Wire.h>
#include <VL53L0X.h>
const int XSHUT_LEFT = 25, XSHUT_RIGHT = 26;
VL53L0X left, right;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22);
pinMode(XSHUT_LEFT, OUTPUT);
pinMode(XSHUT_RIGHT, OUTPUT);
digitalWrite(XSHUT_LEFT, LOW); // both in shutdown
digitalWrite(XSHUT_RIGHT, LOW);
delay(10);
digitalWrite(XSHUT_LEFT, HIGH); // wake the first sensor only
delay(10);
left.setTimeout(500);
left.init();
left.setAddress(0x30); // new address, until power-off
digitalWrite(XSHUT_RIGHT, HIGH); // the second sensor keeps 0x29
delay(10);
right.setTimeout(500);
right.init();
left.startContinuous();
right.startContinuous();
}
void loop() {
Serial.printf("L %4u mm R %4u mm\n",
left.readRangeContinuousMillimeters(),
right.readRangeContinuousMillimeters());
}
Troubleshooting
init() fails
Run an I²C scanner. The sensor must answer at 0x29. Make sure that SDA and SCL are not swapped. If you use two sensors, make sure that both do not have the same address.
The values are too short or jump
The protective film is still on, or a cover glass reflects light back into the sensor. Dust and fingerprints have the same effect. Use a cover that ST approves, or no cover.
“Out of range” on dark or far objects
Use long-range mode and a longer timing budget. Put a white target on the object if you can. In sunlight, add a shade around the sensor.
Resources
- ST VL53L0X datasheetRanging modes, timing budget, accuracy tables and the ambient-light limits.
- Pololu VL53L0X libraryThe API that the sketches use, with examples for long range and high accuracy.
- Adafruit VL53L0X guideBreakout pinout and the Adafruit_VL53L0X library.
- Ultrasonic distance sensorAn alternative with sound, for longer range and a wide beam.
- OLED displayShow the distance as a number or a bar graph.
- ESP32 GPIOI²C pins and safe pins for XSHUT.