Adafruit VL53L0X Library
repository·master·Indexed 41 days ago
https://github.com/adafruit/adafruit_vl53l0xA driver for the VL53L0X Time-of-Flight (ToF) sensor breakout (Product ID: 3317) enabling distance measurement via I2C. The library supports single and continuous ranging measurements, configurable sensing modes (Default, Long Range, High Speed, High Accuracy), measurement timing budget adjustments, and hardware interrupt threshold configuration.
What's inside Adafruit VL53L0X
- The Adafruit VL53L0X library provides an interface for the VL53L0X Time-of-Flight (ToF) breakout board. This sensor uses I2C for communication to measure distance. It is specifically designed for use with the Adafruit VL53L0X breakout (Product ID: 3317).
Configure VL53L0X sensing modes
masterThe library provides a
VL53L0X_Sense_config_tenumeration to choose between different operational modes for the sensor. These can be passed duringbegin()or set usingconfigSensor().Available Modes:
VL53L0X_SENSE_DEFAULT: Standard operating mode.VL53L0X_SENSE_LONG_RANGE: Optimized for longer distance measurements.VL53L0X_SENSE_HIGH_SPEED: Optimized for faster measurement rates.VL53L0X_SENSE_HIGH_ACCURACY: Optimized for higher precision.
typedef enum { VL53L0X_SENSE_DEFAULT = 0, VL53L0X_SENSE_LONG_RANGE, VL53L0X_SENSE_HIGH_SPEED, VL53L0X_SENSE_HIGH_ACCURACY } VL53L0X_Sense_config_t;Configure sensor interrupt thresholds and GPIO
masterThe library allows configuring hardware interrupts based on distance thresholds and setting up GPIO functionality.
Interrupt Thresholds:
setInterruptThresholds(FixPoint1616_t ThresholdLow, FixPoint1616_t ThresholdHigh, boolean debug = false): Sets the low and high distance limits for triggering an interrupt.clearInterruptMask(boolean debug = false): Clears the current interrupt.
GPIO Configuration:
setGpioConfig(VL53L0X_DeviceModes DeviceMode, VL53L0X_GpioFunctionality Functionality, VL53L0X_InterruptPolarity Polarity, boolean debug = false): Configures the GPIO pin mode, functionality, and interrupt polarity.
Perform a single ranging measurement
masterTo get a single distance measurement, use
getSingleRangingMeasurement()or its wrapperrangingTest(). You must provide a pointer to aVL53L0X_RangingMeasurementData_tstruct where the results will be stored.Methods:
VL53L0X_Error rangingTest(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData, boolean debug = false)VL53L0X_Error getSingleRangingMeasurement(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData, boolean debug = false)
Returns
VL53L0X_ERROR_NONEon success, or an error code from theVL53L0X_Errortype.VL53L0X_RangingMeasurementData_t data; VL53L0X_Error err = sensor.rangingTest(&data); if (err == VL53L0X_ERROR_NONE) { // Use data.range_mm to get distance in mm }Perform continuous ranging measurements
masterFor applications requiring repeated measurements at a set interval, use the continuous ranging methods.
Methods:
boolean startRangeContinuous(uint16_t period_ms = 50): Starts continuous measurements at the specified period in milliseconds.void stopRangeContinuous(): Stops the continuous measurement cycle.
To retrieve the result of the most recent measurement in a continuous cycle, use
readRangeResult().// Start continuous ranging every 100ms sensor.startRangeContinuous(100); // In your loop: if (sensor.isRangeComplete()) { uint16_t distance = sensor.readRangeResult(); Serial.print("Distance: "); Serial.println(distance); }Initialize the Adafruit_VL53L0X sensor
masterUse the
begin()method to initialize the sensor and prepare it for communication. You can specify the I2C address, enable debug mode, provide a customTwoWireinstance, and select a sensing configuration.Parameters:
i2c_addr: The I2C address of the sensor (defaults toVL53L0X_I2C_ADDRwhich is0x29).debug: Iftrue, debug information will be printed viaSerial.print.i2c: A pointer to theTwoWireobject to use (defaults to&Wire).vl_config: AVL53L0X_Sense_config_tenum value to set the sensing mode.
Adafruit_VL53L0X sensor; // Initialize with default settings if (!sensor.begin()) { Serial.println("Failed to begin VL53L0X"); while (1); }Adjust measurement timing budget
masterYou can fine-tune the sensor's performance by adjusting the measurement timing budget, which affects the balance between accuracy and speed.
Methods:
boolean setMeasurementTimingBudgetMicroSeconds(uint32_t budget_us): Sets the time allocated for a single measurement in microseconds.uint32_t getMeasurementTimingBudgetMicroSeconds(): Returns the current timing budget.