Pololu VL53L0X Arduino Library

repository·master·Indexed 18 days ago

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

A lightweight Arduino library for interfacing with the ST VL53L0X Time-of-Flight distance sensor via I2C. It provides a streamlined API for memory-constrained environments, supporting single-shot and continuous ranging, measurement timing and signal rate configuration, and low-level register access.

Tokens
1.1K
Snippets
4
Records
8
Agent score
14%

What's inside vl53l0x-arduino

  1. Connect the VL53L0X sensor to an Arduino

    master

    Depending on your Arduino's operating voltage, connect the VL53L0X carrier as follows:

    5V Arduino boards

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

    • 5V $\rightarrow$ VIN
    • GND $\rightarrow$ GND
    • SDA $\rightarrow$ SDA
    • SCL $\rightarrow$ SCL

    3.3V Arduino boards

    (e.g., Due)

    • 3V3 $\rightarrow$ VIN
    • GND $\rightarrow$ GND
    • SDA $\rightarrow$ SDA
    • SCL $\rightarrow$ SCL
  2. Install the VL53L0X library

    master
    1. Open the Arduino IDE.
    2. Go to Sketch $\rightarrow$ Include Library $\rightarrow$ Manage Libraries....
    3. Search for "VL53L0X".
    4. Select the VL53L0X entry and click Install.

    Manual Installation

    1. Download the latest release archive from GitHub and decompress it.
    2. Rename the folder vl53l0x-arduino-master to VL53L0X.
    3. Move the VL53L0X folder into your Arduino libraries directory (found via File $\rightarrow$ Preferences in the IDE).
    4. Restart the Arduino IDE.
  3. Initialize the VL53L0X sensor

    master

    Use the init() method to configure the sensor. By default, it configures the sensor for 2.8V I/O mode. You can specify 1.8V mode by passing false to the io_2v8 parameter.

    Returns true if initialization was successful.

    VL53L0X sensor;
    if (!sensor.init()) {
      // Handle error
    }
  4. Low-level register access

    master

    For advanced users, the library provides direct access to the sensor's registers:

    • writeReg(uint8_t reg, uint8_t value)
    • writeReg16Bit(uint8_t reg, uint16_t value)
    • writeReg32Bit(uint8_t reg, uint32_t value)
    • readReg(uint8_t reg)
    • readReg16Bit(uint16_t reg)
    • readReg32Bit(uint32_t reg)
    • writeMulti(uint8_t reg, uint8_t const * src, uint8_t count)
    • readMulti(uint8_t reg, uint8_t * dst, uint8_t count)
    // Example: Writing to a register
    sensor.writeReg(VL53L0X::SYSRANGE_START, 0x01);
  5. Perform single-shot and continuous ranging

    master

    The library supports two modes of operation:

    1. Single-shot: Performs one measurement and returns the distance.
    2. Continuous: Starts a sequence of measurements. You can use back-to-back mode (default) or timed mode with a specific interval.

    To get the distance in millimeters, use readRangeSingleMillimeters() for single shots or readRangeContinuousMillimeters() when in continuous mode.

    // Single-shot measurement
    uint16_t distance = sensor.readRangeSingleMillimeters();
    
    // Continuous mode (back-to-back)
    sensor.startContinuous();
    // ... later in loop ...
    uint16_t distance = sensor.readRangeContinuousMillimeters();
    
    // Stop continuous mode
    sensor.stopContinuous();
  6. Configure measurement timing and signal rate

    master

    You can tune the sensor performance using the following methods:

    • setMeasurementTimingBudget(uint32_t budget_us): Sets the time allowed for one measurement in microseconds. A longer budget increases accuracy. Minimum is 20,000 $\mu$s (20 ms).
    • setSignalRateLimit(float limit_Mcps): Sets the minimum return signal rate in MCPS (mega counts per second). Lowering this increases range but increases noise from reflections. Default is 0.25 MCPS.
    • setVcselPulsePeriod(vcselPeriodType type, uint8_t period_pclks): Sets the VCSEL pulse period for PreRange or FinalRange. Longer periods increase potential range.
  7. Handle I2C and sensor timeouts

    master

    The library provides mechanisms to monitor communication and sensor readiness:

    • setTimeout(uint16_t timeout): Sets a timeout in milliseconds for read operations. A value of 0 disables it.
    • timeoutOccurred(): Returns true if a read timeout has occurred since the last call to this method.
    • last_status: A member variable containing the status of the last I2C write transmission (compatible with Wire.endTransmission() return values).