Using 2x Adafruit VL53L4CD

Hello, pretty new with Arduino stuff, and I haven't received any parts yet but I was wondering if it was possible to run 2x Adafruit VL53L4CD sensors at the same time.

I noticed in the fritzing diagram that it connects to the SCL and SDA pins to send data, and the Arduino that I will be using (Adafruit ESP32-S3 TFT Feather) only has one of each pin. Would I need a breakout board for this?

The VL53L4CD I2C address appears to be fixed, so only one can be actively connected to the I2C bus at a time.

It might be possible to use the XSHUT shutdown pin as a device select pin, and if so, you will need an extra digital IO "select" pin for each device.

Post on the Adafruit forum, perhaps the engineers will have other suggestions. With some of those TOF sensors, the I2C address can be changed programmatically.

So it would be possible if I were to do something like this?

void read_dual_sensors() {
  
  uint8_t NewDataReady = 0;
  VL53L4CD_Result_t results;
  uint8_t status;
  char report[64];
  sensor_vl53l4cd_sat1.VL53L4CD_GetResult(&result1); // pass in 'true' to get debug data printout!
  sensor_vl53l4cd_sat2.VL53L4CD_GetResult(&result2); // pass in 'true' to get debug data printout!

  // print sensor one reading
  Serial.print("1: ");
  if(result1 != 0) {     // if not out of range
    distance1 = sensor_vl53l4cd_sat1.VL53L4CD_GetResult(&result1);    
    Serial.print(distance1);
    Serial.print("mm");    
  } else {
    Serial.print("Out of range");
  }
  
  Serial.print(" ");

  // print sensor two reading
  Serial.print("2: ");
  if(result2 != 0) {
    distance2 = sensor_vl53l4cd_sat2.VL53L4CD_GetResult(&result2);
    Serial.print(distance2);
    Serial.print("mm");
  } else {
    Serial.print("Out of range");
  }
  
  Serial.println();
}

The posted snippet does not show how you deal with the I2C address problem.

Sorry I didn't select the whole thing for some reason,

#include <Arduino.h>
#include <Wire.h>
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>

// Defining I2C Ports
#define DEV_I2C Wire
#define SerialPort Serial

// address we will assign if dual sensor is present
#define VL531_ADDRESS = VL53L4CD_SetI2CAddress(0x30);
#define VL532_ADDRESS = VL53L4CD_SetI2CAddress(0x31);
float distance1,distance2, offset;

// this holds the measurement
uint8_t measure1, measure2;
VL53L4CD_Result_t result1, result2;

// set the pins to shutdown
#define SHT_VL531 0
#define SHT_VL532 1

// Components.
VL53L4CD sensor_vl53l4cd_sat1(&DEV_I2C, 0);
VL53L4CD sensor_vl53l4cd_sat2(&DEV_I2C, 1);


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

  // activating VL531 and reseting VL532
  digitalWrite(SHT_VL531, HIGH);
  digitalWrite(SHT_VL532, LOW);

  // initing sensor_vl53l4cd_sat1
  if(!sensor_vl53l4cd_sat1.begin()) {
    Serial.println(F("Failed to boot first VL532"));
    while(1);
  }
  delay(10);

  // activating sensor_vl53l4cd_sat2
  digitalWrite(SHT_VL532, HIGH);
  delay(10);

  //initing sensor_vl53l4cd_sat2
  if(!sensor_vl53l4cd_sat2.begin()) {
    Serial.println(F("Failed to boot second VL532"));
    while(1);
  }
}

void read_dual_sensors() {
  
  uint8_t NewDataReady = 0;
  VL53L4CD_Result_t results;
  uint8_t status;
  char report[64];
  sensor_vl53l4cd_sat1.VL53L4CD_GetResult(&result1); // pass in 'true' to get debug data printout!
  sensor_vl53l4cd_sat2.VL53L4CD_GetResult(&result2); // pass in 'true' to get debug data printout!

  // print sensor one reading
  Serial.print("1: ");
  if(result1 != 0) {     // if not out of range
    distance1 = sensor_vl53l4cd_sat1.VL53L4CD_GetResult(&result1);    
    Serial.print(distance1);
    Serial.print("mm");    
  } else {
    Serial.print("Out of range");
  }
  
  Serial.print(" ");

  // print sensor two reading
  Serial.print("2: ");
  if(result2 != 0) {
    distance2 = sensor_vl53l4cd_sat2.VL53L4CD_GetResult(&result2);
    Serial.print(distance2);
    Serial.print("mm");
  } else {
    Serial.print("Out of range");
  }
  
  Serial.println();
}

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

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

  pinMode(SHT_VL531, OUTPUT);
  pinMode(SHT_VL532, OUTPUT);

  Serial.println("Shutdown pins inited...");

  digitalWrite(SHT_VL531, LOW);
  digitalWrite(SHT_VL532, LOW);

  Serial.println("Both in reset mode...(pins are low)");
  
  
  Serial.println("Starting...");
  setID();
 
}

void loop() {
   
  read_dual_sensors();
  delay(100);
}

Please post a link to the library with the SetI2CAddress function.

This is what I used

Is there some documentation describing how the I2C address is changed? That information is not in the device data sheet.

In any case, try the code and let us know if it works.

Will do! Once the parts come in

I can't seem to get past the first init stage (initing sensor1)

Would anyone be able to help with seeing what is wrong with my code?

/* Includes ------------------------------------------------------------------*/
#include <Arduino.h>
#include <Wire.h>
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <SPI.h>

#define DEV_I2C Wire
#define SerialPort Serial

// address we will assign if dual sensor is present
#define sensor1add 0x30
#define sensor2add 0x31
// set the pins to shutdown
#define xshut1 A0
#define xshut2 A1
#define GFX_BL DF_GFX_BL

//this holds the measurement
VL53L4CD_Result_t results1;
VL53L4CD_Result_t results2;
int sensor1, sensor2;

// Components.
VL53L4CD sensor1_vl53l4cd_sat(&DEV_I2C, A0);
VL53L4CD sensor2_vl53l4cd_sat(&DEV_I2C, A1);
/* Setup ---------------------------------------------------------------------*/

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

  // activating Sensor1 and reseting Sensor2
  digitalWrite(xshut1, HIGH);
  digitalWrite(xshut2, LOW);

  // initing Sensor1
  if(!sensor1_vl53l4cd_sat.begin()) {
    Serial.println(F("Failed to boot first VL53L4CD1"));
    while(1);
  }
  delay(10);

  // activating Sensor2
  digitalWrite(xshut2, HIGH);
  delay(10);

  //initing Sensor2
  if(!sensor2_vl53l4cd_sat.begin()) {
    Serial.println(F("Failed to boot second VL53L4CD2"));
    while(1);
  }
}

void read_dual_sensors() {
  
  sensor1_vl53l4cd_sat.VL53L4CD_GetResult(&results1); // pass in 'true' to get debug data printout!
  sensor2_vl53l4cd_sat.VL53L4CD_GetResult(&results2); // pass in 'true' to get debug data printout!

  // print sensor one reading
  Serial.print("Sensor 1 Distance: ");
  if(results1.distance_mm > 300) {     // if not out of range
    sensor1 = results1.distance_mm;    
    Serial.print(sensor1);
    Serial.print("mm");    
  } else {
    Serial.print("Out of range");
  }
  
  Serial.print(" ");

  // print sensor two reading
  Serial.print("Sensor 2 Distance: ");
  if(results2.distance_mm > 300) {     // if not out of range
    sensor2 = results2.distance_mm;    
    Serial.print(sensor2);
    Serial.print("mm");    
  } else {
    Serial.print("Out of range");
  }
  
  Serial.println();
}


void setup()
{
  // Initialize serial for output.
  SerialPort.begin(115200);
  SerialPort.println("Starting...");

  // Initialize I2C bus.
  DEV_I2C.begin();

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

  pinMode(xshut1, OUTPUT);
  pinMode(xshut2, OUTPUT);

  Serial.println("Shutdown pins inited...");

  digitalWrite(xshut1, LOW);
  digitalWrite(xshut2, LOW);

  Serial.println("Both in reset mode...(pins are low)");
  
  
  Serial.println("Starting...");
  setID();
}

void loop()
{
  read_dual_sensors();
  delay(100);
}

I now have my code like this, however, it is stuck after starting and not reporting any distance measurements. I can't seem to see why.

/* Includes ------------------------------------------------------------------*/
#include <Arduino.h>
#include <Wire.h>
#include <vl53l4cd_class.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <stdlib.h>
#include <SPI.h>
#define DEV_I2C Wire
#define SerialPort Serial

// address we will assign if dual sensor is present
#define sensor1add 0x30
#define sensor2add 0x31

// set the pins to shutdown
#define xshut1 A0
#define xshut2 A1
#define GFX_BL DF_GFX_BL

//this holds the measurement
VL53L4CD_Result_t results1;
VL53L4CD_Result_t results2;
int sensor1, sensor2;

// Components.
VL53L4CD sensor1_vl53l4cd_sat(&DEV_I2C, A0);
VL53L4CD sensor2_vl53l4cd_sat(&DEV_I2C, A1);

/* Setup ---------------------------------------------------------------------*/
void setup()
{
  // Initialize serial for output.
  SerialPort.begin(115200);
  SerialPort.println("Starting...");

  // Initialize I2C bus.
  Wire.begin();

  pinMode(xshut1, OUTPUT);
  pinMode(xshut2, OUTPUT);
  
  digitalWrite(xshut1, LOW);
  digitalWrite(xshut2, LOW);
  
  delay(10);
  
  digitalWrite(xshut1, HIGH);
  delay(10);
  sensor1_vl53l4cd_sat.VL53L4CD_SetI2CAddress(sensor1add);
  sensor1_vl53l4cd_sat.begin();
  
  digitalWrite(xshut2, HIGH);
  delay(10);
  sensor2_vl53l4cd_sat.VL53L4CD_SetI2CAddress(sensor2add);
  sensor2_vl53l4cd_sat.begin();
}

void loop()
{
  uint8_t NewDataReady = 0;

  // Read distance from sensor 1
  sensor1_vl53l4cd_sat.VL53L4CD_StartRanging();
  while (!sensor1_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady)) {
    delay(1);
  }
  sensor1_vl53l4cd_sat.VL53L4CD_GetResult(&results1);
  uint16_t distance1 = results1.distance_mm;

  // Read distance from sensor 2
  sensor2_vl53l4cd_sat.VL53L4CD_StartRanging();
  while (!sensor2_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady)) {
    delay(1);
  }
  sensor2_vl53l4cd_sat.VL53L4CD_GetResult(&results2);
  uint16_t distance2 = results2.distance_mm;

  // Print distances
  Serial.print("Distance from Sensor 1: ");
  Serial.print(distance1);
  Serial.println(" mm");

  Serial.print("Distance from Sensor 2: ");
  Serial.print(distance2);
  Serial.println(" mm");

  // Delay before next measurement
  delay(100);
}







Hello, I am having issues with getting my ESP32-S3 QTPY to connect with the VL53L4CD TOF sensor. Were you able to get this to work?

Still trying to figure out getting 2 sensors to simultaneously read, but I was able to get each of the sensors to work with my ESP32.

What is going on with yours?

Thank you for replying. I am getting this error:
Traceback (most recent call last):
File "code.py", line 7, in
File "adafruit_vl53l4cd.py", line 78, in init
RuntimeError: Wrong sensor ID or type!

I've never used circuit python so not sure how much I can help, but also you didn't post your code so idk what line 7 is.

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