I’m using two VL53L0X distance sensors with an arduino nano 33 IoT, which seem to work just great when using the adafruit library and the DUAL example provided by the library. I noticed that when i disconnected the USB cable from the computer and reconnect it, the sensors would stop working.
I ran an I2C scanner to see what was going on. I noticed that after the two sensors succesfully worked, the adresses saved we’re indeed the ones i specified for each sensors: 0x30, 0x31. After disconnecting the USB cable and reconnect it, the only address that was detected was 0x29, the default address for the VL53L0X sensor. After this, i reuploaded the code that was perfectly working with the sensors just moments ago, and noticed that the message “Sensor 2 failed to boot” popped up in the serial monitor. After running an I2C adress scanner i noticed that the only adress that was saved was that of the first sensor, 0x30. The “solution” for this problem (i.e for the sensors to work again) i need to swap the adresses, that and reupload the code again. My goal is for the sensors to always work, even after disconnecting and reconnecting the USB cable. I understand that upon disconnecting the USB cable, the adresses are set to default, but in my code i specify which address each sensor needs to have. Any ideas of how it is possible to mantain the addresses even after reconnecting the USB cable? Or is it not possible at all?
This is the code used (It’s fairly similar or if not, identical to the DUAL example from the adafruit library addresed in this post).
#include "Adafruit_VL53L0X.h"
// address we will assign if dual sensor is present
#define LOX1_ADDRESS 0x31
#define LOX2_ADDRESS 0x30
// set the pins to shutdown
#define SHT_LOX1 A1
#define SHT_LOX2 A3
// objects for the vl53l0x
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
// this holds the measurement
VL53L0X_RangingMeasurementData_t measure1;
VL53L0X_RangingMeasurementData_t measure2;
void setID() {
// all reset
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
delay(100);
// all unreset
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, HIGH);
delay(100);
// activating LOX1 and resetting LOX2
digitalWrite(SHT_LOX1, HIGH);
digitalWrite(SHT_LOX2, LOW);
delay(100);
// initing LOX1
if(!lox1.begin(LOX1_ADDRESS)) {
Serial.println(F("Failed to boot first VL53L0X"));
while(1);
}
delay(100);
// activating LOX2
// digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, HIGH);
delay(100);
// initing LOX2
if(!lox2.begin(LOX2_ADDRESS)) {
Serial.println(F("Failed to boot second VL53L0X"));
while(1);
}
// digitalWrite(SHT_LOX1, HIGH);
}
void read_dual_sensors() {
lox1.rangingTest(&measure1, false); // pass in 'true' to get debug data printout!
lox2.rangingTest(&measure2, false); // pass in 'true' to get debug data printout!
// print sensor one reading
Serial.print(F("1: "));
if(measure1.RangeStatus != 4) { // if not out of range
Serial.print(measure1.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
Serial.print(F(" "));
// print sensor two reading
Serial.print(F("2: "));
if(measure2.RangeStatus != 4) {
Serial.print(measure2.RangeMilliMeter);
} else {
Serial.print(F("Out of range"));
}
Serial.println();
}
void setup() {
Serial.begin(115200);
// wait until serial port opens for native USB devices
while (! Serial) { delay(1); }
pinMode(SHT_LOX1, OUTPUT);
pinMode(SHT_LOX2, OUTPUT);
Serial.println(F("Shutdown pins inited..."));
digitalWrite(SHT_LOX1, LOW);
digitalWrite(SHT_LOX2, LOW);
Serial.println(F("Both in reset mode...(pins are low)"));
Serial.println(F("Starting..."));
setID();
}
void loop() {
read_dual_sensors();
delay(100);
}
Diego Garza Pérez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.