Multiple VL53L0x sensor with TCA9548A I2C Multiplexer using median filter

hello
I am making a simple gauge which measures the Horizontal and Vertical distance using two VL53L0X with TCA9548A I2C multiplexer to gather information simultaneously.
in single mode, I’ve found some accuracy issue so I’ve used MedianFilter to get a stable reading.
but as per my sketch which is posted below, I am getting the same values for horizontal and vertical as my only one sensor is working at a time.

#include <Wire.h>
#include <VL53L0X.h>
#include <MedianFilter.h>


VL53L0X sensor;

#define TCAADDR 0x70

MedianFilter test(10, 0);

extern "C" 
{
   #include "utility/twi.h"  
}

//#define LONG_RANGE



//#define HIGH_SPEED

#define HIGH_ACCURACY

int Distance1,Distance2;

void tcaselect(uint8_t i) 

{
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}

void setup()

{
  Serial.begin(9600);
  Wire.begin();

  sensor.init();
  sensor.setTimeout(500);

  

#if defined LONG_RANGE
  // lower the return signal rate limit (default is 0.25 MCPS)
  sensor.setSignalRateLimit(0.1);
  // increase laser pulse periods (defaults are 14 and 10 PCLKs)
  sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
  sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
#endif

#if defined HIGH_SPEED
  // reduce timing budget to 20 ms (default is about 33 ms)
  sensor.setMeasurementTimingBudget(20000);
#elif defined HIGH_ACCURACY
  // increase timing budget to 200 ms
  sensor.setMeasurementTimingBudget(200000);
#endif


}

void loop()

{
  
    
  tcaselect(1);
  
    int val = sensor.readRangeSingleMillimeters();
    test.in(val);
    val=test.out();
    Distance1=val;
  
  
  
  tcaselect(4);
  
   int val = sensor.readRangeSingleMillimeters();
   test.in(val);
   val=test.out();
   Distance2=val;
  


    Serial.print("Horizontal:");
    Serial.println(Distance1);
	
    Serial.print("Vertical:  ");
    Serial.println(Distance2);

    


    delay(1000);
  
}

Hello, Vijay.

Are you using our VL53L0x carrier board? We have the XSHUT pin broken out to allow the sensor to be used without any kind of multiplexer if you have a spare GPIO pin on your microcontroller. You can read more about that in this thread on our forum:

We do not sell the TCA9548A board, but it sounds like it just switches the sensor that is connected to the I2C bus. If you have multiple sensors, you will still need to initialize each one separately. You should probably use multiple VL53L0X objects like that example I pointed you to. The main difference would probably be that you won’t change the address of the sensors and will need to switch to the appropriate channel on the TCA9548A board before trying any communication.

-Nathan

Thanks Nathan that helped me a lot.
I’ve used 2 sensors but outputI’m getting noise.
Pololu
Suggest me something to reduce noise.

thanks.

Are you using our carrier boards? You might look at this thread for more information about how to reduce noise:

-Nathan

Hi Nathan…
I just want to know which mode gives more accurate results Single or Continuous mode and what is the difference?

Are you using our carrier board?

-Nathan

yes

The accuracy should not be affected by the ranging operating mode. You can find a description of the ranging operating modes in section 2.4 of the VL53L0X datasheet, which we link to on the “Resources” tab of our carrier board’s product page.

-Nathan