VL53L1X Can't change address

Hello,

I bought two VL53L1X sensors from Pololu and I want to use them on the same I2C bus on an Arduino Uno. I downloaded the library and used the example to test both sensor separately and it worked fine.
The problem I face is when I try to change the address of the sensor (one sensor connected). The output I get on the Serial Monitor is “Failed to detect and initialize sensor!”. If I then change the address back to 41 then it works normally again. I used the example code and just added a line to change the sensor address.

/*
This example shows how to take simple range measurements with the VL53L1X. The
range readings are in units of mm.
*/

#include <Wire.h>
#include <VL53L1X.h>

VL53L1X sensor;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);

  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }
  
  // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
  // You can change these settings to adjust the performance of the sensor, but
  // the minimum timing budget is 20 ms for short distance mode and 33 ms for
  // medium and long distance modes. See the VL53L1X datasheet for more
  // information on range and timing limits.
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);

  // Start continuous readings at a rate of one measurement every 50 ms (the
  // inter-measurement period). This period should be at least as long as the
  // timing budget.
  sensor.startContinuous(50);

  sensor.setAddress(42);
}

void loop()
{
  Serial.print(sensor.getAddress());
  Serial.print('\t');
  Serial.print(sensor.read());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}

Hopefully someone can help me with this.

Thanks in advance.

Hi, fbrad.

It’s likely that the issue is that you successfully ran your program once to change the sensor’s address to 42, then reset your Arduino and tried to run the program again (or loaded a new program onto the Arduino) while the sensor was still powered. sensor.init() then tries to initialize the sensor with the default address, but since the sensor’s address was already changed and not reset, the program fails to find the VL53L1X at the default address. I expect that your program would work fine as-is if you reset your entire system, including the sensors, by disconnecting and reconnecting power; can you confirm this?

Unfortunately, moving sensor.setAddress() before sensor.init() does not fully solve the problem either because init() resets the sensor, which includes resetting the address back to the default value. If you need this program to work after a microcontroller reset while the sensor remains powered, the best solution is to connect the sensor’s XSHUT line to your Arduino and have the Arduino toggle that line to reset the sensor at the beginning of your program.

Kevin

Hi Kevin,

Thank you for your reply. What I didn’t know is that every time you open up the Serial Monitor the whole code will be executed again (even the setup function) so that was the reason why it was not able to initialize the sensor.

I followed your instructions and have connected the XSHUT pin to the Arduino and toggled it before the init() method and now everything works fine.

Here is the code I used if someone is interested:

/*
This example shows how to take simple range measurements with the VL53L1X. The
range readings are in units of mm.
*/

#include <Wire.h>
#include <VL53L1X.h>

#define XSHUT 2

VL53L1X sensor;

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(400000); // use 400 kHz I2C

  sensor.setTimeout(500);

  // Toggle XSHUT to reset the sensor
  pinMode(XSHUT, OUTPUT);
  pinMode(XSHUT, INPUT);

  if (!sensor.init())
  {
    Serial.println("Failed to detect and initialize sensor!");
    while (1);
  }
  
  // Use long distance mode and allow up to 50000 us (50 ms) for a measurement.
  // You can change these settings to adjust the performance of the sensor, but
  // the minimum timing budget is 20 ms for short distance mode and 33 ms for
  // medium and long distance modes. See the VL53L1X datasheet for more
  // information on range and timing limits.
  sensor.setDistanceMode(VL53L1X::Long);
  sensor.setMeasurementTimingBudget(50000);

  // Start continuous readings at a rate of one measurement every 50 ms (the
  // inter-measurement period). This period should be at least as long as the
  // timing budget.
  sensor.startContinuous(50);

  sensor.setAddress(42);
}

void loop()
{
  Serial.print(sensor.getAddress());
  Serial.print('\t');
  Serial.print(sensor.read());
  if (sensor.timeoutOccurred()) { Serial.print(" TIMEOUT"); }

  Serial.println();
}

Thanks again for your help!

1 Like