Getting 2 VL53L0X sensors to record seperate readings

Hello,

I am trying to get 2 VL53L0X sensors to give me separate distance readings. However, based on my code, both the VL53L0X sensors give me the same distance reading.

Example:
I place an object in front of my left VL53L0x sensor and both my sensor readings drop in value (information is displayed on Serial Monitor using Arduino).

Can you tell me how I can correct my code?

// sensor has a correction of +10mm
// if the actual distance is 40mm, sensor reads 50mm

#include <VL53L0X.h>
#include <Wire.h>
VL53L0X sensor;   // Sensor 1
VL53L0X sensor1;  // Sensor 2

void setup()
{
  pinMode(12,INPUT_PULLUP);   // Sensor 1
  pinMode(11,INPUT_PULLUP);   // Sensor 2
  digitalWrite(12,HIGH);
  digitalWrite(11, HIGH);
  Serial.begin(9600);
  Wire.begin();

  sensor.init();          // Sensor 1
  sensor.setTimeout(500);
  sensor.startContinuous();

  sensor1.init();       // Sensor 2
  sensor1.setTimeout(500);
  sensor1.startContinuous();
}

void loop()
{
  int distance =sensor.readRangeContinuousMillimeters();
  int distance1 =sensor1.readRangeContinuousMillimeters();
  //int distance =sensor.startContinuous(100);
  
  Serial.print("Distance 1 : ");
  Serial.print(distance);
  Serial.print("mm");
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.print("   Distance 2 : ");
  Serial.print(distance1);
  Serial.print("mm");
  if (sensor1.timeoutOccurred()) { Serial.print(" TIMEOUT");
  }

  Serial.println();
  delay(100);
}

Hello.

To use multiple I2C devices, like your VL53L0X sensors, each device needs to be configured with a unique I2C address. Both of your VL53L0X senors will boot up with a default 7-bit address of 0101001b (41). It can be changed to any other value by writing one of the device configuration registers, but the new address only applies until the sensor is reset or powered off. This application note describes how to use multiple VL53L0X sensors on the same I²C bus by individually bringing each sensor out of reset and assigning it a unique address.

The simplest procedure if you are using our board and our VL53L0X Arduino library will be to connect the XSHUT pins one each of your VL53L0X senors to your Arduino and at the beginning of your program set those pins as outputs and drive them low. Then, enable each sensor one at a time by reconfiguring the XSHUT pin as an input (make it high impedance) to let the board to pull it high and use the setAddress command from our library to change the I2C device for the corresponding sensor. Do not just drive the XSHUT pins high if you are using an Arduino that operates at 5V; the XSHUT on our VL53L0X carrier is not level shifted so it can only handle 3.3V. When you are done, you could use the getAddress command from our library and print the results to your Serial Monitor to confirm that it worked.

For an example, you could look at the ContinuousMultipleSensors example from our VL53L1X Arduino library, which is for a different ST time-of-flight sensor carrier, but is structured similarly to our VL53L0X library.

- Patrick