Pololu VL53L1X Arduino Library

repository·master·Indexed 41 days ago

https://github.com/pololu/vl53l1x-arduino

A lightweight Arduino library for interfacing with the ST VL53L1X time-of-flight distance sensor via I2C. It provides a streamlined wrapper around the ST VL53L1X API (STSW-IMG007) for a smaller memory footprint, supporting continuous and single-shot ranging, distance mode configuration (Short, Medium, Long), and Region of Interest (ROI) adjustments.

Tokens
2.1K
Snippets
8
Records
14
Agent score
2%

What's inside VL53L1X

  1. Access detailed ranging data

    master

    The RangingData struct provides metadata about the most recent measurement. You can access this via the ranging_data member of the sensor object.

    Members:

    • uint16_t range_mm: The distance in millimeters (same as the return value of read()).
    • RangeStatus range_status: The status of the measurement. Use VL53L1X::RangeValid to check if the measurement was successful.
    • float peak_signal_count_rate_MCPS: Peak signal count rate in mega counts per second.
    • float ambient_count_count_rate_MCPS: Ambient count rate in mega counts per second.

    To convert a RangeStatus to a human-readable string, use VL53L1X::rangeStatusToString(status). Note that on AVR-based Arduinos, this consumes significant RAM (~200 bytes).

    sensor.read();
    if (sensor.ranging_data.range_status == VL53L1X::RangeValid) {
      Serial.print("Valid distance: ");
      Serial.println(sensor.ranging_data.range_mm);
    } else {
      Serial.print("Error: ");
      Serial.println(VL53L1X::rangeStatusToString(sensor.ranging_data.range_status));
    }
  2. Understand the relationship between this library and ST's VL53L1X API

    master

    This library is a streamlined wrapper around ST's official VL53L1X API (STSW-IMG007).

    Key Differences:

    • Pros: Provides a quicker, easier interface for Arduino with a smaller storage and memory footprint.
    • Cons: Does not implement advanced features like sensor calibration for cover glass or selecting a smaller Region of Interest (ROI), and has less robust error checking.

    If your application requires advanced functionality and you have sufficient storage/memory, consider using the official ST API directly or the Pololu implementation of ST's VL53L1X API for Arduino.

  3. Install the VL53L1X library manually

    master

    If the Library Manager is unavailable, follow these steps to install manually:

    1. Download the latest release archive from the GitHub releases page and decompress it.
    2. Rename the folder vl53l1x-arduino-master to VL53L1X.
    3. Move the VL53L1X folder into the libraries directory inside your Arduino sketchbook directory.
      • Note: You can find your sketchbook location via File > Preferences in the Arduino IDE.
    4. Restart the Arduino IDE.
  4. Perform continuous ranging measurements

    master

    To take measurements repeatedly at a set interval, use continuous mode.

    1. Call startContinuous(uint32_t period_ms) to begin. The period_ms defines the inter-measurement period. If this period is shorter than the timing budget, the sensor starts a new measurement immediately after the previous one finishes.
    2. Use read() or readRangeContinuousMillimeters() to get the distance in millimeters.
    3. By default, read() is blocking and waits for new data. To avoid blocking, check dataReady() first.
    4. Call stopContinuous() to end the mode.
    sensor.startContinuous(50); // 50ms period
    
    while (true) {
      if (sensor.dataReady()) {
        uint16_t distance = sensor.read();
        Serial.print("Distance: ");
        Serial.println(distance);
      }
    }
  5. Perform single-shot ranging measurements

    master

    To take a single measurement and then stop, use readSingle() (or its alias readRangeSingleMillimeters()).

    Blocking vs Non-blocking

    • Blocking (default): readSingle(true) waits until the measurement is complete and updates the ranging_data struct.
    • Non-blocking: readSingle(false) starts the measurement and returns 0 immediately. To use this pattern, you must manually wait for the data using dataReady() before calling read(false) to retrieve the result.
    // Non-blocking single shot pattern
    sensor.readSingle(false); 
    while (!sensor.dataReady()) {
      // Wait or do other tasks
    }
    uint16_t distance = sensor.read(false);
  6. Connect the VL53L1X to an Arduino

    master

    The VL53L1X sensor communicates via I2C. Depending on your Arduino board's voltage, follow the appropriate wiring scheme below:

    For 5V Arduino boards

    (e.g., Arduino Uno, Leonardo, Mega, Pololu A-Star 32U4)

    ArduinoVL53L1X board
    5VVIN
    GNDGND
    SDASDA
    SCLSCL
  7. Install the VL53L1X library via Arduino Library Manager

    master

    If you are using Arduino IDE version 1.6.2 or later, you can install the library through the built-in manager:

    1. Open the Sketch menu in the Arduino IDE.
    2. Select Include Library > Manage Libraries....
    3. Search for VL53L1X.
    4. Select the VL53L1X entry.
    5. Click Install.
  8. Configure the I2C bus for the VL53L1X

    master

    By default, the library uses the Wire object. If your sensor is connected to a different I2C bus (for example, Wire1 on some Arduino boards), use setBus() to configure it.

    To retrieve the currently configured bus, use getBus().

    // Example: Using the second I2C bus
    sensor.setBus(&Wire1);
  9. Set distance mode and measurement timing budget

    master

    The VL53L1X supports different distance modes and timing budgets to balance range, accuracy, and speed.

    Distance Modes

    Use setDistanceMode(DistanceMode mode) to select one of the following:

    • VL53L1X::Short: Lower maximum range, but less affected by ambient light.
    • VL53L1X::Medium
    • VL53L1X::Long

    Timing Budget

    Use setMeasurementTimingBudget(uint32_t budget_us) to set the time allowed for one measurement in microseconds.

    • A longer budget increases accuracy.
    • Minimums: 20,000 $\mu$s for Short mode; 33,000 $\mu$s for Medium and Long modes.
    sensor.setDistanceMode(VL53L1X::Long);
    sensor.setMeasurementTimingBudget(50000); // 50ms
  10. Initialize the VL53L1X sensor

    master

    Use the init() method to configure the sensor. You can optionally specify the I/O voltage mode via the io_2v8 parameter:

    • true (default): Configures the sensor for 2.8V I/O.
    • false: Configures the sensor for 1.8V I/O.

    The method returns a bool indicating whether initialization was successful.

    if (!sensor.init(true)) {
      // Initialization failed
    }