stm32duino / VL53L4CD

Arduino library to support the VL53L4CD Time-of-Flight high accuracy proximity sensor
BSD 3-Clause "New" or "Revised" License
5 stars 7 forks source link

Setting Up Multiple Sensors #6

Closed Hs-Cheng closed 2 years ago

Hs-Cheng commented 2 years ago

Hi,

My problem is similar to another issue that was closed two weeks ago. I am trying to setup two VL53L4CD sensors together, and I would list my questions below.

  1. Why it is important to connect the XSHUT pins when setting up multiple sensors? Can I just let them floating?
  2. During the setup phase, both the XSHUT pins should be "HIGH" to keep the sensors turn on, is that correct?
  3. By reading another issue mentioned above, I've already known that each sensor occupies two I2C addresses (for example, 0x52 and 0x53). Are there other restrictions when setting up the addresses? Can I just use some random numbers that are not continuous like [0x12, 0x28]?

I also attached the code of:

  1. Declaring two instances of the sensor object.
  2. The setup function of my Arduino code.

Thank you very much for your assistance.

All the best, HC

Screen Shot 2022-05-24 at 8 48 54 PM
cparata commented 2 years ago

Hello @Hs-Cheng , the XSHUT pin is fundamental if you use multiple sensors because it avoids conflicts on the I2C bus. When you power the sensors, all of these sensors have the same I2C address (0x52), so if they are connected to the same I2C bus you get an hardware conflict. To avoid it, you must disable all the sensors at the beginning (putting all the XSHUT pins to LOW). Then, you can initialize the sensors one by one as you correctly did in your sketch. If you are using just one sensor, you cannot leave the XSHUT pin floating but at least connected to a Pull-Up resistor to keep the sensor active. Yes, you can use random numbers, the important thing is that they are not contiguous. [0x12, 0x28] are fine. Anyway, your sketch seems fine to me. Doesn't it work? Did you connect correctly all the pins? Best Regards, Carlo

cparata commented 2 years ago

Sorry @Hs-Cheng , now I found an issue in your sketch. You are using the same XSHUT pin on the 2 sensors (pin 17). This is wrong. You must assign a different XSHUT pin to the 2 sensors otherwise you are not able to control them in an independent way. This is because, when you initialize one sensor, the other one must be disable to avoid a conflict on the bus. Please, try for example to assign pin 17 as XSHUT pin for sensor 1 and pin 18 as XSHUT pin for sensor 2. So, you can try something like:

VL53L4CD sensor_vl53l4cd_sat(&DEV_I2C, 17);
VL53L4CD sensor_vl53l4cd_sat2(&DEV_I2C, 18);

Best Regards, Carlo

Hs-Cheng commented 2 years ago

Hi @cparata ,

Thank you for your reply. I have several follow-up questions (sorry I am not experienced in I2C).

  1. In my setup function above, when should I turn off sensor 2? Before the InitSensor() function of sensor 1 or before the begin() function of sensor 1? Also, if I need to turn off sensor 2 when initializing sensor 1, is it better to separate the setup codes for sensor 1 and sensor 2? Right now the setup codes for both sensors are mixed together.
  2. Is there a way to modify the I2C address by hardware wiring, instead of software coding? This is the datasheet of an accelerometer I used before: https://cdn-learn.adafruit.com/downloads/pdf/adxl343-breakout-learning-guide.pdf
    On page 9 and 10, it says that we can connect 3.3V to the SDO pin to modify the I2C address. Is there a similar function on the VL53L4CD sensor?

Thank you again for your assistance.

All the best, HC

cparata commented 2 years ago

Hello @Hs-Cheng , you need to turn off all the sensors before starting to call the InitSensor() in cascade. So, as you correctly did in the sketch, you must call begin() and VL53L4CD_Off() for all sensors before and then you can call the InitSensor() one by one. Unfortunately, for VL53L4CD component you can only set the I2C address by software. There is not any mechanism to change it by hardware. Anyway, now is your setup working? Can I close this issue? Best Regards, Carlo

Hs-Cheng commented 2 years ago

Hi @cparata ,

My setup is working now. Thank you very much for your help. I am going to close this issue.

All the best, HC

nh1628 commented 1 year ago

@Hs-Cheng I'm currently struggling with getting 2 of the sensors to read together, would it be possible to post your code so I can see how you got it working?

I tried following what you did in the setup to assign the address to the sensors, but I am still only seeing one address, 0x14, when using this in my loop to scan for i2C devices

byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknown error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

Resolved the issue by setting the addresses from 0x29 and 0x28 to 0x51 and 0x28.

Hs-Cheng commented 1 year ago

Hi @nh1628,

I am glad that you solved the problem by modifying the I2C address. Usually the address of each sensor need to be separated by at least 2. Therefore, I guess 0x30 and 0x28 will also work if 0x30 is not occupied by other sensors/ICs.

I wish you all the best with your project. HC