Proximity Sensor: VL53L1X

Background

VL53L1X Overview

The VL53L1X is a Time-of-Flight (ToF) sensor developed by STMicroelectronics, capable of measuring absolute distances up to 4 meters with high accuracy. It operates using a 940nm laser emitter, making it resistant to ambient light interference. The sensor supports different distance measurement modes (Short, Medium, Long) and can be used in applications such as gesture recognition, obstacle detection, and proximity sensing.

Reference Documentation

Code Origin and Linux Porting

This library is a Linux port of the Pololu VL53L1X Arduino Library, which is based on STMicroelectronics’ official API. The porting process included:

  • Removing Arduino dependencies and replacing them with Linux-native I2C functions.

  • Adapting timing mechanisms (replacing millis() with Linux equivalents).

  • Ensuring compatibility with Linux device drivers and standard I2C interfaces.

Note

Only the functions listed in the class description were modified and tested during the porting process. No additional features or modifications beyond the required Linux adaptation were implemented.

License

This library follows the MIT License.

Class Description

Todo

Proximity

class VL53L1X

VL53L1X Time-of-Flight (ToF) Sensor Driver.

This class provides an interface for the VL53L1X ToF sensor, enabling distance measurements using I2C communication. It supports different distance modes, continuous and single-shot ranging, and error handling.

The class includes functions for sensor initialization, configuration, reading sensor data, and managing timeouts. It also provides utilities for setting the I2C address and retrieving measurement status.

Public Functions

VL53L1X(uint8_t i2c_address)

Constructs a VL53L1X sensor instance.

Parameters:

i2c_address – The I2C address of the sensor.

void setAddress(uint8_t new_addr)

Changes the I2C address of the sensor.

Parameters:

new_addr – The new I2C address to assign to the sensor.

bool init(bool io_2v8 = true)

Initializes the sensor with default settings.

This function verifies the sensor model ID, performs a soft reset, and applies default configuration settings.

Parameters:

io_2v8 – If true, sets the I/O voltage mode to 2.8V (default: 1.8V).

Returns:

True if initialization is successful, false otherwise.

void writeReg(uint16_t reg, uint8_t value)

Writes a single byte to the specified register over I2C.

Parameters:
  • reg – Register address.

  • value – Byte value to write.

void writeReg16Bit(uint16_t reg, uint16_t value)

Writes a 16-bit value to the specified register over I2C.

Parameters:
  • reg – Register address.

  • value – 16-bit value to write.

void writeReg32Bit(uint16_t reg, uint32_t value)

Writes a 32-bit value to the specified register over I2C.

Parameters:
  • reg – Register address.

  • value – 32-bit value to write.

uint8_t readReg(regAddr reg)

Reads a single byte from the specified register.

Parameters:

reg – Register address.

Returns:

The read byte value.

uint16_t readReg16Bit(uint16_t reg)

Reads a 16-bit value from the specified register.

Parameters:

reg – Register address.

Returns:

The read 16-bit value.

uint32_t readReg32Bit(uint16_t reg)

Reads a 32-bit value from the specified register.

Parameters:

reg – Register address.

Returns:

The read 32-bit value.

void startContinuous(uint32_t period_ms)

Starts continuous ranging measurements.

Parameters:

period_ms – Measurement interval in milliseconds.

uint16_t read_sensor(bool blocking = true)

Reads the measured distance from the sensor.

This function reads the measurement data only if an interrupt is active. It then converts the measured data into millimeters and clears the interrupt after reading all values.

Parameters:

blocking – If true, waits for a new measurement to be available.

Returns:

The measured distance in millimeters.

struct RangingData