VL53L4CX Time Of Flight Distance Sensor
The vl53l4cx sensor platform allows you to use VL53L4CX optical time of flight
(datasheet,
ST) sensors with ESPHome
to measure distances up to about 6m. The sensor works optically by emitting short infrared pulses
and measuring the time it takes the light to be reflected back.
Unlike the VL53L1X, the VL53L4CX sees up to four targets in one measurement,
for example a box under part of its field of view and the floor behind it. The target option picks the
one the sensor reports. The measurements are processed by ST’s own driver on the ESP, so each sensor uses
about 9.5 KB of RAM.
The I²C Bus is required to be set up in your configuration for this sensor to work.
Set its frequency to 400kHz if the other devices on the bus allow it: each measurement moves about
150 bytes, which blocks the main loop for about 4ms at 400kHz but about 38ms at 50kHz, the default on most platforms.
ESPHome logs a warning at compile time when the bus is slower.
# Example configuration entrysensor: - platform: vl53l4cx name: "VL53L4CX Distance"The sensor reports the distance in meters, or NaN when no target passes the checks. The first five
measurements after the sensor starts (at boot or after a reset) are not reported, since ST’s driver needs
them to settle.
When a measurement fails or times out, the sensor keeps its last state and the component shows a warning.
After five failed measurements in a row, the state becomes NaN, so a sensor that stopped answering does
not keep reporting its last distance. The sensor is then reset (through enable_pin when set, otherwise by
a software reset) and set up again every 5 seconds until it answers.
Wiring
Section titled “Wiring”VINconnects to 3.3VGNDconnects to groundSCLconnects to I²C SCL (clock)SDAconnects to I²C SDA (data)GPIO1(labelledGPIOon some boards) is the interrupt output, low while a measurement is ready. Connect it to a free input pin and set it asinterrupt_pinto save an I²C read on every loop.XSHUTconnects to a free GPIO pin and is used asenable_pin. It is optional for a single sensor at the default0x29address, and required for every sensor when more than one VL53L4CX shares an I²C bus.
Configuration Variables
Section titled “Configuration Variables”-
distance_mode (Optional, string): The distance mode,
mediumorlong. Defaults tolong. ST’s driver has no short mode for the VL53L4CX. -
timing_budget (Optional, Time): The time allowed for a single distance measurement, from
10msto200ms. A longer budget gives more accurate readings at a lower measurement rate. Defaults to33ms. -
target (Optional, string): Which of the valid targets in a measurement the sensor reports:
nearest,farthestorstrongest(the highest return signal). Defaults tonearest. -
min_distance (Optional, distance): Targets closer than this are ignored, for example dust or insects on the cover glass. Defaults to
0mm. -
offset (Optional, int): Calibration offset in millimeters added to every target, from -1000 to 1000. It applies before
min_distanceis checked. Defaults to0. -
address (Optional, int): The I²C address of the sensor. Defaults to
0x29. If another address is set, the sensor is re-addressed at startup, which requiresenable_pin. When more than one VL53L4CX shares an I²C bus, every sensor must be set to an address other than0x29. -
enable_pin (Optional, Pin Schema): The pin connected to XSHUT on the VL53L4CX. Required when
addressis not0x29, and for every sensor when more than one VL53L4CX shares an I²C bus. -
interrupt_pin (Optional, Pin Schema): The pin connected to GPIO1 on the VL53L4CX. Without it, ESPHome asks the sensor over I²C whether a measurement is ready. Each sensor needs its own pin, and GPIO16 on the ESP8266 cannot be used because it has no interrupts.
-
update_interval (Optional, Time): How often to start a measurement. Defaults to
1s. Set it toneverto measure continuously: each measurement starts as soon as the last one is read, so the sensor reports one everytiming_budget. An interval no longer thantiming_budgetskips updates, and ESPHome warns about it. -
All other options from Sensor.
Choosing a Target
Section titled “Choosing a Target”With several objects in view, the sensor reports up to four targets with their distance and signal strength.
nearestreports the closest valid target, the way a single-target sensor behaves.farthestlooks past partial obstructions. A garage sensor pointed at the floor keeps reporting the floor while the open door or a car roof is at the edge of its view; combine it withmin_distanceso anything on the cover glass is ignored.strongestreports the target that reflects the most light.
# Garage floor sensor that ignores partial obstructionssensor: - platform: vl53l4cx name: "Garage Distance" target: farthest min_distance: 100mm interrupt_pin: GPIOXX update_interval: never filters: - median: window_size: 15 send_every: 15Measuring continuously reports a distance every timing_budget, about 30 times a second at the default.
The median filter above sends one value for every 15 measurements; use a filter like it, or throttle,
so Home Assistant is not flooded with updates.
Multiple Sensors
Section titled “Multiple Sensors”Multiple VL53L4CX sensors can share one I²C bus. All sensors start at the same default address (0x29),
so each one is held in reset with its XSHUT pin while the others are given new addresses:
- Connect the XSHUT pin of each sensor to a different GPIO and set it as
enable_pin - Give each sensor its own
addressother than0x29
ESPHome rejects a configuration where a sensor on a shared bus has no enable_pin, stays at 0x29,
or uses the same address as another VL53L4CX on that bus. A VL53L4CX also cannot share an I²C bus with a
VL53L0X, VL53L1X or
VL6180X, since those boot at 0x29 too and are re-addressed separately; put
them on another bus.
# Two VL53L4CX sensors on the same I²C bussensor: - platform: vl53l4cx name: "Distance Front" address: 0x30 enable_pin: GPIOXX
- platform: vl53l4cx name: "Distance Rear" address: 0x31 enable_pin: GPIOXXSee Also
Section titled “See Also”- VL53L1X Time Of Flight Distance Sensor
- VL53L0X Time Of Flight Distance Sensor
- Sensor Filters
- VL53L4CX driver, the ESPHome fork of ST’s driver
- API Reference: vl53l4cx.h