Inter-Vl53l0x sensors timing issue

Hi,

I have two Vl53l0x sensors separated by a distance of (say) 60mm, both connected to an Esp32 wroom, to calculate the time an object takes to trigger first one, then the other sensor.

The sensors are programmed to work in either direction and this works ok. However, i have noticed that the time taken in one direction is usually several 10s or even 100s of milliseconds less than in the other direction.

I swapped the sensors over and this made no difference to the times. However, when i swapped the gpios, the time difference swaps too.

So, does this mean that there is a performance issue with the Esp32 pins (i have read but didn’t understand about fast and slow pins) or is this something to do with the configuration cof the sensors?

Thanks

Connal

Hello, Connal.

Can you post a picture or diagram that shows how you have everything connected?

- Patrick

Hi,

Here is the wiring diagram:

and here is the relevant code:

// required libraries

#include <Wire.h> //I2C library
#include <VL53L0X.h>


// Sensor Stuff
#define SENSOR_1_ADDRESS 0x30
#define SENSOR_2_ADDRESS 0x31
#define SENSOR_1_INTERRUPT 25
#define SENSOR_1_SHUTDOWN 19
#define SENSOR_2_INTERRUPT 16
#define SENSOR_2_SHUTDOWN 17
volatile byte sensor_1_State = LOW;
volatile byte sensor_2_State = LOW;

VL53L0X sensor_1;
VL53L0X sensor_2;

// variables for capturing sensor activation
byte l_r, state;

// Calibration values
int sensor_1Calibrated;
int sensor_2Calibrated;
int passes = 4;
int sensor_1Offset = 20;
int sensor_2Offset = 60;

bool calibrateMode = false;
bool LR = false;

unsigned long start_us, stop_us, measured_us, waittime = 2, trigger_start_us_S1, trigger_start_us_S2;
float m_per_s;
float km_per_hr;
float mi_per_hr = 0.00;

void setID()
{
  // all reset
  digitalWrite(SENSOR_1_SHUTDOWN, LOW);
  digitalWrite(SENSOR_2_SHUTDOWN, LOW);
  delay(10);
  // all unreset
  digitalWrite(SENSOR_1_SHUTDOWN, HIGH);
  digitalWrite(SENSOR_2_SHUTDOWN, HIGH);
  delay(10);

  // activating sensor_1 and resetting sensor_2
  digitalWrite(SENSOR_1_SHUTDOWN, HIGH);
  digitalWrite(SENSOR_2_SHUTDOWN, LOW);

  // initiating sensor_1
  Serial.println("S1 start");
  sensor_1.init(true);
  Serial.println("01");
  delay(100);
  sensor_1.setAddress(SENSOR_1_ADDRESS);
  delay(10);

  // activating sensor_2
  digitalWrite(SENSOR_2_SHUTDOWN, HIGH);
  delay(10);

  // initiating sensor_2
  Serial.println("S2 start");
  sensor_2.init(true);
  Serial.println("02");
  delay(100);
  sensor_2.setAddress(SENSOR_2_ADDRESS);
  delay(10);
}

void VL53LOXISR1()
{
  sensor_1_State = digitalRead(SENSOR_1_INTERRUPT);
  trigger_start_us_S1 = micros();
}

void VL53LOXISR2()
{
  sensor_2_State = digitalRead(SENSOR_2_INTERRUPT);
  trigger_start_us_S2 = micros();
}

void setup()
{

  Serial.begin(115200);
 
  Wire.begin();

  // wait until serial port opens for native USB devices
  while (!Serial)
  {
    delay(1);
  }
  pinMode(SENSOR_1_SHUTDOWN, OUTPUT);
  pinMode(SENSOR_2_SHUTDOWN, OUTPUT);
  pinMode(SENSOR_1_INTERRUPT, INPUT_PULLUP);
  pinMode(SENSOR_2_INTERRUPT, INPUT_PULLUP);

  attachInterrupt(SENSOR_1_INTERRUPT, VL53LOXISR1, CHANGE);
  attachInterrupt(SENSOR_2_INTERRUPT, VL53LOXISR2, CHANGE);

  Serial.println(F("Shutdown pins initiated..."));
  digitalWrite(SENSOR_1_SHUTDOWN, LOW);
  digitalWrite(SENSOR_2_SHUTDOWN, LOW);
  Serial.println(F("Both in reset mode...(pins are low)"));
  Serial.println(F("Starting..."));
  setID();
  Serial.println("addresses set");
  sensor_1.setTimeout(500);
  sensor_1.setMeasurementTimingBudget(17600);
  sensor_1.startContinuous();
  sensor_2.setTimeout(500);
  sensor_2.setMeasurementTimingBudget(17600);
  sensor_2.startContinuous();



}

void loop()
{


  if (/*conditions*/)
  {
    switch (state)
    {
    case 0: // initial state, ready to start measuring
      if (sensor_1_State == LOW && (sensor_1.readRangeContinuousMillimeters() - sensor_1Offset) < maxDist && (sensor_1.readRangeContinuousMillimeters() - sensor_1Offset) > minDist)
      {
        sensor_1Calibrated = sensor_1.readRangeContinuousMillimeters() - sensor_1Offset;
        start_us = trigger_start_us_S1;
        l_r = 0;
        state = 1;
      }
      if (sensor_2_State == LOW && (sensor_2.readRangeContinuousMillimeters() - sensor_2Offset) < maxDist && (sensor_2.readRangeContinuousMillimeters() - sensor_2Offset) > minDist)
      {
        
        sensor_2Calibrated = sensor_2.readRangeContinuousMillimeters() - sensor_2Offset;
        start_us = trigger_start_us_S2;
        l_r = 1;
        state = 1;
      }
      break;

    case 1: // wait for the other sensor to be triggered
      if (l_r)
      {
        while ((sensor_1.readRangeContinuousMillimeters() - sensor_1Offset) < minDist || (sensor_1.readRangeContinuousMillimeters() - sensor_1Offset) > maxDist)
        {

        } // loop here until sensor L is triggered
      }
      else
      {
        bitMapPos = -130;
        while ((sensor_2.readRangeContinuousMillimeters() - sensor_2Offset) < minDist || (sensor_2.readRangeContinuousMillimeters() - sensor_2Offset) > maxDist)
        {

        } // loop here until sensor R is triggered
      }
      stop_us = micros();

      state = 2;
      break;

    case 2:                                    // calculate and show speed values
      measured_us = stop_us - start_us - 10UL; // -10 microseconds to compensate for code delay
      Serial.print(F("measured_us : "));
      Serial.println(measured_us);
      m_per_s = float(scaleCalc) * float(sensor_distance * 1000) / float(measured_us);
      km_per_hr = 3.6 * m_per_s;
      mi_per_hr = 2.23694 * m_per_s;

      stop_us = micros();
      state = 2;
      
      // measured_microseconds is needed for a move of sensor_distance micrometers
      // measured_us * train_length / sensor_distance) time is needed for train_length to pass


      // Serial.println(F("Waiting for train..."));
      state = 0;
      break;
    }
  }
}


Your connections look okay from what I can tell. You might try moving the GPIO1 connections to other pins just in case some of the pins on the ESP32 are limited in ways that others are not, but we are not familiar enough with the ESP32 to offer specific guidance.

It is typically beyond the scope of our technical support to offer assistance with user code (especially in cases like this where it seems look like you are not using any of our products), but I took a quick look and did not see any immediate explanation for the asymmetrical behavior you described. You could try printing timestamps or time differences at various points to try to identify where a lot of time is being spent, but keep in mind that the amount of time these time-of-flight sensors take to complete measurements (typically around 33 ms for the VL53L0X) can make them difficult or impractical to use in precise timing gate type applications (which you seemed to be describing in your first post). You might have more success if you use some of the ST VL53L0X API’s more advanced interrupt threshold settings, but unfortunately, it is not easy to access those settings using our Arduino library.

- Patrick

Thanks for the information

Connal