Adafruit VL53L0X Library

repository·master·Indexed 41 days ago

https://github.com/adafruit/adafruit_vl53l0x

A 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.

Tokens
1.2K
Snippets
4
Records
7
Agent score
2%

What's inside Adafruit VL53L0X

  1. Overview of the Adafruit VL53L0X Library

    master
    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).
  2. Configure VL53L0X sensing modes

    master

    The library provides a VL53L0X_Sense_config_t enumeration to choose between different operational modes for the sensor. These can be passed during begin() or set using configSensor().

    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;
  3. Configure sensor interrupt thresholds and GPIO

    master

    The 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.
  4. Perform a single ranging measurement

    master

    To get a single distance measurement, use getSingleRangingMeasurement() or its wrapper rangingTest(). You must provide a pointer to a VL53L0X_RangingMeasurementData_t struct 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_NONE on success, or an error code from the VL53L0X_Error type.

    VL53L0X_RangingMeasurementData_t data;
    VL53L0X_Error err = sensor.rangingTest(&data);
    
    if (err == VL53L0X_ERROR_NONE) {
      // Use data.range_mm to get distance in mm
    }
  5. Perform continuous ranging measurements

    master

    For 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);
    }
  6. Initialize the Adafruit_VL53L0X sensor

    master

    Use the begin() method to initialize the sensor and prepare it for communication. You can specify the I2C address, enable debug mode, provide a custom TwoWire instance, and select a sensing configuration.

    Parameters:

    • i2c_addr: The I2C address of the sensor (defaults to VL53L0X_I2C_ADDR which is 0x29).
    • debug: If true, debug information will be printed via Serial.print.
    • i2c: A pointer to the TwoWire object to use (defaults to &Wire).
    • vl_config: A VL53L0X_Sense_config_t enum value to set the sensing mode.
    Adafruit_VL53L0X sensor;
    
    // Initialize with default settings
    if (!sensor.begin()) {
      Serial.println("Failed to begin VL53L0X");
      while (1);
    }
  7. Adjust measurement timing budget

    master

    You 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.