I am trying to connect 2 VL53L7CX sensors to Arduino Portenta Lite H7 board.
I am using Pololu - VL53L7CX Time-of-Flight 8×8-Zone Wide FOV Distance Sensor Carrier with Voltage Regulator, 350cm Max
I am able to run the sensors individually but cannot run both together. I am changing the I2C address using set_I2C_address function. The status returned for I2C_set_address is 2, but if I toggle power to sensor and reset the module the status changes to 0.
#include <Arduino.h>
#include <Wire.h>
#include <vl53l7cx_class.h>
#define DEV_I2C Wire
#define sensor1_address ((uint16_t)0x52) // default
#define sensor2_address ((uint16_t)0x54)
byte i2c_rcv;
#define LPN_PIN_1 PC_13 // GPIO 0
#define LPN_PIN_2 PC_15 // GPIO 1
#define I2C_RST_PIN_1 PD_4 // GPIO 2
#define I2C_RST_PIN_2 PD_5 // GPIO 3
void print_result(VL53L7CX_ResultsData *Result);
// Components.
VL53L7CX sensor1(&DEV_I2C, LPN_PIN_1, I2C_RST_PIN_1);
VL53L7CX sensor2(&DEV_I2C, LPN_PIN_2, I2C_RST_PIN_2);
uint8_t res = VL53L7CX_RESOLUTION_4X4;
uint8_t ranging_mode = VL53L7CX_RANGING_MODE_CONTINUOUS;
char report[256];
uint8_t status1, status2;
void setup() {
Serial.begin(115200);
DEV_I2C.setClock(400000);
// Initialize I2C bus.
DEV_I2C.begin();
pinMode(LPN_PIN_1, OUTPUT);
pinMode(LPN_PIN_2, OUTPUT);
//Changing I2C address of sensor 2 to 0x54
digitalWrite(LPN_PIN_1, LOW);
digitalWrite(LPN_PIN_2, HIGH);
sensor2.vl53l7cx_set_i2c_address(sensor2_address);
delay(10);
// Initializing sensor 2
sensor2.vl53l7cx_init();
sensor2.vl53l7cx_set_ranging_mode(ranging_mode);
sensor2.vl53l7cx_set_sharpener_percent(5);
sensor2.vl53l7cx_set_ranging_frequency_hz(10);
delay(10);
// Initializing sensor 1
digitalWrite(LPN_PIN_1, HIGH);
digitalWrite(LPN_PIN_2, LOW);
sensor1.vl53l7cx_init();
sensor1.vl53l7cx_set_ranging_mode(ranging_mode);
sensor1.vl53l7cx_set_sharpener_percent(5);
sensor1.vl53l7cx_set_ranging_frequency_hz(10);
delay(10);
// Activating both sensors
digitalWrite(LPN_PIN_1, HIGH);
digitalWrite(LPN_PIN_2, HIGH);
delay(50);
sensor1.vl53l7cx_start_ranging();
sensor2.vl53l7cx_start_ranging();
}
void loop()
{
VL53L7CX_ResultsData Results;
uint8_t NewDataReady = 0;
uint8_t status;
do {
status1 = sensor1.vl53l7cx_check_data_ready(&NewDataReady);
status2 = sensor2.vl53l7cx_check_data_ready(&NewDataReady);
} while (!NewDataReady);
if ((!status1) && (NewDataReady != 0)) {
status = sensor1.vl53l7cx_get_ranging_data(&Results);
print_result(&Results);
}
else if ((!status2) && (NewDataReady != 0)) {
status = sensor2.vl53l7cx_get_ranging_data(&Results);
print_result(&Results);
}
}
void print_result(VL53L7CX_ResultsData *Result)
{
int8_t j, k, l;
uint8_t zones_per_line;
uint8_t number_of_zones = res;
zones_per_line = (number_of_zones == 16) ? 4 : 8;
for (j = 0; j < number_of_zones; j += zones_per_line)
{
for (l = 0; l < VL53L7CX_NB_TARGET_PER_ZONE; l++)
{
// Print distance and status
for (k = (zones_per_line - 1); k >= 0; k--)
{
if ((Result->nb_target_detected[j+k]>0) && (Result->target_status[(VL53L7CX_NB_TARGET_PER_ZONE * (j+k)) + l] ==5))
{
Serial.print(Result->distance_mm[(VL53L7CX_NB_TARGET_PER_ZONE * (j+k)) + l]);
Serial.print(":");
}
else
{
Serial.print(0);
Serial.print(":");
}
}
}
}
}
