Sensor ToF vl53l0x with TCA9548A switch

Hello,

I am trying to run Time of Flight sensors (VL53L0X) using the Adafruit_VL53L0X library on an ESP32-S3. Starting from the beginning, the ToF sensors are connected via a TCA9548A, which acts as a switch. First, I create the appropriate Wire instance and scan the entire bus. I find the address 0x70, which is the address of the TCA9539 switch, and so far everything works. Next, I select which channel I want to operate on (selecting an example device) and try to initialize the ToF sensor, but I get the following error:`Bus 0 selected successfully.

[   143][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   160][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   166][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   172][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   179][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   185][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   191][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   197][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   204][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   210][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   216][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   222][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   229][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   235][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   241][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   247][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   254][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   260][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   266][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   272][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   279][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   285][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   292][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   299][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   305][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   311][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   328][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   336][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   342][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   348][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   355][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   361][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   368][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   374][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   381][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   387][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   393][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   400][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   406][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   413][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   420][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
[   426][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1
Failed to boot VL53L0X

Here is the code which i am using:

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_VL53L0X.h"
//#include <twi.h>  

//https://forum.pololu.com/t/multiple-vl53l0x-sensor-with-tca9548a-i2c-multiplexer-using-median-filter/15227
#define I2C_MASTER_SCL_IO           5    // GPIO number used for I2C master clock
#define I2C_MASTER_SDA_IO           4    // GPIO number used for I2C master data
#define I2C_MASTER_FREQ_HZ          100000 // I2C master clock frequency
#define TCA9539_ADDR                0x74  // I2C address of the TCA9539PWR
#define TOF_G_PIN                   16
#define TOF_R_PIN                   32
#define TOF_B_PIN                   64


void TCA9548A_Select(uint8_t bus);
void tca9539_init();
void tca9539_write_register(uint8_t reg_addr, uint8_t value);
void set_tof(uint8_t red, uint8_t green, uint8_t blue);
#define NUM_SENSORS 6 
Adafruit_VL53L0X lox = Adafruit_VL53L0X();


//Adafruit_VL53L0X lox = Adafruit_VL53L0X();
//Adafruit_VL53L0X lox[NUM_SENSORS];
void setup(){
  Serial.begin(115200);
  Wire.begin(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO, I2C_MASTER_FREQ_HZ);
  Wire.setPins(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO);
  byte error, address;
  int nDevices;
  tca9539_init();
  set_tof(0,0,0); 

  Serial.println("Scanning...");
  
  nDevices = 0;
  for (address = 1; address < 127; address++) {
    // The i2c_scanner uses the return value of 
    // the Write.endTransmission 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");

  TCA9548A_Select(0);

    if (!lox.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    }else{
      Serial.println(F("VL53L0X booted successfully"));
    }
}

I don't see where you included the TCA9548A library.
#include "TCA9548A.h"

i'm not using tca library, because it is not necessary. Here is my function to swithc beetwen devicesusingtca9548a

void TCA9548A_Select(uint8_t bus) {
  Wire.beginTransmission(0x70);  // TCA9548A address is 0x70
  Wire.write(1 << bus);          // send byte to select bus
  uint8_t error = Wire.endTransmission();
  
  if (error == 0) {
    Serial.print("Bus ");
    Serial.print(bus);
    Serial.println(" selected successfully.");
  } else {
    Serial.print("Error selecting bus ");
    Serial.print(bus);
    Serial.print(": ");
    Serial.println(error);
  }
}

Where are these functions located?

void TCA9548A_Select(uint8_t bus);
void tca9539_init();
void tca9539_write_register(uint8_t reg_addr, uint8_t value);
void set_tof(uint8_t red, uint8_t green, uint8_t blue);
void tca9539_init() {
    // Ustawienie wszystkich pinów jako wyjścia (0x00)
    tca9539_write_register(0x06, 0x00); // Konfiguracja portu 0
    tca9539_write_register(0x07, 0x00); // Konfiguracja portu 1
}
void tca9539_write_register(uint8_t reg_addr, uint8_t value) {
    Wire.begin();
    Wire.beginTransmission(TCA9539_ADDR);
    Wire.write(reg_addr);
    Wire.write(value);
    Wire.endTransmission();
}

void set_tof(uint8_t red, uint8_t green, uint8_t blue) {
    uint8_t tof_state = (red * TOF_R_PIN | green * TOF_G_PIN | blue * TOF_B_PIN);
    tca9539_write_register(0x03, tof_state); // Ustawienie stanu portu 2
}

these functions was prepared by me

I didn't see that in your original code. If you don't post it all, I can't help you.
You only need to call "Wire.begin()" in setup. Why are you calling it again in tca9539_write_register()?

i have wire begin in setup with a proper pins for sda and scl. You are right that wire.begin() i function tca9539_write_register() is not good. but i erased this line and nothing has changed

Could it be your Wire.begin() call in setup isn't working with those parameters?

Post your entire code not just fragments.

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_VL53L0X.h"
 

//https://forum.pololu.com/t/multiple-vl53l0x-sensor-with-tca9548a-i2c-multiplexer-using-median-filter/15227
#define I2C_MASTER_SCL_IO           5    // GPIO number used for I2C master clock
#define I2C_MASTER_SDA_IO           4    // GPIO number used for I2C master data
#define I2C_MASTER_FREQ_HZ          100000 // I2C master clock frequency
#define TCA9539_ADDR                0x74  // I2C address of the TCA9539PWR
#define TOF_G_PIN                   16
#define TOF_R_PIN                   32
#define TOF_B_PIN                   64


void TCA9548A_Select(uint8_t bus);
void tca9539_init();
void I2Cscanner();
void tca9539_write_register(uint8_t reg_addr, uint8_t value);
void set_tof(uint8_t red, uint8_t green, uint8_t blue);
Adafruit_VL53L0X lox = Adafruit_VL53L0X();

 void setup() {
  Serial.begin(115200);
  Wire.begin(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO, I2C_MASTER_FREQ_HZ);
  I2Cscanner();
  tca9539_init();
  set_tof(0,0,0); 

  TCA9548A_Select(0);
   if (!lox.begin()) {
      Serial.println(F("Failed to boot VL53L0X"));
  }else{
      Serial.println(F("VL53L0X booted successfully"));
     }

 }

void loop() {
 

}
void TCA9548A_Select(uint8_t bus) {
  Wire.begin();
  Wire.beginTransmission(0x70);  
  Wire.write(1 << bus);          
  uint8_t error = Wire.endTransmission();
  
  if (error == 0) {
    Serial.print("Bus ");
    Serial.print(bus);
    Serial.println(" selected successfully.");
  } else {
    Serial.print("Error selecting bus ");
    Serial.print(bus);
    Serial.print(": ");
    Serial.println(error);
  }
}

void tca9539_init() {
    tca9539_write_register(0x06, 0x00); 
    tca9539_write_register(0x07, 0x00); 
}
void tca9539_write_register(uint8_t reg_addr, uint8_t value) {
    Wire.begin(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO, I2C_MASTER_FREQ_HZ);
    Wire.beginTransmission(TCA9539_ADDR);
    Wire.write(reg_addr);
    Wire.write(value);
    Wire.endTransmission();
}

void set_tof(uint8_t red, uint8_t green, uint8_t blue) {
    uint8_t tof_state = (red * TOF_R_PIN | green * TOF_G_PIN | blue * TOF_B_PIN);
    tca9539_write_register(0x02, tof_state); 
}
  void I2Cscanner() {
  Serial.println ();
  Serial.println ("I2C scanner. Scanning ...");
  byte count = 0;

  Wire.begin(I2C_MASTER_SDA_IO, I2C_MASTER_SCL_IO, I2C_MASTER_FREQ_HZ);
  for (byte i = 8; i < 120; i++)
  {
    Wire.beginTransmission (i);          
    if (Wire.endTransmission () == 0)  
    {
      Serial.print ("Found address: ");
      Serial.print (i, DEC);
      Serial.print (" (0x");
      Serial.print (i, HEX);     
      Serial.println (")");
      count++;
    }
  }
  Serial.print ("Found ");      
  Serial.print (count, DEC);        
  Serial.println (" device(s).");
}

no, i am sure that thisparameters are correct because i used another device and connect properly with this settings of wire

How many times are you going to call Wire.begin()? How about using the standard i2c pins and call "Wire.begin()" with no parameters? And only once in setup().

now i am calling wire.begin only in setup(), with no parameters it is not working because wire has default pins like 21, 21 or something like this

Some sources say the i2c bus is gpio.8 (SDA) & gpio.9 (SCL).

http://wiki.fluidnc.com/en/hardware/ESP32-S3_Pin_Reference

i use this ESP32-S3-WROOM-1U-N8R8 and i have sda and scl configured as 4 and 5

You need call Wire.begin() once in setup. I see 3 calls now in your latest code.
setup()
I2Cscanner()
tca9539_write_register()

That's all I have.

i erase calls from all function except setup()

I lied. I have one more thing. What i2c address did you say the TCA9548A has? Didn't it scan at 0x70? If so, why do you think it is at 0x74?

#define TCA9539_ADDR                0x74 

// then later:
    Wire.beginTransmission(TCA9539_ADDR);

I have expander for gpio which is on 0x74 adress and switch for I2C at 0x70.
now i have probably connected ToF but still getting this message after connection [284594][E][Wire.cpp:513] requestFrom(): i2cRead returned Error -1 which means that i have problem still with i2c