VL53L1X Lidar i2c address change

Hi, I want to use 3 VL53L1X lidar sensors so need to change the address of two of them, has anyone else done this?

I have found some code for arduino used for setting the i2c addresses , is it as simple as using that code and then they will share the i2c bus without issues?

I’m assuming that I only need to change two of the three sensors addresses and can leave the third on the default address right?

Thanks, Steve.

Here’s the arduino code I have found:

#include <VL53L1X.h> //Download this library from https://github.com/pololu/vl53l1x-arduino
#include <Wire.h>

VL53L1X sensor_A; //Create the sensor object
VL53L1X sensor_B; //Create the sensor object

int startTime = millis(); //used for our timing loop
int mInterval = 100; //refresh rate of 10hz

#define XSHUT_A 9
#define XSHUT_B 10

void setup() {

    Serial.begin(57600);
    
    Wire.begin(); //Setup your I2C interface
    Wire.setClock(400000); // use 400 kHz I2C

    //Set the pin mode to output
    pinMode(XSHUT_A ,OUTPUT);
    pinMode(XSHUT_B ,OUTPUT);
    
    //Turn all TOF's off
    digitalWrite(XSHUT_A, LOW);
    digitalWrite(XSHUT_B, LOW);

    //-----------------------------------------------------------------
    //FIRST WE WILL CONFIGURE AND SETUP SENSOR_A
    //-----------------------------------------------------------------
    delay(50);
    digitalWrite(XSHUT_A, HIGH); //Turn sensor_A on
    delay(50);
    
    sensor_A.setTimeout(500); //Set the sensors timeout
    
    if (!sensor_A.init())//try to initilise the sensor
    {
        //Sensor does not respond within the timeout time
        Serial.println("Sensor_A is not responding, check your wiring");
    }
    else
    {
        sensor_A.setAddress(42); //Set the sensors I2C address
        sensor_A.setDistanceMode(VL53L1X::Long); //Set the sensor to maximum range of 4 meters
        sensor_A.setMeasurementTimingBudget(40000); //Set its timing budget in microseconds longer timing budgets will give more accurate measurements
        sensor_A.startContinuous(45); //Sets the interval where a measurement can be requested in milliseconds
    }   

    //-----------------------------------------------------------------
    //NOW CONFIGURE AND SETUP SENSOR_B
    //-----------------------------------------------------------------
    delay(50);
    digitalWrite(XSHUT_B, HIGH); //Turn sensor_A on
    delay(50);
    
    sensor_B.setTimeout(500); //Set the sensors timeout
    
    if (!sensor_B.init())//try to initilise the sensor
    {
        //Sensor does not respond within the timeout time
        Serial.println("Sensor_A is not responding, check your wiring");
    }
    else
    {
        sensor_B.setAddress(43); //Set the sensors I2C address
        sensor_B.setDistanceMode(VL53L1X::Long); //Set the sensor to maximum range of 4 meters
        sensor_B.setMeasurementTimingBudget(40000); //Set its timing budget in microseconds longer timing budgets will give more accurate measurements
        sensor_B.startContinuous(45); //Sets the interval where a measurement can be requested in milliseconds
    }  
}

void loop() {
    //We have to be careful here. If we request a measurement before the measurement has been taken your
    //code will be blovked until the measurement is complete. In order to stop this from happening we
    //must ensure that time between measurement requests is greater than the timing budget and the argument 
    //given in the startContinuous() function. In our case our measurement time must be greater than 50mS.

    if((millis()- startTime) > mInterval)
    {
        Serial.print("Sensor_A Reading: ");
        Serial.println(sensor_A.read()); //Get a reading in millimeters
        Serial.print("Sensor_B Reading: ");
        Serial.println(sensor_B.read()); //Get a reading in millimeters
        startTime = millis();
    }
}

Hello,

Yes this is similar to what I did with the original POC

That’s great, thanks for sharing you experience with the sensors on your project, I’ll attempt to flash an arduino to set the addresses tomorrow ,:slight_smile:

1 Like

Put the code on an arduino and ran it, I looked in serial monitor and the arduino was seeing the two sensors independently of each other and reporting values for each but after disconnecting them from the arduino and connecting to the flight controller I still can’t get them to work :frowning:

It looks like the address change was temporary and they reverted back to the default address after a power cycle as I can get any one sensor to be recognised as rangefinder 1 bit all data stops when I try and connect two or three sensors .

There is no internal memory to save the adress on these devices. The Arduino is setting the adress and then it is scanning the sensors and tranfert the distances to the FC using the mavlink distance message on Serial

That sucks and eliminates these sensors on this project then, I need to connect them via the FC’s i2c bus and not through an arduino, I thought the address could he changed and that was that :frowning:

In that case you can use TFmini S in I2C mode as you can program the adress, as I did with the second version of the POC