Skip to main content
Visitor II
June 27, 2021
Question

Hi guys, Now I have some troubles with 2 VL53L1X that measure the distance at the same time but I can't init these sensor ?

  • June 27, 2021
  • 1 reply
  • 1094 views

Hi guys,

Now I have some troubles with 2 VL53L1X that measure the distance at the same time but I can't init these sensor ? Here my code I code on Esp32. I'm using SparkFun library

https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library.git

#include <Arduino.h>

#include <WiFi.h>

#include <Wire.h>

#include <Ethernet.h>

#include <SPI.h>

#include <VL53L1X.h>

#define SHUTDOWN_PIN 2

#define INTERRUPT_PIN 3

#define Sensor1_newAddress 47

#define Sensor2_newAddress 45

SFEVL53L1X Sensor1;

SFEVL53L1X Sensor2;

uint16_t distance0, distance1;

// #define SHUTDOWN_PIN 2 // có thể mở

// #define INTERRUPT_PIN 3

#define XSHUT_pin1 33

#define XSHUT_pin2 32

void setup (void)

{

  Wire.begin();

  Wire.setClock(400000); // use 400 kHz I2C

  Serial.begin(115200);

  Serial.println("Begin.....");

    

  Wire.begin();

  Wire.setClock(300000); 

  pinMode(XSHUT_pin1, OUTPUT);

  pinMode(XSHUT_pin2, OUTPUT);    

   

  pinMode(XSHUT_pin1, INPUT);

  if (!Sensor1.init())

  {

    Serial.println("VL53L1X custom sensor, Failed to detect and initialize sensor1!");

    while (1);

  }

  delay(10);

  Sensor1.setI2CAddress(Sensor1_newAddress);

   

  pinMode(XSHUT_pin2, INPUT);

  if (!Sensor2.init())

  {

    Serial.println("VL53L1X custom sensor, Failed to detect and initialize sensor2!");

    while (1);

  }

  delay(10);

  Sensor2.setI2CAddress(Sensor2_newAddress);

   

    

  // Sensor1.setTimeout(500); // có thể mở

  // Sensor2.setTimeout(500);

  Sensor1.setDistanceModeLong();

  Sensor1.setTimingBudgetInMs(50000);

   

  Sensor2.setDistanceModeLong();

  Sensor2.setTimingBudgetInMs(50000);

}

void loop(void)

{  

  distance0 = Sensor1.getDistance(); // return distance 

  Sensor1.stopRanging(); //Stops taking measurements // after measure the first distance stop measurement

  Sensor1.clearInterrupt(); // Clear the interrupt flag // clear the interrupt 

   

  distance1 = Sensor2.getDistance(); // return distance 

  Sensor2.stopRanging(); //Stops taking measurements // after measure the first distance stop measurement

  Sensor2.clearInterrupt(); // Clear the interrupt flag // clear the interrupt 

   

  Serial.print("Distance 0: ");

  Serial.println(distance0);

  Serial.print("Distance 1: ");

  Serial.println(distance1);

     

  return;

}

Thanks for help

    This topic has been closed for replies.

    1 reply

    ST Employee
    June 28, 2021

    I didn't read all the code, but I have 2 suggestions.

    1 - your MCU is much faster than the sensors, I think you will find a 'wait_for_booted' in that library somewhere.

    You were turning the sensor on in one clock cycle and initializing it in the next. It's a good sensor, but it does have to boot up.

    (or you can just wait a few milliseconds.)

    2 - The other trick is that the I2C is generally specified by the write address - and those have zero's in the LSbit.

    It's also a superstition of mine that I put more distance between the addresses. You are right that they have to be 2 apart, but I've had better luck with 10 appart.

    Download

    STSW-IMG017

    2D LIDAR using multiple VL53L1X Time-of-Flight long distance ranging sensors.

    It has an example of how we did 9 sensors. It will give you some ideas.

    • john