I can’t seem to use VL53L0X with the multiplexer
General project help for Adafruit customers
Please be positive and constructive with your questions and comments.
zackous
Posts: 46 Joined: Sat Aug 20, 2016 11:59 pm
Post
by zackous » Thu Feb 15, 2018 1:40 pm
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.
zackous
Posts: 46 Joined: Sat Aug 20, 2016 11:59 pm
Post
by zackous » Thu Feb 15, 2018 3:09 pm
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);
}
zackous
Posts: 46 Joined: Sat Aug 20, 2016 11:59 pm
Post
by zackous » Sat Feb 17, 2018 3:47 am
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);
}
Please be positive and constructive with your questions and comments.