Reading mutliple VL53L0X sensors

Actually the Xshut pins are supposed to be used when connecting multiple VL53L0X sensors, to turn off and turn on sensors to assign different addresses.
But what if i connect according to the following schmatic


And controlling the vcc pins using the pwm of arduino to turn it on and off using the following code

#include "Adafruit_VL53L0X.h"

// Define power control pins
#define PWR_LOX1 2
#define PWR_LOX2 3

// Create instances of the VL53L0X sensors
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

// This holds the measurements
VL53L0X_RangingMeasurementData_t measure1;
VL53L0X_RangingMeasurementData_t measure2;

void setup() {
  Serial.begin(115200);
  
  // Initialize power control pins
  pinMode(PWR_LOX1, OUTPUT);
  pinMode(PWR_LOX2, OUTPUT);

  digitalWrite(PWR_LOX1, LOW);
  digitalWrite(PWR_LOX2, LOW);

  Serial.println(F("Powering up VL53L0X sensors..."));

  // Power on sensor 1
  digitalWrite(PWR_LOX1, HIGH);
  delay(10);
  if (!lox1.begin(0x30)) {
    Serial.println(F("Failed to boot first VL53L0X"));
    while (1);
  }

  // Power on sensor 2
  digitalWrite(PWR_LOX2, HIGH);
  delay(10);
  if (!lox2.begin(0x31)) {
    Serial.println(F("Failed to boot second VL53L0X"));
    while (1);
  }

  Serial.println(F("VL53L0X sensors initialized successfully"));
}

void loop() {
  
  
  lox1.rangingTest(&measure1, false);
  lox2.rangingTest(&measure2, false);
  

  Serial.print(F("1: "));
  if (measure1.RangeStatus != 4) {  
    Serial.print(measure1.RangeMilliMeter);
    Serial.print(F(" mm"));
  } else {
    Serial.print(F("Out of range"));
  }
  
  Serial.print(F(" | 2: "));
  if (measure2.RangeStatus != 4) {
    Serial.print(measure2.RangeMilliMeter);
    Serial.println(F(" mm"));
  } else {
    Serial.println(F("Out of range"));
  }
  
  delay(50);
}

This code works and gives the correct readings of both the sensors.

Q1. Can the above method be used? Am i doing anything wrong or harming the sensors in anyway? If not then what is the point of xshut pin?

Now, I wanted to get the reading continuously from the sensors. For that i modified the following code which works perfectly fine for a single sensor:

CODE FOR SINGLE SENSOR:

#include "Adafruit_VL53L0X.h"

Adafruit_VL53L0X lox = Adafruit_VL53L0X();

void setup() {
  Serial.begin(115200);

  // wait until serial port opens for native USB devices
  while (! Serial) {
    delay(1);
  }

  Serial.println("Adafruit VL53L0X test.");
  if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power
  Serial.println(F("VL53L0X API Continuous Ranging example\n\n"));

  // start continuous ranging
  lox.startRangeContinuous();
}

void loop() {
  if (lox.isRangeComplete()) {
    Serial.print("Distance in mm: ");
    Serial.println(lox.readRange());
  }
}

To this, for multiple sensor:

#include "Adafruit_VL53L0X.h"

// Define power control pins
#define PWR_LOX1 2
#define PWR_LOX2 3

// Create instances of the VL53L0X sensors
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

void setup() {
  Serial.begin(115200);
  
  // Initialize power control pins
  pinMode(PWR_LOX1, OUTPUT);
  pinMode(PWR_LOX2, OUTPUT);

  Serial.println(F("Powering up VL53L0X sensors..."));

  // Power on sensor 1
  digitalWrite(PWR_LOX1, HIGH);
  delay(50); // Give sensor time to power up
  if (!lox1.begin(0x30)) {
    Serial.println(F("Failed to boot first VL53L0X"));
    while (1);
  }

  // Power on sensor 2
  digitalWrite(PWR_LOX2, HIGH);
  delay(50);
  if (!lox2.begin(0x31)) {
    Serial.println(F("Failed to boot second VL53L0X"));
    while (1);
  }

  Serial.println(F("VL53L0X sensors initialized successfully"));

  // Start continuous mode
  lox1.startRangeContinuous();
  lox2.startRangeContinuous();
}

void loop() {
  Serial.print(F("1: "));
  if (lox1.isRangeComplete()) {  
    Serial.print(lox1.readRange());
    Serial.print(F(" mm"));
  } else {
    Serial.print(F("Waiting..."));
  }

  Serial.print(F(" | 2: "));
  if (lox2.isRangeComplete()) {
    Serial.print(lox2.readRange());
    Serial.println(F(" mm"));
  } else {
    Serial.println(F("Waiting..."));
  }

  //delay(50);
}

But the code for multiple sensor does not work like the code for single sensor , without the commented out delay line.

Q2. Why does the code for multiple sensor need a delay to be specified, while the single sensor and read continuously without a a delay being explicitly specified. Is this in anyway related to the way to Q1.

  • Try using P channel MOSFET HIGH Side switches.

Use a logic level MOSFET.

Is there any issue if I don't use a mosfet

  • A MOSFET would be best but you could try a PNP BJT.

VL datasheet gives typical current draw of 19mA with absolute max peak of 40mA.
That's on high side to be powered from arduino pin. Depending what arduino you use.
For uno max current draw from pin afaik is 20mA / peak 40mA, so there's no tolerance left if you power VL from pin.
Some arduinos have max 8mA.

While it might work without problems, I suggest transistor circuit on post above.
Or use xshut.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.