EDIT: I'm currently using an Arduino Nano 33 IoT
Hi, I'm currently working on a project where I need to measure distances at two different points using distance sensors (I'm currently using VL53L4CD laser distance sensors). However, this project requires a high sampling rate of 100Hz. To achieve this, I have 4 distance sensors arranged into 2 sensor sets. Each sensor set consists of 2 distance sensors (So for example, sensor set 1 consists of distance sensor 2&3 and sensor set 2 consists of distance sensor 1&4) and I would alternate between reading each sensor set.
This seemed to work at first, I did get a new distance reading every 8ms. However, the serial port stops receiving the distance readings after a very random amount of time. Sometimes the serial port stops receiving new data after 5 seconds, sometimes 30s, sometimes 60 and even 180s.
I'm not sure what's causing this issue what I can do to fix it. Any help is appreciated.
Thanks!
Here is my code below:
// Included libraries
#include <Arduino.h>
#include <Wire.h>
// Libraries required for VL53L4CD sensors
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
// Definitions
#define DEV_I2C Wire
#define SerialPort Serial
// variables
int alternate = 0; // Variable used to alternate between sensor sets
// Components.
VL53L4CD sensor_vl53l4cd_1(&DEV_I2C, 2); // xshut pin of distance sensor 1 connected to pin 2
VL53L4CD sensor_vl53l4cd_2(&DEV_I2C, 3); // xshut pin of distance sensor 2 connected to pin 3
VL53L4CD sensor_vl53l4cd_3(&DEV_I2C, 5); // xshut pin of distance sensor 3 connected to pin 5
VL53L4CD sensor_vl53l4cd_4(&DEV_I2C, 6); // xshut pin of distance sensor 4 connected to pin 6
/* Setup ---------------------------------------------------------------------*/
void setup()
{
// DISTANCE SENSOR SETUP
// Initialize serial for output.
SerialPort.begin(9600);
SerialPort.println("Starting...");
// Initialize I2C bus.
DEV_I2C.begin();
// Configure VL53L4CD number 1
Serial.println("Configuring distance sensor 1");
sensor_vl53l4cd_1.begin();
sensor_vl53l4cd_1.VL53L4CD_Off();
sensor_vl53l4cd_1.InitSensor();
sensor_vl53l4cd_1.VL53L4CD_SetI2CAddress(0x40);
sensor_vl53l4cd_1.VL53L4CD_SetRangeTiming(10, 0);
// Configure VL53L4CD number 2
Serial.println("Configuring distance sensor 2");
sensor_vl53l4cd_2.begin();
sensor_vl53l4cd_2.VL53L4CD_Off();
sensor_vl53l4cd_2.InitSensor();
sensor_vl53l4cd_2.VL53L4CD_SetI2CAddress(0x42);
sensor_vl53l4cd_2.VL53L4CD_SetRangeTiming(10, 0);
// Configure VL53L4CD number 3
Serial.println("Configuring distance sensor 3");
sensor_vl53l4cd_3.begin();
sensor_vl53l4cd_3.VL53L4CD_Off();
sensor_vl53l4cd_3.InitSensor();
sensor_vl53l4cd_3.VL53L4CD_SetI2CAddress(0x44);
sensor_vl53l4cd_3.VL53L4CD_SetRangeTiming(10, 0);
// Configure VL53L4CD number 4
Serial.println("Configuring distance sensor 4");
sensor_vl53l4cd_4.begin();
sensor_vl53l4cd_4.VL53L4CD_Off();
sensor_vl53l4cd_4.InitSensor();
sensor_vl53l4cd_4.VL53L4CD_SetI2CAddress(0x46);
sensor_vl53l4cd_4.VL53L4CD_SetRangeTiming(10, 0);
// Start Measurements
sensor_vl53l4cd_1.VL53L4CD_StartRanging();
sensor_vl53l4cd_2.VL53L4CD_StartRanging();
sensor_vl53l4cd_3.VL53L4CD_StartRanging();
sensor_vl53l4cd_4.VL53L4CD_StartRanging();
}
void loop()
{
// Read results from sensor set 1
if (alternate == 0){
VL53L4CD_Result_t results2;
VL53L4CD_Result_t results3;
// Read results from sensor set 1 (Sensors 2 and 3)
sensor_vl53l4cd_2.VL53L4CD_ClearInterrupt();
sensor_vl53l4cd_2.VL53L4CD_GetResult(&results2);
sensor_vl53l4cd_3.VL53L4CD_ClearInterrupt();
sensor_vl53l4cd_3.VL53L4CD_GetResult(&results3);
Serial.print("Distance 1-1 = ");
Serial.print(results2.distance_mm);
Serial.println(" ");
Serial.print("Distance 1-2 = ");
Serial.print(results3.distance_mm);
Serial.println(" ");
alternate = 1; // Change value so sensor set 2 is read in the following loop
}
// Read results from sensor set 2
else if (alternate == 1){
VL53L4CD_Result_t results1;
VL53L4CD_Result_t results4;
// Read results from sensor set 1 (Sensors 1 and 4)
sensor_vl53l4cd_1.VL53L4CD_ClearInterrupt();
sensor_vl53l4cd_1.VL53L4CD_GetResult(&results1);
sensor_vl53l4cd_4.VL53L4CD_ClearInterrupt();
sensor_vl53l4cd_4.VL53L4CD_GetResult(&results4);
Serial.print("Distance 2-1 = ");
Serial.print(results1.distance_mm);
Serial.println(" ");
Serial.print("Distance 2-2 = ");
Serial.print(results4.distance_mm);
Serial.println(" ");
alternate = 0; // Change value so sensor set 2 is read in the following loop
}
Serial.println(millis());
}