Possible to use 2x Adafruit VL53L4CD?

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

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?

User avatar
adafruit_support_carter
 
Posts: 29157
Joined: Tue Nov 29, 2016 2:45 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by adafruit_support_carter »

I2C is a shared bus - more than one thing can use the same SDA/SCL pins. HOWEVER - each device needs to have a unique I2C address. The VL53L4CD only has one I2C address. Using more than one is possible, but will require a little extra work. See here:
https://learn.adafruit.com/working-with ... s/overview

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

adafruit_support_carter wrote: Thu Apr 27, 2023 1:11 pm I2C is a shared bus - more than one thing can use the same SDA/SCL pins. HOWEVER - each device needs to have a unique I2C address. The VL53L4CD only has one I2C address. Using more than one is possible, but will require a little extra work. See here:
https://learn.adafruit.com/working-with ... s/overview
Thanks for the link, the breakout board might come in handy. But in the mean time I would have to define the different I2C addresses and then use the xshut pins correct?

User avatar
adafruit_support_carter
 
Posts: 29157
Joined: Tue Nov 29, 2016 2:45 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by adafruit_support_carter »

In theory, yes. The VL53L4CD does have a settable I2C address:
https://github.com/adafruit/VL53L4CD/bl ... i.cpp#L161
But using that can be cumbersome, since it must also be done over I2C, and will require coordinated use of the XSHUT pin.

This example might help?
https://github.com/adafruit/Adafruit_VL ... tended.ino

User avatar
robcranfill
 
Posts: 142
Joined: Wed Feb 13, 2013 4:14 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by robcranfill »

nh1628 wrote: Thu Apr 27, 2023 1:38 pm Thanks for the link, the breakout board might come in handy. But in the mean time I would have to define the different I2C addresses and then use the xshut pins correct?
"Support Carter" is correct in saying that this can, indeed, be done, and I just wanted to verify that - I have done so for my current project, a theremin-like device that uses two VL53L4CDs. It's really pretty straightforward: you wire the XSHUT pin of one device to a GPIO pin, and at startup you hold that pin high (low? sorry, I forget) and then re-program the address of the *other* one.

I could supply CircuitPython code if need be, but like I say, it's pretty simple.

/rob

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

robcranfill wrote: Sun Apr 30, 2023 8:09 pm
nh1628 wrote: Thu Apr 27, 2023 1:38 pm Thanks for the link, the breakout board might come in handy. But in the mean time I would have to define the different I2C addresses and then use the xshut pins correct?
"Support Carter" is correct in saying that this can, indeed, be done, and I just wanted to verify that - I have done so for my current project, a theremin-like device that uses two VL53L4CDs. It's really pretty straightforward: you wire the XSHUT pin of one device to a GPIO pin, and at startup you hold that pin high (low? sorry, I forget) and then re-program the address of the *other* one.

I could supply CircuitPython code if need be, but like I say, it's pretty simple.

/rob
Hi Rob,

I'm writing my code in C++ but I'd love to see how you did it in circuitpython as a reference to logic!

Thanks!

User avatar
robcranfill
 
Posts: 142
Joined: Wed Feb 13, 2013 4:14 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by robcranfill »

See my code at

https://github.com/RobCranfill/featheremin

Specifically, main.py at init_hardware(). You may note that I now use a VL53L4CD as the 2nd ToF sensor, but the logic is the same.

Let me know if you have questions. :-)

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

Thanks Rob!

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

I got it somewhat working, but was wondering if it was possible to get some help with understanding why it stops after giving me one result.

It seems like it reads once, but then goes into error state. Am I not turning the sensor back on?

Code: Select all

/* 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>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <WiFi.h>
//#include <ESPAsyncWebServer.h>

#define DEV_I2C Wire
#define SerialPort Serial
char report[64];

// address we will assign if dual sensor is present
uint8_t sensor1add = 0x30;
uint8_t 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;
float sensor1, sensor2;

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

//init Wifi
/*void initWifi(){
    Serial.print("Setting AP (Access Point)...");
  // No password
  WiFi.softAP (ssid);
  // Start WiFi Access Point
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.print(IP);
    // Print ESP8266 Local IP Adress
  Serial.println(WiFi.localIP());
}*/

//init sensors
void initSensor(VL53L4CD sensorX, uint8_t sensoradd)
{
  // Init sensor1
  //Set sensor address.
  sensorX.VL53L4CD_SetI2CAddress(sensoradd);
  // Configure VL53L4CD satellite component.
  sensorX.begin();
  // Switch off VL53L4CD satellite component.
  sensorX.VL53L4CD_Off();
  //Initialize VL53L4CD satellite component.
  sensorX.InitSensor();
  // Program the highest possible TimingBudget, without enabling the
  // low power mode. This should give the best accuracy
  sensorX.VL53L4CD_SetRangeTiming(200, 0);
  sensorX.VL53L4CD_SetOffset(-10);
  // Start Measurements
  sensorX.VL53L4CD_StartRanging();
}

//getSensor1Reading
void getSensor1Reading(uint8_t status, uint8_t NewDataReady)
{
  sensor1_vl53l4cd_sat.VL53L4CD_On();
  Serial.println("Sensor 1 is on");
  if ((!status) && (NewDataReady != 0)) {
    // (Mandatory) Clear HW interrupt to restart measurements
    sensor1_vl53l4cd_sat.VL53L4CD_ClearInterrupt();
    // Read measured distance. RangeStatus = 0 means valid data
    sensor1_vl53l4cd_sat.VL53L4CD_GetResult(&results1);
    snprintf(report, sizeof(report), "Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
             results1.range_status,
             results1.distance_mm,
             results1.signal_per_spad_kcps);
    sensor1 = results1.distance_mm;
    Serial.println((String)"This is Sensor1 " + (float)sensor1 + (String)"mm");
  }
  sensor1_vl53l4cd_sat.VL53L4CD_Off();
}

//getSensor2Reading
void getSensor2Reading(uint8_t status, uint8_t NewDataReady)
{
  sensor2_vl53l4cd_sat.VL53L4CD_On();
  Serial.println("Sensor 2 is on");
  if ((!status) && (NewDataReady != 0)) {
    // (Mandatory) Clear HW interrupt to restart measurements
    sensor2_vl53l4cd_sat.VL53L4CD_ClearInterrupt();
    // Read measured distance. RangeStatus = 0 means valid data
    sensor2_vl53l4cd_sat.VL53L4CD_GetResult(&results2);
    snprintf(report, sizeof(report), "Status = %3u, Distance = %5u mm, Signal = %6u kcps/spad\r\n",
             results2.range_status,
             results2.distance_mm,
             results2.signal_per_spad_kcps);
    sensor2 = results2.distance_mm;
    Serial.println((String)"This is Sensor2 " + (float)sensor2 + (String)"mm");
  }
  sensor2_vl53l4cd_sat.VL53L4CD_Off();
}

/*void offset()
{

}*/

/* Setup ---------------------------------------------------------------------*/
void setup()
{
  // Initialize serial for output.
  SerialPort.begin(115200);
  SerialPort.println("Starting...");
  // Initialize I2C bus.
  DEV_I2C.begin();
  
  initSensor(sensor1_vl53l4cd_sat, sensor1add);
  initSensor(sensor2_vl53l4cd_sat, sensor2add);
}

void loop()
{
  uint8_t NewDataReady1 = 0;
  uint8_t NewDataReady2 = 0;
  uint8_t status1;
  uint8_t status2;
  do {
    status1 = sensor1_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady1);
    status2 = sensor2_vl53l4cd_sat.VL53L4CD_CheckForDataReady(&NewDataReady2);
  } while (!NewDataReady1 || !NewDataReady2);
  getSensor1Reading(status1, NewDataReady1);
  getSensor2Reading(status2, NewDataReady2);
}
Output:

Code: Select all

--- forcing DTR inactive
--- forcing RTS inactive
--- Terminal on COM9 | 115200 8-N-1
--- Available filters and text transformations: colorize, debug, default, direct, esp32_exception_decoder, hexlify, log2file, nocontrol, printable, send_on_enter, time
--- More details at https://BANNED/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Starting...
This is Sensor1 12.00mm
This is Sensor2 253.00mm
om(): i2cWriteReadNonStop returned Error -1
[ 23624][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[ 23631][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[ 23638][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[ 23645][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[ 23652][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1
[ 23659][E][Wire.cpp:499] requestFrom(): i2cWriteReadNonStop returned Error -1

User avatar
robcranfill
 
Posts: 142
Joined: Wed Feb 13, 2013 4:14 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by robcranfill »

Man, I'd like to help ya, but that code looks *way* different from the easy CircuitPython stuff I've been doing - way past my coding abilities. Sorry. :-]

That said, I've been running into issues using the VL53L4CD, myself, somewhat along the same lines, although of a more intermittent nature. One of these days I'm gonna try to nail down just what's going on, but I doubt it's the same as your issue.

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

robcranfill wrote: Mon May 15, 2023 4:31 pm See my code at

https://github.com/RobCranfill/featheremin

Specifically, main.py at init_hardware(). You may note that I now use a VL53L4CD as the 2nd ToF sensor, but the logic is the same.

Let me know if you have questions. :-)
Rob, how did you wire the SDA/SCL from the second sensor to the first? Are you able to do it through the pinouts? Or did you have to pass it through the stemma QT like this image?
https://learn.adafruit.com/working-with ... te-address

User avatar
robcranfill
 
Posts: 142
Joined: Wed Feb 13, 2013 4:14 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by robcranfill »

Yes, I used the ever-so-convenient Stemma QT connectors on the Feather 2040.

I haven’t done it but I think it’s fairly straightforward to just daisy-chain the I2C data lines (“data” meaning all the signals, including clock) together from one device to the next. I guess there may be some issue with pull-down (pull-up?) resistors, but if your Arduino doesn’t have Stemma, this must be a fairly common task that I’d imagine there’s a lot of info out there on.

User avatar
nh1628
 
Posts: 18
Joined: Thu Apr 27, 2023 12:09 pm

Re: Possible to use 2x Adafruit VL53L4CD?

Post by nh1628 »

robcranfill wrote: Fri May 26, 2023 8:47 pm Man, I'd like to help ya, but that code looks *way* different from the easy CircuitPython stuff I've been doing - way past my coding abilities. Sorry. :-]

That said, I've been running into issues using the VL53L4CD, myself, somewhat along the same lines, although of a more intermittent nature. One of these days I'm gonna try to nail down just what's going on, but I doubt it's the same as your issue.
No worries, and I found the issue, it seems like it didn't like the way I set my addresses. I changed it from 0x29 for sensor 1 to 0x51 and left sensor 2 as 0x28 and it seems to be happy with that. Not sure why it didn't like the addresses before.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”