Hi All,
I have been trying all day to get this working. I can See the 0x70 address of the mux with an I2C scanner code running on the esp32. I added print statements to the mux code and it prints 1 and 2 but not 3 so the conclusion is it hangs on “Wire.requestFrom”. It does this for enable and disable mux port calls. I can talk directly to my ToF sensors if I exclude the MUX so it is some combination of the ESP32 and the MUX. I have tried 100k and 400k i2c freq. I have read of a lot of problems but none seem like mine. I have tried a different mux and same behavior (both were new in packaging earlier today). Any feedback would be helpful. Maybe someone can just try and run my sketch to see if they can enable ports on the MUX?
#include "Adafruit_VL53L1X.h"
#define IRQ_PIN 2
#define XSHUT_PIN 3
#define MUX_ADDR 0x70 //7-bit unshifted default I2C Address
Adafruit_VL53L1X vl53 = Adafruit_VL53L1X(XSHUT_PIN, IRQ_PIN);
//Enables a specific port number
void enableMuxPort(byte portNumber)
{
if (portNumber > 7) portNumber = 7;
Serial.println("1");
Wire.beginTransmission(MUX_ADDR);
Serial.println("2");
//Read the current mux settings
delay(10);
Wire.requestFrom(MUX_ADDR, 1);
Serial.println("3");
if (!Wire.available()) return; //Error
byte settings = Wire.read();
Serial.println("4");
//Set the wanted bit to enable the port
settings |= (1 << portNumber);
Serial.println("5");
Wire.write(settings);
Serial.println("6");
Wire.endTransmission();
Serial.println("7");
}
//Disables a specific port number
void disableMuxPort(byte portNumber)
{
if (portNumber > 7) portNumber = 7;
Serial.println("1");
Wire.beginTransmission(MUX_ADDR);
Serial.println("2");
//Read the current mux settings
Wire.requestFrom(MUX_ADDR, 1);
Serial.println("3");
if (!Wire.available()) return; //Error
Serial.println("4");
byte settings = Wire.read();
Serial.println("5");
//Clear the wanted bit to disable the port
settings &= ~(1 << portNumber);
Serial.println("6");
Wire.write(settings);
Serial.println("7");
Wire.endTransmission();
Serial.println("8");
}
void setup() {
Serial.begin(115200);
while (!Serial) delay(10);
Serial.println(F("Adafruit VL53L1X sensor demo"));
Wire.begin();
Wire.setClock(400000); // tried at 100 and 400
/*disableMuxPort(0);
disableMuxPort(1);
disableMuxPort(2);
disableMuxPort(3);
disableMuxPort(4);
disableMuxPort(5);
disableMuxPort(6);
disableMuxPort(7);*/
enableMuxPort(0);
/*enableMuxPort(1);
enableMuxPort(2);
enableMuxPort(3);
enableMuxPort(4);
enableMuxPort(5);
enableMuxPort(6);
enableMuxPort(7);*/
if (! vl53.begin(0x29, &Wire)) {
Serial.print(F("Error on init of VL sensor: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("VL53L1X sensor OK!"));
Serial.print(F("Sensor ID: 0x"));
Serial.println(vl53.sensorID(), HEX);
if (! vl53.startRanging()) {
Serial.print(F("Couldn't start ranging: "));
Serial.println(vl53.vl_status);
while (1) delay(10);
}
Serial.println(F("Ranging started"));
// Valid timing budgets: 15, 20, 33, 50, 100, 200 and 500ms!
vl53.setTimingBudget(50);
Serial.print(F("Timing budget (ms): "));
Serial.println(vl53.getTimingBudget());
/*
vl.VL53L1X_SetDistanceThreshold(100, 300, 3, 1);
vl.VL53L1X_SetInterruptPolarity(0);
*/
}
void loop() {
int16_t distance;
if (vl53.dataReady()) {
// new measurement for the taking!
distance = vl53.distance();
if (distance == -1) {
// something went wrong!
Serial.print(F("Couldn't get distance: "));
Serial.println(vl53.vl_status);
return;
}
Serial.print(F("Distance: "));
Serial.print(distance);
Serial.println(" mm");
// data is read out, time for another reading!
vl53.clearInterrupt();
}
}