### SPI Communication with VL53L8CH Sensor Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures the VL53L8CH sensor for SPI communication with custom speed and pins. This example demonstrates initializing the SPI bus and the sensor for ranging. It outputs the distance detected by the sensor. ```cpp #include #include #define SPI_CLK_PIN 3 #define SPI_MISO_PIN 5 #define SPI_MOSI_PIN 4 #define CS_PIN 10 #define PWREN_PIN 11 SPIClass DEV_SPI(SPI_MOSI_PIN, SPI_MISO_PIN, SPI_CLK_PIN); VL53L8CH sensor(&DEV_SPI, CS_PIN); void setup() { Serial.begin(460800); if (PWREN_PIN >= 0) { pinMode(PWREN_PIN, OUTPUT); digitalWrite(PWREN_PIN, HIGH); delay(10); } // Initialize SPI bus DEV_SPI.begin(); // Initialize sensor uint8_t status = sensor.begin(); if (status != VL53LMZ_STATUS_OK) { Serial.println("Sensor begin failed"); while(1); } status = sensor.init(); if (status != VL53LMZ_STATUS_OK) { Serial.println("Sensor init failed"); while(1); } status = sensor.start_ranging(); if (status == VL53LMZ_STATUS_OK) { Serial.println("SPI sensor ready"); } } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); // Process results Serial.print("Distance: "); Serial.print(results.distance_mm[0]); Serial.println(" mm"); } delay(100); } ``` -------------------------------- ### Configure Sharpener with VL53L8CH Arduino Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Adjusts the sharpener percentage of the VL53L8CH sensor to filter out blurred targets. The sharpener can be set to a value between 0 and 99, with higher values filtering more aggressively. This example shows how to set the sharpener, retrieve its current value, and observe its effect on detected targets. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); // Set sharpener to 30% (0-99, higher values filter more aggressively) uint8_t status = sensor.set_sharpener_percent(30); if (status == VL53LMZ_STATUS_OK) { Serial.println("Sharpener set to 30%"); } // Get current sharpener value uint8_t sharpener; sensor.get_sharpener_percent(&sharpener); Serial.print("Current sharpener: "); Serial.print(sharpener); Serial.println("%"); sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); // Sharpener affects nb_target_detected by filtering blurred targets for (int zone = 0; zone < 16; zone++) { if (results.nb_target_detected[zone] > 0) { Serial.print("Zone "); Serial.print(zone); Serial.print(": "); Serial.print(results.distance_mm[zone]); Serial.print(" mm, Sigma: "); Serial.print(results.range_sigma_mm[zone]); Serial.println(" mm"); } } } delay(500); } ``` -------------------------------- ### Change VL53L8CH Sensor I2C Address Source: https://context7.com/stm32duino/vl53l8ch/llms.txt This example shows how to change the I2C address of a VL53L8CH sensor. This is useful when using multiple sensors on the same I2C bus. The code initializes two sensors, changes the address of the first one, and then starts ranging from both. ```cpp #include VL53L8CH sensor1(&Wire, A3); VL53L8CH sensor2(&Wire, A4); void setup() { Serial.begin(460800); Wire.begin(); // Initialize first sensor with default address (0x52) sensor1.begin(); sensor1.init(); // Change first sensor's I2C address to 0x54 uint8_t status = sensor1.set_i2c_address(0x54); if (status == VL53LMZ_STATUS_OK) { Serial.println("Sensor 1 address changed to 0x54"); } // Now initialize second sensor at default address sensor2.begin(); sensor2.init(); Serial.println("Two sensors initialized at different addresses"); sensor1.start_ranging(); sensor2.start_ranging(); } void loop() { VL53LMZ_ResultsData results1, results2; uint8_t ready1 = 0, ready2 = 0; // Read from both sensors sensor1.check_data_ready(&ready1); sensor2.check_data_ready(&ready2); if (ready1) { sensor1.get_ranging_data(&results1); Serial.print("Sensor 1: "); Serial.print(results1.distance_mm[0]); Serial.println(" mm"); } if (ready2) { sensor2.get_ranging_data(&results2); Serial.print("Sensor 2: "); Serial.print(results2.distance_mm[0]); Serial.println(" mm"); } delay(100); } ``` -------------------------------- ### Configure Ranging Modes with VL53L8CH Arduino Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures the VL53L8CH sensor to operate in either continuous or autonomous ranging modes. Autonomous mode allows for precise control over integration time, while continuous mode uses the maximum integration time. This example demonstrates setting the mode and verifying it. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); // Set autonomous ranging mode for precise integration control uint8_t status = sensor.set_ranging_mode(VL53LMZ_RANGING_MODE_AUTONOMOUS); if (status == VL53LMZ_STATUS_OK) { Serial.println("Ranging mode set to AUTONOMOUS"); } // In autonomous mode, integration time can be precisely controlled sensor.set_integration_time_ms(10); // Alternative: Continuous mode (always maximum integration) // sensor.set_ranging_mode(VL53LMZ_RANGING_MODE_CONTINUOUS); // Verify mode uint8_t mode; sensor.get_ranging_mode(&mode); Serial.print("Current mode: "); Serial.println(mode == VL53LMZ_RANGING_MODE_AUTONOMOUS ? "AUTONOMOUS" : "CONTINUOUS"); sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); Serial.print("Distance: "); Serial.print(results.distance_mm[0]); Serial.println(" mm"); } } ``` -------------------------------- ### Configure VL53L8CH Resolution (C++) Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Demonstrates how to switch between 4x4 and 8x8 zone resolution modes for the VL53L8CH sensor. This involves calling `set_resolution` and processing the corresponding number of zones. It requires the `vl53l8ch.h` library. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); // Set to 8x8 resolution (64 zones) uint8_t status = sensor.set_resolution(VL53LMZ_RESOLUTION_8X8); if (status == VL53LMZ_STATUS_OK) { Serial.println("Resolution set to 8x8"); } sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); // Process all 64 zones in 8x8 mode for (int row = 0; row < 8; row++) { for (int col = 0; col < 8; col++) { int zone = row * 8 + col; if (results.nb_target_detected[zone] > 0) { Serial.print(results.distance_mm[zone]); Serial.print("\t"); } else { Serial.print("----\t"); } } Serial.println(); } Serial.println("\n"); } delay(500); } void toggle_resolution() { uint8_t current_res; sensor.stop_ranging(); sensor.get_resolution(¤t_res); if (current_res == VL53LMZ_RESOLUTION_4X4) { sensor.set_resolution(VL53LMZ_RESOLUTION_8X8); } else { sensor.set_resolution(VL53LMZ_RESOLUTION_4X4); } sensor.start_ranging(); } ``` -------------------------------- ### Basic I2C Ranging with VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Initializes the VL53L8CH sensor and captures multizone distance measurements using the I2C communication protocol. It requires the `vl53l8ch.h` and `Wire.h` libraries. The function outputs distances in millimeters for each detected zone. ```cpp #include #include #define DEV_I2C Wire #define LPN_PIN A3 #define PWREN_PIN 11 VL53L8CH sensor(&DEV_I2C, LPN_PIN); void setup() { Serial.begin(460800); // Enable power if PWREN pin is used if (PWREN_PIN >= 0) { pinMode(PWREN_PIN, OUTPUT); digitalWrite(PWREN_PIN, HIGH); delay(10); } DEV_I2C.begin(); // Initialize sensor uint8_t status = sensor.begin(); if (status != VL53LMZ_STATUS_OK) { Serial.println("Sensor begin failed"); while(1); } status = sensor.init(); if (status != VL53LMZ_STATUS_OK) { Serial.println("Sensor init failed"); while(1); } // Start ranging status = sensor.start_ranging(); if (status != VL53LMZ_STATUS_OK) { Serial.println("Start ranging failed"); while(1); } Serial.println("Sensor initialized and ranging started"); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; // Wait for data ready do { sensor.check_data_ready(&data_ready); } while (!data_ready); // Get ranging data uint8_t status = sensor.get_ranging_data(&results); if (status == VL53LMZ_STATUS_OK) { // Process results for 4x4 zones (16 zones default) for (int zone = 0; zone < 16; zone++) { if (results.nb_target_detected[zone] > 0) { int distance = results.distance_mm[zone]; int status = results.target_status[zone]; Serial.print("Zone "); Serial.print(zone); Serial.print(": "); Serial.print(distance); Serial.println(" mm"); } } } delay(100); } ``` -------------------------------- ### Configure VL53L8CH Ranging Frequency and Integration Time (C++) Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures the ranging frequency (Hz) and integration time (ms) for the VL53L8CH sensor to optimize performance. It includes verification steps to read back the configured values. Requires the `vl53l8ch.h` library. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); // Set ranging frequency to 15 Hz uint8_t status = sensor.set_ranging_frequency_hz(15); if (status == VL53LMZ_STATUS_OK) { Serial.println("Ranging frequency set to 15 Hz"); } // Set integration time to 20 ms status = sensor.set_integration_time_ms(20); if (status == VL53LMZ_STATUS_OK) { Serial.println("Integration time set to 20 ms"); } // Verify settings uint8_t freq; uint32_t integration_time; sensor.get_ranging_frequency_hz(&freq); sensor.get_integration_time_ms(&integration_time); Serial.print("Frequency: "); Serial.print(freq); Serial.println(" Hz"); Serial.print("Integration time: "); Serial.print(integration_time); Serial.println(" ms"); sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); Serial.print("Distance: "); Serial.print(results.distance_mm[0]); Serial.println(" mm"); } } ``` -------------------------------- ### Configure Motion Detection with VL53L8CH Sensor Source: https://context7.com/stm32duino/vl53l8ch/llms.txt This snippet demonstrates how to initialize and configure the VL53L8CH sensor for motion detection. It sets the resolution and a specific distance range for detecting moving objects. The code includes error handling for initialization and checks for motion in each zone. ```cpp #include #include VL53L8CH sensor(&Wire, A3); VL53LMZ_Motion_Configuration motion_config; uint8_t resolution = VL53LMZ_RESOLUTION_4X4; void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); sensor.set_resolution(resolution); // Initialize motion indicator with resolution uint8_t status = sensor.motion_indicator_init(&motion_config, resolution); if (status != VL53LMZ_STATUS_OK) { Serial.println("Motion indicator init failed"); while(1); } // Set distance range for motion detection (200mm to 1500mm) status = sensor.motion_indicator_set_distance_motion(&motion_config, 200, 1500); if (status == VL53LMZ_STATUS_OK) { Serial.println("Motion detection range: 200-1500 mm"); } sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); // Check motion indicator in results for (int zone = 0; zone < resolution; zone++) { if (results.motion_indicator.motion[zone] > 0) { Serial.print("Motion detected in zone "); Serial.print(zone); Serial.print(", Distance: "); Serial.print(results.distance_mm[zone]); Serial.println(" mm"); } } } delay(100); } ``` -------------------------------- ### Target Order Configuration for VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures the VL53L8CH sensor to report targets based on either proximity (closest first) or signal strength (strongest first). This affects the order in which multiple targets within a zone are listed in the results. Requires the vl53l8ch library and standard Arduino serial communication. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); // Set target order to closest uint8_t status = sensor.set_target_order(VL53LMZ_TARGET_ORDER_CLOSEST); if (status == VL53LMZ_STATUS_OK) { Serial.println("Target order set to CLOSEST"); } // Alternative: Set to strongest // sensor.set_target_order(VL53LMZ_TARGET_ORDER_STRONGEST); // Verify current setting uint8_t target_order; sensor.get_target_order(&target_order); Serial.print("Current target order: "); Serial.println(target_order == VL53LMZ_TARGET_ORDER_CLOSEST ? "CLOSEST" : "STRONGEST"); sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); // With multiple targets per zone, order affects which is reported first for (int zone = 0; zone < 16; zone++) { if (results.nb_target_detected[zone] > 0) { for (int target = 0; target < VL53LMZ_NB_TARGET_PER_ZONE; target++) { int idx = zone * VL53LMZ_NB_TARGET_PER_ZONE + target; Serial.print("Zone "); Serial.print(zone); Serial.print(" Target "); Serial.print(target); Serial.print(": "); Serial.print(results.distance_mm[idx]); Serial.println(" mm"); } } } } delay(500); } ``` -------------------------------- ### Manage Power Modes with VL53L8CH Arduino Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Demonstrates how to manage the power states of the VL53L8CH sensor for low-power applications. The code shows how to enter sleep mode, which retains firmware and configuration, and how to wake the sensor up. Deep sleep mode, which clears firmware and configuration, is also mentioned as an alternative for longer sleep periods. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); } void loop() { // Take measurement sensor.start_ranging(); VL53LMZ_ResultsData results; uint8_t data_ready = 0; do { sensor.check_data_ready(&data_ready); } while (!data_ready); sensor.get_ranging_data(&results); Serial.print("Distance: "); Serial.print(results.distance_mm[0]); Serial.println(" mm"); sensor.stop_ranging(); // Enter sleep mode (retains firmware and configuration) uint8_t status = sensor.set_power_mode(VL53LMZ_POWER_MODE_SLEEP); if (status == VL53LMZ_STATUS_OK) { Serial.println("Entering sleep mode"); } delay(5000); // Sleep for 5 seconds // Wake up sensor.set_power_mode(VL53LMZ_POWER_MODE_WAKEUP); Serial.println("Waking up"); // For longer sleep periods, use deep sleep (clears firmware and config) // sensor.set_power_mode(VL53LMZ_POWER_MODE_DEEP_SLEEP); // After deep sleep, sensor.init() must be called again } ``` -------------------------------- ### Perform and Apply Cross-talk Calibration for VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Calibrates the VL53L8CH sensor for cross-talk compensation to enhance measurement accuracy. It involves placing a specific target at a set distance and then applying the saved calibration data. Dependencies include the vl53l8ch library. Inputs are reflectance percentage, number of samples, and distance in mm. Outputs are calibration status and data. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); Serial.println("Starting cross-talk calibration"); Serial.println("Place a grey17% target at 600mm distance"); delay(5000); // Perform calibration (17% reflectance target at 600mm, 16 samples) uint8_t status = sensor.calibrate_xtalk(17, 16, 600); if (status == VL53LMZ_STATUS_OK) { Serial.println("Cross-talk calibration complete"); // Save calibration data uint8_t xtalk_data[VL53LMZ_XTALK_SIZE]; sensor.get_caldata_xtalk(xtalk_data); // Store xtalk_data to EEPROM or flash for future use Serial.println("Calibration data ready to save"); } else { Serial.println("Calibration failed"); } sensor.start_ranging(); } void apply_saved_calibration() { uint8_t xtalk_data[VL53LMZ_XTALK_SIZE]; // Load xtalk_data from EEPROM or flash sensor.set_caldata_xtalk(xtalk_data); // Optionally adjust xtalk margin (default recommended) uint32_t margin = 50; sensor.set_xtalk_margin(margin); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); Serial.print("Distance with xtalk compensation: "); Serial.print(results.distance_mm[0]); Serial.println(" mm"); } delay(100); } ``` -------------------------------- ### Configure and Capture CNH Data for VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures the VL53L8CH sensor to capture Compact Normalized Histogram (CNH) data for detailed analysis. This involves initializing CNH configuration, creating an aggregation map, and setting up the output to include CNH blocks. Dependencies include vl53l8ch and vl53lmz_plugin_cnh libraries. The output provides CNH data for each aggregate. ```cpp #include #include VL53L8CH sensor(&Wire, A3); VL53LMZ_Motion_Configuration cnh_config; cnh_data_buffer_t cnh_data_buffer; uint32_t cnh_data_size = 0; void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); uint8_t resolution = VL53LMZ_RESOLUTION_4X4; sensor.set_resolution(resolution); // Initialize CNH config: start_bin=0, num_bins=24, sub_sample=4 uint8_t status = sensor.cnh_init_config(&cnh_config, 0, 24, 4); if (status != VL53LMZ_STATUS_OK) { Serial.println("CNH init failed"); while(1); } // Create aggregation map (4x4 grid, 1x1 merge, 4 cols, 4 rows) status = sensor.cnh_create_agg_map(&cnh_config, 16, 0, 0, 1, 1, 4, 4); if (status != VL53LMZ_STATUS_OK) { Serial.println("CNH agg map failed"); while(1); } // Calculate required memory status = sensor.cnh_calc_required_memory(&cnh_config, &cnh_data_size); if (status != VL53LMZ_STATUS_OK || cnh_data_size > VL53LMZ_CNH_MAX_DATA_BYTES) { Serial.println("CNH memory error"); while(1); } Serial.print("CNH data size: "); Serial.print(cnh_data_size); Serial.println(" bytes"); // Send CNH configuration status = sensor.cnh_send_config(&cnh_config); if (status != VL53LMZ_STATUS_OK) { Serial.println("CNH send config failed"); while(1); } // Configure output to include CNH data sensor.create_output_config(); union Block_header cnh_data_bh; cnh_data_bh.idx = VL53LMZ_CNH_DATA_IDX; cnh_data_bh.type = 4; cnh_data_bh.size = cnh_data_size / 4; sensor.add_output_block(cnh_data_bh.bytes); sensor.send_output_config_and_start(); Serial.println("CNH configured, ranging started"); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; do { sensor.check_data_ready(&data_ready); } while (!data_ready); if (data_ready) { sensor.get_ranging_data(&results); // Extract CNH data block uint8_t status = sensor.results_extract_block(VL53LMZ_CNH_DATA_IDX, (uint8_t *)cnh_data_buffer, cnh_data_size); if (status == VL53LMZ_STATUS_OK) { int32_t *p_hist = NULL; int8_t *p_hist_scaler = NULL; int32_t *p_ambient = NULL; int8_t *p_ambient_scaler = NULL; // Process each aggregate for (int agg_id = 0; agg_id < cnh_config.nb_of_aggregates; agg_id++) { sensor.cnh_get_block_addresses(&cnh_config, agg_id, cnh_data_buffer, &p_hist, &p_hist_scaler, &p_ambient, &p_ambient_scaler); float ambient = ((float)*p_ambient) / (1 << *p_ambient_scaler); Serial.print("Aggregate "); Serial.print(agg_id); Serial.print(", Ambient: "); Serial.print(ambient, 1); Serial.print(", Bins: "); // Print histogram bins for (int bin = 0; bin < cnh_config.feature_length; bin++) { float bin_val = ((float)p_hist[bin]) / (1 << p_hist_scaler[bin]); Serial.print(bin_val, 1); Serial.print(" "); } Serial.println(); } } } delay(500); } ``` -------------------------------- ### Access Advanced Ranging Data with VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt This snippet shows how to retrieve detailed ranging data from the VL53L8CH sensor, including distance, signal strength, sigma values, target status, reflectance, and ambient light. It iterates through all zones and detected targets to print this information. ```cpp #include VL53L8CH sensor(&Wire, A3); void setup() { Serial.begin(460800); Wire.begin(); sensor.begin(); sensor.init(); sensor.start_ranging(); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready) { sensor.get_ranging_data(&results); Serial.println("=== Detailed Results ==="); for (int zone = 0; zone < 16; zone++) { if (results.nb_target_detected[zone] > 0) { Serial.print("Zone "); Serial.print(zone); Serial.println(":"); for (int target = 0; target < VL53LMZ_NB_TARGET_PER_ZONE; target++) { int idx = zone * VL53LMZ_NB_TARGET_PER_ZONE + target; Serial.print(" Target "); Serial.print(target); Serial.println(":"); // Distance in millimeters Serial.print(" Distance: "); Serial.print(results.distance_mm[idx]); Serial.println(" mm"); // Signal rate in kcps/spad Serial.print(" Signal: "); Serial.print(results.signal_per_spad[idx]); Serial.println(" kcps/spad"); // Range sigma (accuracy) in millimeters Serial.print(" Sigma: "); Serial.print(results.range_sigma_mm[idx]); Serial.println(" mm"); // Target status (0-255) Serial.print(" Status: "); Serial.println(results.target_status[idx]); // Reflectance percentage Serial.print(" Reflectance: "); Serial.print(results.reflectance[idx]); Serial.println("%"); } // Ambient per spad (zone-level) Serial.print(" Ambient: "); Serial.print(results.ambient_per_spad[zone]); Serial.println(" kcps/spad"); Serial.println(); } } } delay(1000); } ``` -------------------------------- ### Threshold Detection with Interrupts using VL53L8CH Source: https://context7.com/stm32duino/vl53l8ch/llms.txt Configures distance thresholds (200-600 mm) for the VL53L8CH sensor and utilizes an interrupt to signal when these thresholds are crossed. Requires the vl53l8ch library and specific pin configurations for I2C, interrupt, and power enable. Outputs detected distances within the specified range when an interrupt is triggered. ```cpp #include #define LPN_PIN A3 #define INT_PIN A2 #define PWREN_PIN 11 VL53L8CH sensor(&Wire, LPN_PIN); volatile int interrupt_count = 0; uint8_t resolution = VL53LMZ_RESOLUTION_4X4; void interrupt_handler() { interrupt_count = 1; } void setup() { Serial.begin(460800); if (PWREN_PIN >= 0) { pinMode(PWREN_PIN, OUTPUT); digitalWrite(PWREN_PIN, HIGH); delay(10); } Wire.begin(); // Configure interrupt pin pinMode(INT_PIN, INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(INT_PIN), interrupt_handler, FALLING); sensor.begin(); sensor.init(); // Disable thresholds initially sensor.set_detection_thresholds_enable(0); // Configure thresholds for each zone VL53LMZ_DetectionThresholds thresholds[VL53LMZ_NB_THRESHOLDS]; memset(&thresholds, 0, sizeof(thresholds)); for (uint8_t i = 0; i < resolution; i++) { thresholds[i].zone_num = i; thresholds[i].measurement = VL53LMZ_DISTANCE_MM; thresholds[i].type = VL53LMZ_IN_WINDOW; thresholds[i].mathematic_operation = VL53LMZ_OPERATION_NONE; thresholds[i].param_low_thresh = 200; // 200 mm thresholds[i].param_high_thresh = 600; // 600 mm } // Mark last threshold thresholds[resolution - 1].zone_num |= VL53LMZ_LAST_THRESHOLD; // Send thresholds to sensor uint8_t status = sensor.set_detection_thresholds(thresholds); if (status != VL53LMZ_STATUS_OK) { Serial.println("Failed to set thresholds"); while(1); } // Enable threshold detection sensor.set_detection_thresholds_enable(1); // Optional: Enable auto-stop when threshold is met sensor.set_detection_thresholds_auto_stop(1); sensor.start_ranging(); Serial.println("Threshold detection configured (200-600 mm)"); } void loop() { VL53LMZ_ResultsData results; uint8_t data_ready = 0; sensor.check_data_ready(&data_ready); if (data_ready && interrupt_count) { interrupt_count = 0; sensor.get_ranging_data(&results); Serial.println("Threshold triggered!"); for (int zone = 0; zone < resolution; zone++) { if (results.nb_target_detected[zone] > 0) { int dist = results.distance_mm[zone]; if (dist >= 200 && dist <= 600) { Serial.print("Zone "); Serial.print(zone); Serial.print(": "); Serial.print(dist); Serial.println(" mm [IN RANGE]"); } } } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.