### Multi-Sensor Setup Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Illustrates how to configure and use multiple VL53L0X sensors simultaneously. This requires careful management of I2C addresses. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox1 = Adafruit_VL53L0X(); Adafruit_VL53L0X lox2 = Adafruit_VL53L0X(); // This is a simple example of how to use two VL53L0X sensors. // You will need to wire them up with different I2C addresses. // See the datasheet for more information on how to change the I2C address. // The default address is 0x29. You can change it to anything from 0x08 to 0x7F. void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X multi-sensor test!"); // init I2C bus Wire.begin(); // init the sensors if (lox1.begin(0x29) != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X sensor 1")); while(1); } Serial.println(F("VL53L0X Sensor 1 Initialized")); if (lox2.begin(0x2A) != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X sensor 2")); while(1); } Serial.println(F("VL53L0X Sensor 2 Initialized")); } void loop(void) { VL53L0X_RangingMeasurementData_t measure1; VL53L0X_RangingMeasurementData_t measure2; Serial.print("Sensor 1 Reading: "); vl53l0x_error_t status1 = lox1.performSingleMeasurement(&measure1); if (status1 != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status1); } else { if (measure1.RangeStatus != 4) { Serial.print(measure1.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } Serial.print("Sensor 2 Reading: "); vl53l0x_error_t status2 = lox2.performSingleMeasurement(&measure2); if (status2 != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status2); } else { if (measure2.RangeStatus != 4) { Serial.print(measure2.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } delay(100); } ``` -------------------------------- ### Continuous Measurement Cycle Setup Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Configures the sensor for continuous ranging mode with a specified timing budget and starts measurements. ```cpp void setup() { lox.begin(); lox.setMeasurementTimingBudgetMicroSeconds(33000); // 33ms per measurement lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); lox.startMeasurement(); } void loop() { if (lox.isRangeComplete()) { uint16_t distance = lox.readRange(); Serial.println(distance); } } ``` -------------------------------- ### Basic Single Measurement Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Demonstrates how to perform a single distance measurement using the VL53L0X sensor. This is a fundamental example for getting started with the sensor. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); } void loop(void) { VL53L0X_RangingMeasurementData_t measure; Serial.print("Reading a distance..."); vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status); } else { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } delay(100); } ``` -------------------------------- ### Advanced Multi-Configuration Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/usage-examples.md This example demonstrates how to dynamically switch between different measurement modes (fast, balanced, accurate, long range) to suit various application needs. It configures the sensor and timing budget for each mode. ```cpp #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); enum MeasurementMode { MODE_FAST, MODE_BALANCED, MODE_ACCURATE, MODE_LONG_RANGE }; void setMeasurementMode(MeasurementMode mode) { switch (mode) { case MODE_FAST: lox.configSensor(VL53L0X_SENSE_HIGH_SPEED); lox.setMeasurementTimingBudgetMicroSeconds(30000); Serial.println("Mode: Fast (30Hz)"); break; case MODE_BALANCED: lox.configSensor(VL53L0X_SENSE_DEFAULT); lox.setMeasurementTimingBudgetMicroSeconds(50000); Serial.println("Mode: Balanced (20Hz)"); break; case MODE_ACCURATE: lox.configSensor(VL53L0X_SENSE_HIGH_ACCURACY); lox.setMeasurementTimingBudgetMicroSeconds(100000); Serial.println("Mode: Accurate (10Hz)"); break; case MODE_LONG_RANGE: lox.configSensor(VL53L0X_SENSE_LONG_RANGE); lox.setMeasurementTimingBudgetMicroSeconds(200000); Serial.println("Mode: Long Range (5Hz)"); break; } } void setup() { Serial.begin(115200); if (!lox.begin()) while(1); setMeasurementMode(MODE_BALANCED); } void loop() { VL53L0X_RangingMeasurementData_t measure; lox.getSingleRangingMeasurement(&measure); if (measure.RangeStatus == 0) { Serial.println(measure.RangeMilliMeter); } delay(500); } ``` -------------------------------- ### Presence Detection Setup Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Configures the sensor for presence detection using default settings and a standard timing budget. ```cpp void setupPresenceDetection() { lox.begin(); lox.configSensor(VL53L0X_SENSE_DEFAULT); lox.setMeasurementTimingBudgetMicroSeconds(33000); } ``` -------------------------------- ### Simple Proximity Sensor Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/usage-examples.md This example detects if an object is within a specified distance (30cm). It uses continuous ranging and an LED to indicate detection. ```cpp #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); const uint16_t DETECTION_DISTANCE = 300; // 30cm const byte LED_PIN = 13; void setup() { Serial.begin(115200); pinMode(LED_PIN, OUTPUT); if (!lox.begin()) while(1); lox.startRangeContinuous(50); // Update every 50ms } void loop() { if (lox.isRangeComplete()) { uint16_t distance = lox.readRange(); uint8_t status = lox.readRangeStatus(); if (status == 0 && distance < DETECTION_DISTANCE) { // Object detected within 30cm digitalWrite(LED_PIN, HIGH); Serial.println("Object detected!"); } else { // No object or too far digitalWrite(LED_PIN, LOW); Serial.println("No object"); } } } ``` -------------------------------- ### Advanced Multi-Configuration Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Demonstrates how to switch between different sensor configurations dynamically. This allows for adapting the sensor's behavior based on application needs. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X advanced multi-configuration test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // Start with a default configuration (e.g., high accuracy) lox.setSignalRateLimit(0.25f); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_PRE_RANGE, 16*1000); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 21*1000); lox.setInterMeasurementInMs(50); } void loop(void) { VL53L0X_RangingMeasurementData_t measure; // Perform measurement with current configuration vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status == VL53L0X_ERROR_NONE && measure.RangeStatus != 4) { Serial.print("Distance: "); Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F("Sensor reading error or out of range.")); } // Example: Switch configuration after a delay delay(2000); Serial.println("Switching to long range configuration..."); lox.setSignalRateLimit(0.1f); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18*1000); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 33*1000); lox.setInterMeasurementInMs(100); // Perform measurement with new configuration status = lox.performSingleMeasurement(&measure); if (status == VL53L0X_ERROR_NONE && measure.RangeStatus != 4) { Serial.print("Long Range Distance: "); Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F("Sensor reading error or out of range.")); } delay(2000); Serial.println("Switching back to high accuracy configuration..."); // Revert to previous configuration lox.setSignalRateLimit(0.25f); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_PRE_RANGE, 16*1000); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 21*1000); lox.setInterMeasurementInMs(50); } ``` -------------------------------- ### Interrupt-Driven Measurement Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Configures the VL53L0X to trigger an interrupt when a measurement is ready. This is an efficient method for event-driven applications. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); // Define the interrupt pin #define VL53L0X_INTERRUPT_PIN 2 void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X interrupt driven measurement test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // configure the interrupt pin pinMode(VL53L0X_INTERRUPT_PIN, INPUT); // enable interrupt lox.enableInterrupt(VL53L0X_INTERRUPT_PIN, VL53L0X_HIGH_ACCURACY_MODE); // Use HIGH_ACCURACY_MODE for better range } void loop(void) { // wait for the interrupt if (digitalRead(VL53L0X_INTERRUPT_PIN) == HIGH) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.readSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status); } else { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } } delay(10); } ``` -------------------------------- ### Simple Proximity Sensor Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Configures the VL53L0X to act as a simple proximity sensor, triggering an action when an object enters a defined range. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); #define PROXIMITY_THRESHOLD_MM 100 // Trigger if object is closer than 100mm void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X proximity sensor test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); } void loop(void) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status == VL53L0X_ERROR_NONE && measure.RangeStatus != 4) { Serial.print("Distance: "); Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); if (measure.RangeMilliMeter < PROXIMITY_THRESHOLD_MM) { Serial.println("Proximity Alert! Object detected."); // Add your action here, e.g., turn on an LED, sound a buzzer } } else { Serial.println(F("Sensor reading error or out of range.")); } delay(200); } ``` -------------------------------- ### Basic VL53L0X Setup and Single Measurement Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/README.md Initializes the VL53L0X sensor and performs a single distance measurement. Ensure the sensor is connected and the library is included. ```cpp #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); // Initialize with default settings if (!lox.begin()) { Serial.println("Failed to boot VL53L0X"); while(1); } } void loop() { VL53L0X_RangingMeasurementData_t measure; // Get a measurement lox.getSingleRangingMeasurement(&measure); // Check if measurement is valid if (measure.RangeStatus == 0) { Serial.println(measure.RangeMilliMeter); } else { Serial.println("Out of range"); } delay(100); } ``` -------------------------------- ### Start Continuous Measurement and Read Range Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Starts continuous measurement mode and then repeatedly retrieves and prints the distance. Ensure `begin()` is called before `startMeasurement()`. ```cpp void setup() { lox.begin(); lox.startMeasurement(); } void loop() { VL53L0X_RangingMeasurementData_t measure; if (lox.getRangingMeasurement(&measure) == VL53L0X_ERROR_NONE) { Serial.println(measure.RangeMilliMeter); } } ``` -------------------------------- ### Mobile Robot Navigation Setup Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Sets up the sensor for mobile robot navigation, enabling continuous ranging with a fast measurement interval. ```cpp void setupRobotNavigation() { lox.begin(); lox.configSensor(VL53L0X_SENSE_HIGH_SPEED); lox.setMeasurementTimingBudgetMicroSeconds(30000); lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); lox.startRangeContinuous(50); // Measure every 50ms } ``` -------------------------------- ### High-Accuracy Measurement Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Configures the VL53L0X for high-accuracy measurements (down to ±3mm). This involves using specific timing budgets and modes. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X high accuracy test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // Set the configuration for high accuracy lox.setSignalRateLimit(0.25f); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_PRE_RANGE, 16*1000); // 16 us lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 21*1000); // 21 us lox.setInterMeasurementInMs(50); // 50ms } void loop(void) { VL53L0X_RangingMeasurementData_t measure; Serial.print("Reading a distance..."); vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status); } else { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } delay(100); } ``` -------------------------------- ### Multi-Sensor Setup for VL53L0X Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/usage-examples.md Initializes and reads from multiple VL53L0X sensors on the same I2C bus. Each sensor requires a dedicated shutdown pin (XSHUT) for sequential initialization. This setup allows for simultaneous distance readings from different sensors. ```cpp #include "Adafruit_VL53L0X.h" #define LOX1_ADDRESS 0x30 #define LOX2_ADDRESS 0x31 #define SHT_LOX1 7 // Shutdown pin for sensor 1 #define SHT_LOX2 6 // Shutdown pin for sensor 2 Adafruit_VL53L0X lox1 = Adafruit_VL53L0X(); Adafruit_VL53L0X lox2 = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); pinMode(SHT_LOX1, OUTPUT); pinMode(SHT_LOX2, OUTPUT); // Reset all sensors digitalWrite(SHT_LOX1, LOW); digitalWrite(SHT_LOX2, LOW); delay(10); // Activate both digitalWrite(SHT_LOX1, HIGH); digitalWrite(SHT_LOX2, HIGH); delay(10); // Initialize sensor 1 digitalWrite(SHT_LOX1, HIGH); digitalWrite(SHT_LOX2, LOW); if (!lox1.begin(LOX1_ADDRESS)) { Serial.println("Failed to boot LOX1"); while(1); } // Initialize sensor 2 digitalWrite(SHT_LOX2, HIGH); delay(10); if (!lox2.begin(LOX2_ADDRESS)) { Serial.println("Failed to boot LOX2"); while(1); } } void loop() { VL53L0X_RangingMeasurementData_t measure1, measure2; lox1.getSingleRangingMeasurement(&measure1); lox2.getSingleRangingMeasurement(&measure2); Serial.print("Sensor 1: "); if (measure1.RangeStatus == 0) { Serial.print(measure1.RangeMilliMeter); } else { Serial.print("ERROR"); } Serial.print(" | Sensor 2: "); if (measure2.RangeStatus == 0) { Serial.println(measure2.RangeMilliMeter); } else { Serial.println("ERROR"); } delay(100); } ``` -------------------------------- ### Error Handling Best Practices Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Demonstrates how to properly check for and handle errors returned by the VL53L0X sensor functions. This ensures robust operation of the system. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X error handling test!"); vl53l0x_error_t status = lox.begin(); if (status != VL53L0X_ERROR_NONE) { Serial.print(F("Failed to boot VL53L0X, error code: ")); Serial.println(status); // Handle the error appropriately, e.g., retry, log, or halt while(1); } Serial.println(F("VL53L0X Sensor Initialized")); } void loop(void) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error during measurement: "); Serial.println(status); // Handle measurement error, e.g., ignore reading, reset sensor } else { if (measure.RangeStatus == 4) { // 4 = invalid measurement result Serial.println(F("Measurement out of range.")); // Handle out of range condition } else { Serial.print("Distance: "); Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); // Process valid distance reading } } delay(500); } ``` -------------------------------- ### startRange() Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Begins a single range measurement. Must be followed by calls to `waitRangeComplete()` or polling `isRangeComplete()`, then `readRange()` to get the result. ```APIDOC ## startRange() ### Description Begins a single range measurement. Must be followed by calls to `waitRangeComplete()` or polling `isRangeComplete()`, then `readRange()` to get the result. ### Method N/A (This is a method call, not an HTTP endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response - `boolean`: `true` if measurement started successfully, `false` otherwise #### Response Example N/A ``` -------------------------------- ### startMeasurement() Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Starts continuous measurement mode. Initiates continuous measurement mode. After calling this, use getRangingMeasurement() to retrieve measurements. ```APIDOC ## startMeasurement() ### Description Starts continuous measurement mode. Initiates continuous measurement mode. After calling this, use `getRangingMeasurement()` to retrieve measurements. ### Signature ```cpp VL53L0X_Error startMeasurement(boolean debug = false) ``` ### Parameters #### Arguments - `debug` (boolean) - Enable debug output ### Returns `VL53L0X_Error` ### Example ```cpp void setup() { lox.begin(); lox.startMeasurement(); } void loop() { VL53L0X_RangingMeasurementData_t measure; if (lox.getRangingMeasurement(&measure) == VL53L0X_ERROR_NONE) { Serial.println(measure.RangeMilliMeter); } } ``` ``` -------------------------------- ### Single Measurement Cycle Setup and Loop Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Initializes the sensor in single ranging mode and continuously performs measurements in the loop, printing the distance if valid. ```cpp Adafruit_VL53L0X lox; VL53L0X_RangingMeasurementData_t measure; void setup() { lox.begin(); // Now in DEVICEMODE_SINGLE_RANGING } void loop() { // Measure lox.getSingleRangingMeasurement(&measure); // Check validity if (measure.RangeStatus == 0) { Serial.println(measure.RangeMilliMeter); } delay(100); } ``` -------------------------------- ### Continuous Measurement Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Shows how to continuously poll the VL53L0X sensor for distance measurements. This is suitable for applications requiring frequent updates. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X continuous measurement test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // start the continuous measurement lox.startContinuous(100); // 100ms interval } void loop(void) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.readSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status); } else { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } delay(100); } ``` -------------------------------- ### Long-Range Configuration Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Configures the VL53L0X sensor for long-range measurements (up to 2 meters). This involves adjusting timing budgets and other parameters. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X long range test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // Set the configuration for long range lox.setSignalRateLimit(0.1f); lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_PRE_RANGE, 18*1000); // 18 us lox.setVcselPulsePeriod(VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 33*1000); // 33 us } void loop(void) { VL53L0X_RangingMeasurementData_t measure; Serial.print("Reading a distance..."); vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status != VL53L0X_ERROR_NONE) { Serial.print("Error: "); Serial.println(status); } else { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } } delay(100); } ``` -------------------------------- ### startRangeContinuous(uint16_t period_ms = 50) Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Starts continuous ranging mode where measurements are taken automatically at the specified interval. Use `readRange()` to get the current distance value, and check `isRangeComplete()` before reading. ```APIDOC ## startRangeContinuous(uint16_t period_ms = 50) ### Description Initiates continuous ranging mode where measurements are taken automatically at the specified interval. Use `readRange()` to get the current distance value, and check `isRangeComplete()` before reading. ### Method N/A (This is a method call, not an HTTP endpoint) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Parameters - `period_ms` (uint16_t) - Optional - Time between measurements in milliseconds (default: 50) ### Request Example N/A ### Response #### Success Response - `boolean`: `true` if continuous mode started, `false` on error #### Response Example N/A ``` -------------------------------- ### Industrial Positioning Setup Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Configures the sensor for industrial positioning applications requiring high accuracy and a longer timing budget. ```cpp void setupIndustrialPositioning() { lox.begin(); lox.configSensor(VL53L0X_SENSE_HIGH_ACCURACY); lox.setMeasurementTimingBudgetMicroSeconds(100000); } ``` -------------------------------- ### Handle Unsupported GPIO Functionality Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/errors.md This example demonstrates how to handle the VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED error, which occurs when the requested GPIO function is not compatible with the device's current mode or hardware capabilities. ```cpp VL53L0X_Error status = lox.setGpioConfig( VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, VL53L0X_GPIOFUNCTIONALITY_NEW_MEASURE_READY, VL53L0X_INTERRUPTPOLARITY_LOW); if (status == VL53L0X_ERROR_GPIO_FUNCTIONALITY_NOT_SUPPORTED) { Serial.println("GPIO functionality not supported"); } ``` -------------------------------- ### Signal Quality Monitoring Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Monitors the signal quality of the distance measurement. This can help diagnose issues with reflectivity or ambient light. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X signal quality monitoring test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); } void loop(void) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status == VL53L0X_ERROR_NONE) { Serial.print("Distance: "); if (measure.RangeStatus != 4) { // 4 = invalid measurement result Serial.print(measure.RangeMilliMeter); Serial.println(" mm"); } else { Serial.println(F(" out of range ")); } Serial.print("Signal Rate: "); Serial.print(measure.SignalRateRtnMegaCenti); Serial.println(" Mcps"); Serial.print("Signal Quality: "); Serial.println(measure.EffectiveSpadRtnCount); } else { Serial.print("Error: "); Serial.println(status); } delay(500); } ``` -------------------------------- ### Signal Quality Monitoring Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/usage-examples.md This snippet monitors and prints the signal quality metrics of each measurement. It's useful for diagnosing measurement issues or understanding environmental impacts. ```cpp #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void printMeasurementQuality(VL53L0X_RangingMeasurementData_t* measure) { Serial.print("Distance: "); Serial.print(measure->RangeMilliMeter); Serial.print(" mm | Status: "); Serial.print(measure->RangeStatus); Serial.print(" | Signal: "); float signal = (float)measure->SignalRateRtnMegaCps / 65536.0; Serial.print(signal); Serial.print(" MCPS"); Serial.print(" | Ambient: "); float ambient = (float)measure->AmbientRateRtnMegaCps / 65536.0; Serial.print(ambient); Serial.println(" MCPS"); } void setup() { Serial.begin(115200); if (!lox.begin()) while(1); } void loop() { VL53L0X_RangingMeasurementData_t measure; lox.getSingleRangingMeasurement(&measure); printMeasurementQuality(&measure); delay(200); } ``` -------------------------------- ### Running Average Filter Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md Implements a running average filter to smooth out distance readings over time. This provides a more stable output compared to simple averaging. ```cpp #include #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); const int FILTER_SIZE = 5; long distanceReadings[FILTER_SIZE]; int readingIndex = 0; long total = 0; void setup(void) { Serial.begin(115200); // wait until serial port is opened and it is established while (!Serial) { delay(1); } Serial.println("Adafruit VL53L0X running average filter test!"); if (lox.begin() != VL53L0X_ERROR_NONE) { Serial.println(F("Failed to boot VL53L0X")); while(1); } Serial.println(F("VL53L0X Sensor Initialized")); // Initialize the filter array with zeros for (int i = 0; i < FILTER_SIZE; i++) { distanceReadings[i] = 0; } } void loop(void) { VL53L0X_RangingMeasurementData_t measure; vl53l0x_error_t status = lox.performSingleMeasurement(&measure); if (status == VL53L0X_ERROR_NONE && measure.RangeStatus != 4) { long currentDistance = measure.RangeMilliMeter; // Subtract the last reading total = total - distanceReadings[readingIndex]; // Read from the sensor distanceReadings[readingIndex] = currentDistance; // Add the new reading total = total + distanceReadings[readingIndex]; // Advance to the next position in the array readingIndex = readingIndex + 1; // If we're at the end of the array, wrap around back to the beginning if (readingIndex >= FILTER_SIZE) { readingIndex = 0; } // Calculate the average long averageDistance = total / FILTER_SIZE; Serial.print("Filtered Distance: "); Serial.print(averageDistance); Serial.println(" mm"); } else { Serial.println(F("Sensor reading error or out of range.")); } delay(100); } ``` -------------------------------- ### High-Accuracy Measurement Example Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/usage-examples.md Use this snippet for precise distance measurements. It configures the sensor for high accuracy and enables strict measurement quality checks. ```cpp #include "Adafruit_VL53L0X.h" Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); // High-accuracy preset if (!lox.begin(0x29, false, &Wire, VL53L0X_SENSE_HIGH_ACCURACY)) { while(1); } // Ensure strict measurement quality lox.setLimitCheckEnable(VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, 1); lox.setLimitCheckEnable(VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, 1); } void loop() { VL53L0X_RangingMeasurementData_t measure; lox.getSingleRangingMeasurement(&measure); if (measure.RangeStatus == 0) { Serial.print("Distance: "); Serial.print(measure.RangeMilliMeter); Serial.print(" mm ± 3mm"); // Check measurement confidence FixPoint1616_t signal; lox.getLimitCheckCurrent(VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, &signal); float signalMCPS = (float)signal / 65536.0; Serial.print(" | Signal: "); Serial.print(signalMCPS); Serial.println(" MCPS"); } delay(500); // Allow time for long measurement } ``` -------------------------------- ### Distance-Triggered Alert Setup Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Configures the sensor to trigger an alert when an object enters a specified zone (30cm). It sets up interrupt thresholds and GPIO configuration for the alert. ```cpp void setupDistanceAlert() { lox.begin(); lox.configSensor(VL53L0X_SENSE_DEFAULT); // Alert when object enters 30cm zone FixPoint1616_t threshold = (FixPoint1616_t)(300.0 * 65536); lox.setInterruptThresholds(0, threshold); lox.setGpioConfig(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_LOW, VL53L0X_INTERRUPTPOLARITY_LOW); lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); lox.startMeasurement(); } ``` -------------------------------- ### Get Measurement Timing Budget Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Retrieves the currently configured measurement timing budget in microseconds. This value indicates the time allocated for each measurement. ```cpp uint32_t budget = lox.getMeasurementTimingBudgetMicroSeconds(); Serial.print("Timing budget: "); Serial.print(budget); Serial.println(" µs"); ``` -------------------------------- ### Start and Wait for Range Measurement Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Initiates a single range measurement and blocks until it completes. Use this when you need to ensure a measurement is finished before proceeding. Requires the sensor to be initialized. ```cpp void loop() { if (!lox.startRange()) { Serial.println("Failed to start range"); return; } if (lox.waitRangeComplete()) { uint16_t distance = lox.readRange(); Serial.println(distance); } } ``` -------------------------------- ### Adafruit_VL53L0X Class Methods Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/MANIFEST.md This section details the public methods available in the Adafruit_VL53L0X class. Each method includes its signature, parameter table, return type, error conditions, and a usage example. ```APIDOC ## Adafruit_VL53L0X Class Methods ### Description This section details the public methods available in the Adafruit_VL53L0X class. Each method includes its signature, parameter table, return type, error conditions, and a usage example. ### Methods Documented: - `begin()` - Initialization - `setAddress()` - I2C address configuration - `rangingTest()` / `getSingleRangingMeasurement()` - Single measurement - `getRangingMeasurement()` - Continuous mode measurement - `startMeasurement()`, `stopMeasurement()` - Measurement control - `readRange()`, `readRangeStatus()` - Result reading - `startRange()`, `isRangeComplete()`, `waitRangeComplete()` - Single shot flow - `startRangeContinuous()`, `stopRangeContinuous()` - Continuous mode - `printRangeStatus()`, `timeoutOccurred()` - Utilities - `configSensor()` - Configuration presets - `setMeasurementTimingBudgetMicroSeconds()`, `getMeasurementTimingBudgetMicroSeconds()` - Timing - `setVcselPulsePeriod()`, `getVcselPulsePeriod()` - VCSEL tuning - `setLimitCheckEnable()`, `getLimitCheckEnable()` - Limit check control - `setLimitCheckValue()`, `getLimitCheckValue()` - Limit thresholds - `getLimitCheckCurrent()` - Current limit check values - `getDeviceMode()`, `setDeviceMode()` - Operating mode - `setInterruptThresholds()`, `getInterruptThresholds()` - GPIO thresholds - `clearInterruptMask()` - Interrupt management - `getGpioConfig()`, `setGpioConfig()` - GPIO configuration ### Format: - Full signature in code block - Parameter table (name, type, default, description) - Return type documentation - Error/exception conditions - Complete usage example for each method - Source file path and line number ``` -------------------------------- ### Configure GPIO for Motion Detection Interrupt Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Sets up the GPIO pin to trigger an interrupt when the measured distance falls outside the specified low and high thresholds. This example configures for continuous ranging and interrupts when the distance is either too close or too far. ```cpp void setupMotionDetection() { // Interrupt when distance is less than 200mm or greater than 800mm FixPoint1616_t lowThresh = (FixPoint1616_t)(200.0 * 65536); FixPoint1616_t highThresh = (FixPoint1616_t)(800.0 * 65536); lox.setInterruptThresholds(lowThresh, highThresh); // Configure GPIO to interrupt when outside thresholds lox.setGpioConfig(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING, VL53L0X_GPIOFUNCTIONALITY_THRESHOLD_CROSSED_OUT, VL53L0X_INTERRUPTPOLARITY_LOW); lox.setDeviceMode(VL53L0X_DEVICEMODE_CONTINUOUS_RANGING); lox.startMeasurement(); } void VL53LOXISR() { // Handle interrupt VL53L0X_RangingMeasurementData_t measure; lox.getRangingMeasurement(&measure); // Clear interrupt to allow next trigger lox.clearInterruptMask(); } ``` -------------------------------- ### begin() Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Initializes the I2C interface and sensor hardware with optional debug output and custom configurations. Returns true on success, false on failure. ```APIDOC ## begin() ### Description Initializes the I2C interface and sensor hardware. ### Method ```cpp boolean begin(uint8_t i2c_addr = VL53L0X_I2C_ADDR, boolean debug = false, TwoWire* i2c = &Wire, VL53L0X_Sense_config_t vl_config = VL53L0X_SENSE_DEFAULT) ``` ### Parameters #### Arguments - `i2c_addr` (uint8_t) - Optional - I2C address of the sensor (7-bit format), defaults to `0x29`. - `debug` (boolean) - Optional - Enable serial debug output during initialization, defaults to `false`. - `i2c` (TwoWire*) - Optional - I2C bus instance for communication, defaults to `&Wire`. - `vl_config` (VL53L0X_Sense_config_t) - Optional - Sensor configuration preset, defaults to `VL53L0X_SENSE_DEFAULT`. ### Returns - `boolean` - `true` if sensor initialized successfully, `false` if initialization failed. ### Throws - Sets `Status` to an error code (see `VL53L0X_Error` types). ### Description Performs complete sensor initialization including I2C communication setup, device data initialization, I2C address assignment, device information retrieval, static initialization (calibration data loading), reference SPAD management, reference calibration, device mode setup (single ranging mode), and sensor configuration application. ### Example ```cpp Adafruit_VL53L0X lox = Adafruit_VL53L0X(); void setup() { Serial.begin(115200); if (!lox.begin()) { Serial.println("Failed to boot VL53L0X"); while(1); } Serial.println("VL53L0X initialized successfully"); } ``` ``` -------------------------------- ### getRangingMeasurement() Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/api-reference.md Gets a measurement from the device after starting measurement mode. Retrieves a ranging measurement from the sensor and is used in continuous or timed measurement modes. Must be called after startMeasurement() has been invoked. ```APIDOC ## getRangingMeasurement() ### Description Gets a measurement from the device after starting measurement mode. Retrieves a ranging measurement from the sensor. Used in continuous or timed measurement modes. Must be called after `startMeasurement()` has been invoked. ### Signature ```cpp VL53L0X_Error getRangingMeasurement(VL53L0X_RangingMeasurementData_t* pRangingMeasurementData, boolean debug = false) ``` ### Parameters #### Arguments - `pRangingMeasurementData` (VL53L0X_RangingMeasurementData_t*) - Pointer to measurement data struct - `debug` (boolean) - Enable debug output ### Returns `VL53L0X_Error` ``` -------------------------------- ### Initialize VL53L0X with High Speed Configuration Source: https://github.com/adafruit/adafruit_vl53l0x/blob/master/_autodocs/configuration.md Initializes the VL53L0X sensor with settings optimized for rapid measurements. This configuration reduces the timing budget for faster update rates, suitable for motion tracking or real-time monitoring. ```cpp lox.begin(0x29, false, &Wire, VL53L0X_SENSE_HIGH_SPEED); ```