I can’t seem to use VL53L0X with the multiplexer

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
zackous
 
Posts: 46
Joined: Sat Aug 20, 2016 11:59 pm

I can’t seem to use VL53L0X with the multiplexer

Post by zackous »

I keep getting an error when I try to use two VL53L0X sensors with the multiplexer. In the multiplexer sample code I need to label them separately as seen here in italics.

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
 
#define TCAADDR 0x70
 
[i]/* Assign a unique ID to this sensor at the same time */
Adafruit_HMC5883_Unified mag1 = Adafruit_HMC5883_Unified(1);
Adafruit_HMC5883_Unified mag2 = Adafruit_HMC5883_Unified(2);[/i]
 
void displaySensorDetails(Adafruit_HMC5883_Unified *mag)
{
  sensor_t sensor;
  mag->getSensor(&sensor);
  Serial.println("------------------------------------");
  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
  Serial.println("------------------------------------");
  Serial.println("");
  delay(500);
}
 
void tcaselect(uint8_t i) {
  if (i > 7) return;
 
  Wire.beginTransmission(TCAADDR);
  Wire.write(1 << i);
  Wire.endTransmission();  
}
 
 
void setup(void) 
{
  Serial.begin(9600);
  Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
  
  /* Initialise the 1st sensor */
  tcaselect(2);
  if(!mag1.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Initialise the 2nd sensor */
  tcaselect(6);
  if(!mag2.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
  
  /* Display some basic information on this sensor */
  tcaselect(2);
  displaySensorDetails(&mag1);
  tcaselect(6);
  displaySensorDetails(&mag2);
}
 
void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  
  tcaselect(2);
  mag1.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("Sensor #1 - ");
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  
  tcaselect(6);
  mag2.getEvent(&event);
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  Serial.print("Sensor #2 - ");
  Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print("  ");
  Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print("  ");
  Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print("  ");Serial.println("uT");
  
  delay(500);
}
I tried the same thing but it isn’t working.

Code: Select all

/* Assign a unique ID to this sensor at the same time */
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X(1);
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X(2);
Can someone please help me.

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: I can’t seem to use VL53L0X with the multiplexer

Post by adafruit_support_carter »

That HMC5883 example uses the Unified Sensor Driver:
https://learn.adafruit.com/using-the-ad ... troduction
Not all sensors use that framework. The VL53L0X does not. So just do this:

Code: Select all

Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();
You'll want to call begin() for each one, but you'll need to come up with a different version of displaySensorDetails().

User avatar
zackous
 
Posts: 46
Joined: Sat Aug 20, 2016 11:59 pm

Re: I can’t seem to use VL53L0X with the multiplexer

Post by zackous »

Thank you for your quick response! I’ll make the changes later today. The rest of my code with no changes looks like this.

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_VL53L0X.h"

#define TCAADDR 0x70

/* Assign a unique ID to this sensor at the same time */
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X(1);
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X(2);

//void displaySensorDetails(Adafruit_HMC5883_Unified *mag)
//{
//  sensor_t sensor;
//  mag->getSensor(&sensor);
//  Serial.println("------------------------------------");
//  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
//  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
//  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
//  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
//  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
//  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
//  Serial.println("------------------------------------");
//  Serial.println("");
//  delay(500);
//}

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


void setup(void) 
{
  Serial.begin(115200);
  for (uint8_t t=0; t<8; t++) {//identifying all sensors
      tcaselect(t);
      Serial.print("TCA Port #"); Serial.println(t);
 
      for (uint8_t addr = 0; addr<=127; addr++) {
        if (addr == TCAADDR) continue;
      
        uint8_t data;
//        if (! twi_writeTo(addr, &data, 0, 1, 1)) {
        uint8_t result = Wire.requestFrom(addr, 1);
//           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
        if ( Wire.requestFrom(addr, 1))
        {
           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);

        }
      }
    }
    Serial.println("\ndone");//identifying all sensors
    
  /* Initialise the 1st sensor */
  tcaselect(1);
//  while (! Serial) {
//    delay(1);
//  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox1.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
  
  /* Initialise the 2nd sensor */
//  tcaselect(2);
//  while (! Serial) {
//    delay(1);
//  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox2.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
  
  /* Display some basic information on this sensor */
//  tcaselect(2);
//  displaySensorDetails(&mag1);
//  tcaselect(6);
//  displaySensorDetails(&mag2);
}

void loop(void) 
{
  /* Get a new sensor event */ 
  sensors_event_t event; 
  
  tcaselect(1);
  lox1.getEvent(&event);
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
  
  tcaselect(2);
  lox2.getEvent(&event);
  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
  
  delay(500);
}

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: I can’t seem to use VL53L0X with the multiplexer

Post by adafruit_support_carter »

Code: Select all

  /* Initialise the 2nd sensor */
//  tcaselect(2);
Looks like you have your select commented out for the second sensor.

Since the VL53L0X doesn't use the unified sensor, this won't work:

Code: Select all

  lox1.getEvent(&event);
You'll need to access the readings as shown in the example:
https://github.com/adafruit/Adafruit_VL ... 0x.ino#L27

User avatar
zackous
 
Posts: 46
Joined: Sat Aug 20, 2016 11:59 pm

Re: I can’t seem to use VL53L0X with the multiplexer

Post by zackous »

Is this better? I haven't had a chance to test it but the IDE says there are no errors.

Code: Select all

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_VL53L0X.h"

#define TCAADDR 0x70

/* Assign a unique ID to this sensor at the same time */
Adafruit_VL53L0X lox1 = Adafruit_VL53L0X();
Adafruit_VL53L0X lox2 = Adafruit_VL53L0X();

//void displaySensorDetails(Adafruit_HMC5883_Unified *mag)
//{
//  sensor_t sensor;
//  mag->getSensor(&sensor);
//  Serial.println("------------------------------------");
//  Serial.print  ("Sensor:       "); Serial.println(sensor.name);
//  Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
//  Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
//  Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" uT");
//  Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" uT");
//  Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" uT");  
//  Serial.println("------------------------------------");
//  Serial.println("");
//  delay(500);
//}

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


void setup(void) 
{
  Serial.begin(115200);
  for (uint8_t t=0; t<8; t++) {//identifying all sensors
      tcaselect(t);
      Serial.print("TCA Port #"); Serial.println(t);
 
      for (uint8_t addr = 0; addr<=127; addr++) {
        if (addr == TCAADDR) continue;
      
        uint8_t data;
//        if (! twi_writeTo(addr, &data, 0, 1, 1)) {
        uint8_t result = Wire.requestFrom(addr, 1);
//           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);
        if ( Wire.requestFrom(addr, 1))
        {
           Serial.print("Found I2C 0x");  Serial.println(addr,HEX);

        }
      }
    }
    Serial.println("\ndone");//identifying all sensors
    
  /* Initialise the 1st sensor */
  tcaselect(1);
//  while (! Serial) {
//    delay(1);
//  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox1.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
  
  /* Initialise the 2nd sensor */
  tcaselect(2);
//  while (! Serial) {
//    delay(1);
//  }
  
  Serial.println("Adafruit VL53L0X test");
  if (!lox2.begin()) {
    Serial.println(F("Failed to boot VL53L0X"));
    while(1);
  }
  // power 
  Serial.println(F("VL53L0X API Simple Ranging example\n\n"));
  
  /* Display some basic information on this sensor */
//  tcaselect(2);
//  displaySensorDetails(&mag1);
//  tcaselect(6);
//  displaySensorDetails(&mag2);
}

void loop(void) 
{
  VL53L0X_RangingMeasurementData_t measure;
  /* Get a new sensor event */ 
  sensors_event_t event; 
  
  tcaselect(1);
//  lox1.getEvent(&event);
//  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox1.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
  
  tcaselect(2);
//  lox2.getEvent(&event);
//  VL53L0X_RangingMeasurementData_t measure;
    
  Serial.print("Reading a measurement... ");
  lox2.rangingTest(&measure, false); // pass in 'true' to get debug data printout!

  if (measure.RangeStatus != 4) {  // phase failures have incorrect data
    Serial.print("Distance (mm): "); Serial.println(measure.RangeMilliMeter);
  } else {
    Serial.println(" out of range ");
  }
  
  delay(500);
}

User avatar
adafruit_support_carter
 
Posts: 29168
Joined: Tue Nov 29, 2016 2:45 pm

Re: I can’t seem to use VL53L0X with the multiplexer

Post by adafruit_support_carter »

Try that and see what happens.

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”