Trouble ESP32 wroom with Vl53L0x and multiplexer PCA9548A

Hi,
I'm trying to carry out the project of using an esp32 dev kit that communicates with the PCA9548A multiplexer via I2C and can read 2 VL53L0x sensors ( same addrees 0x29).
I have found several codes on the web, but none of them work or they don't read the addresses on the multiplexer ports.

Could someone from the community help me with some basic code that allows me to read the 2 VL53L0x?

Thank you in advance for your valuable help.

Are you using the VL53L0x chip or a board that contains the VL53L0x chip.

If it is a board then can you please tell us what sort of board because that matters as to the answer.

shows you the code for this.

Hi Grumpy_Mike,

First of all, thank you for your attention.

Let me explain:
I am doing a basic project in which I show 3 measurements from 3 sensors (VL53L0X) using I2C.
I bought the VL53L0X board which has the default address (0x29).

I am using the ESP32 DEVKITV1 micro.
Initially I did it for a single sensor, I used a generic code and it worked very well.

I was looking into getting 2 measurements using 2 VL53L0X with the same controller and I found that I should use a multiplexer.

I bought the PCA5948A board, which can connect up to 8 devices. It has the default address (0x70). I have used some examples on the internet but none of them work for me.

For this reason I was asking for some code that could help me.

What board? The default address is always 0x29 because that part has a single address.

Do you know that this processor only produces 3V3 signals and in theory that is not enough voltage to drive a multiplexer of a 5V powered device.

So before you can consider any code you have to make sure that your hardware is correct.

So we need to see how you have wired things up. The best way for that is to use a schematic showing how everything is wired up, that includes power supplies and grounds. Hand drawn is fine, photograph it and post it in the correct orientation to how it is drawn. That means get the landscape / portrait correct.

Please do not post diagrams with pictures connected together with wires, of Fritzing physical layout diagrams, they are less than helpful especial with greater than four components.

As I told you at the beginning, I had already tried it initially with a single sensor (without multiplexer) and it worked very well, which means that I had to have previously read the datasheet to make the connections.
The micro comes as ESP32 DEV KIT V1 which is the same esp32-wroom-32

I attach the link. Esp32 Wroom 32 Pins

My question is:
According to what I can understand from the PCA9548A datasheet, it tells me that it has A0, A1 and A2, which through binary selection I can select the port.
However, when I put A0= 0V, A1= 0V and A2= 0v, it should select port 0 and read the VL53L0x that I have connected to that port, but it does not.
Could you tell me if I am doing the right thing or am I misinterpreting how to do it?

no way!, with A0 to A2 you can select the I2C address of the PCA9548A, if you want to select a port you do that by writing the port number with the I2C protocol....

so for I2C address 0X70 you put A0 to A2 to ground.

void TCA9548A(uint8_t bus)
{
  Wire.beginTransmission(0x70);        //   I2C address of the TCA9548A
  Wire.write(1 << bus);                           //   send byte to select bus
  Wire.endTransmission();
  }

We can only tell you this if you post a schematic, like I requested.

Thank for the schematic. I can tell now why it will not work.
All the lines connected to the PCA9548 are connected to ground ground, and that means that the address that the PCA9548 uses is always the same.

In order to change what address the PCA9548 uses you must be able to set these address lines differently for each address. So the A0, A1 and A2 must be connected to three output signals on your ESP chip. These output signals will then control which one of eight possible channels that the PCA9548 will use. This allows the SD1 and SC1 to be used along with the other SDx and SCx where x is 1 to 7, to be able to output signals.

The PCA should be powered from the 3.3volt pin, NOT from Vin.
Likely the same for the (unspecified) sensors.
Leo..

But I don't understand what you're telling me.
I checked the datasheet and enabled port 1:
A0=1, A1=0, A2=0. So it should read port 1 and it doesn't.

I'm using this code and It doesn't show me any sensor readings.

#include "Wire.h"
#include "DFRobot_VL53L0X.h"
#include <LiquidCrystal_I2C.h>
#include <Adafruit_Sensor.h>

#define LED_1 2
#define LED_2 4
#define LED_3 15

LiquidCrystal_I2C lcd(0x27, 20, 4); // declaration display
DFRobot_VL53L0X sensor;
float x1; // variable sensor 1
float x2; // variable sensor 2

void TCA9548A(uint8_t bus)
{
Wire.beginTransmission(0x70);
Wire.write(1 << bus);
Wire.endTransmission();
}

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

pinMode(LED_1, OUTPUT);
pinMode(LED_2, OUTPUT);
pinMode(LED_3, OUTPUT);

Serial.begin(115200);
TCA9548A(0);

sensor.begin(0x29); // sensor #1
sensor.setMode(sensor.eContinuous, sensor.eHigh); //Set to Back-to-back mode and high precision mode
sensor.start(); //Laser rangefinder begins to work

TCA9548A(1);
sensor.begin(0x29); //sensor #2
sensor.setMode(sensor.eContinuous, sensor.eHigh); //Set to Back-to-back mode and high precision mode
sensor.start(); //Laser rangefinder begins to work

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

}

void loop() {

TCA9548A(0); // call sensor #1 and Get the distance

        digitalWrite(LED_1, HIGH);
        digitalWrite(LED_2, LOW);
        digitalWrite(LED_3, LOW);     // turn the LED off by making the voltage LOW
        x1 = sensor.getDistance() ;
        Serial.println("Axes X mm: ");
        Serial.println(x1);
  

TCA9548A(1);  // call sensor #2 and Get the distance
       
        digitalWrite(LED_1, LOW);
        digitalWrite(LED_2, HIGH);
        digitalWrite(LED_3, LOW);     // turn the LED off by making the voltage LOW
       x2 = sensor.getDistance()  ;

        Serial.println("Axes Y mm: ");
        Serial.println(x2);  

delay(1000);

}

You misunderstand stood what the data sheet says. With A0=1, A1=0, A2=0 then it will only allow the SD0 and SC0 to be used, none of the other multiplexed channels.

You might want to look at this How to get the best out of this forum before you proceed any further.

It tells you how to post code on this forum correctly. So please apply this to your post#13.

Thanks. I solved it with or without the multiplexer.

Great stuff well done.

For people with the same problem can you please say how you solved the problem.

Thanks