Trouble using Two lidar v3HP sensors

Hello everyone,

I'm working on a project we use two Lidar Garmin v3HP sensors to read the respective distances so we can process them into a file.

We haven't been able to read from any of the sensors if they are wired at the same time in the I2C bus (They are in the same address 0x62). We tried switching addreses to no avail and now we are trying to just turn the sensors on and of when we need to take distances, even this is not working as expected and just gives the data from 1 sensor or doesn't give anything

if anyone could help me solve this issue

#include <Arduino.h>
#include <Wire.h>
#include <stdint.h>
#include <LIDARLite_v3HP.h>

#define POWER_ENABLE_S1 12
#define POWER_ENABLE_S2 11

LIDARLite_v3HP Sensor;

void setup(){
    Serial.begin(9600);
    pinMode(POWER_ENABLE_S1,OUTPUT);
    pinMode(POWER_ENABLE_S2,OUTPUT);
    Wire.begin()
    Sensor.configure(0);
}

void measure(){
    int s1;
    int s2;
    digitalWrite(POWER_ENABLE_S1,HIGH);
    digitalWrite(POWER_ENABLE_S2,LOW);
    s1=Sensor.readDistance();
    delay(25);
    digitalWrite(POWER_ENABLE_S1,LOW);
    digitalWrite(POWER_ENABLE_S2,HIGH);
    s2=Sensor.readDistance();
    Serial.println("Sensor 1: "+String(s1)+" -- Sensor 2: "+String(s2));
}

void loop(){
    measure();
}

Just in case here is the Manual for the sensor

Did you see this part in the manual? "The I2C address will be restored to its default value after you
cycle the power."

Yes, we are that I2C addresses returnb to default after a powe cycle, we used to switch the address on setup, but even with that and rewiring the sensors we get no reading or reading from only 1 sensor. both sensors are working fine if we use them separately

Do you have I2C pull-up resistors on both sensors, one sensor, or neither?

At the moment we don't have the pull up resistors connected to the SDA/SCL lines of the sensors

I see that the example sketch reads a distance like this:

    // 1. Wait for busyFlag to indicate device is idle. This must be
    //    done before triggering a range measurement.
    myLidarLite.waitForBusy();

    // 2. Trigger range measurement.
    myLidarLite.takeRange();

    // 3. Wait for busyFlag to indicate device is idle. This should be
    //    done before reading the distance data that was triggered above.
    myLidarLite.waitForBusy();

    // 4. Read new distance data from device registers
    *distance = myLidarLite.readDistance();

Your sketch is not doing any of the waiting loops.

I'll implement those waiting loops on the sketch and check if that solves the issue

Have a bit of a update, we implemented the following code and the sensors are detected correctlya nd configured to the addresses 0x42 and 0x62 (the sensor in this address is resisting to the changes).

anyways the first sensor measures correctky but the second one measure is off or not giving the correct info, also when we blind one of the sensor (it seems to be the master sensor) both of them go to 0

#include <Arduino.h>
#include <Wire.h>
#include <stdint.h>
#include <LIDARLite_v3HP.h>

#define POWER_ENABLE_S1 12
#define POWER_ENABLE_S2 11
#define DEFAULT_ADDRESS 98

#define NUMERO_LIDARS 2

LIDARLite_v3HP Sensor1;
LIDARLite_v3HP Sensor2;


int detectedAddreses[NUMERO_LIDARS];
int currentAdd;
int deviceCount = 0;
int i = 0;

void scanI2C()
{
    int nDevices = 0;
    while (i < NUMERO_LIDARS)
    {
        for (byte addr = 1; addr < 127; ++addr)
        {
            Wire.beginTransmission(addr);
            byte error = Wire.endTransmission();
            if (error == 0)
            {
                Serial.print("Se encontro un dispositivo en 0x");
                if (addr < 16)
                {
                    Serial.print("0");
                }
                Serial.print(addr, HEX);
                Serial.println("  !");
                ++nDevices;
                if (addr == DEFAULT_ADDRESS)
                {
                    configSensors(i, 66 + deviceCount, addr);
                    i++;
                }
                i++;
            }
            else if (error == 4)
            {
                Serial.print("Error desconocido en 0x");
                if (addr < 16)
                {
                    Serial.print("0");
                }
                Serial.println(addr, HEX);
            }
        }
        if (nDevices == 0)
        {
            Serial.println("No se encontraron dispositivos\n");
        }
        else
        {
            Serial.println("Terminado\n");
        }
    }
}

void configSensors(int sensor, uint8_t new_address, uint8_t old_address)
{
    switch (sensor)
    {
    case 0:
        Sensor1.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S1, LOW);
        detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor1.configure(0,new_address);
        break;

    case 1:
        Sensor2.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S2, LOW);
        detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor2.configure(0,new_address);
        i = 999;
        break;

    case 2:
        break;
    }
}

void setup()
{
    Serial.begin(9600);
    pinMode(POWER_ENABLE_S1, OUTPUT);
    pinMode(POWER_ENABLE_S2, OUTPUT);
    digitalWrite(POWER_ENABLE_S1, HIGH);
    digitalWrite(POWER_ENABLE_S2, HIGH);
    Wire.begin();
    scanI2C();
    digitalWrite(POWER_ENABLE_S1,HIGH);
    digitalWrite(POWER_ENABLE_S2,HIGH);
    Sensor1.configure(3,66);
    Sensor2.configure(3,98);
}

void measure(){
  float s1;
  float s2;

  Sensor1.waitForBusy();
  Sensor1.takeRange();
  Sensor1.waitForBusy();
  s1 = Sensor1.readDistance(66);
  Sensor2.waitForBusy();
  Sensor2.takeRange();
  Sensor2.waitForBusy();
  s2 = Sensor2.readDistance(98);
  Serial.println("Sensor 1: " + String(s1) + "; Sensor 2: " + String(s2));
  }

void loop()
{
  measure();
  
}


Is that supposed to be "66 + nDevices" or "66 + i"? You don't seem to be incrementing 'deviceCount' at all.

And why increment 'i' twice for the default address?? That means address 62 will be counted as two devices.

Are you saving detected addresses in your array of detected addresses? You don't seem to be.

deviceCount is incremented after it's configured in the configSensors void, same for the array list which wasn't working properly inside that function so i moved it to the scanI2C function and now is working as intended, it's doing it while inside de configuration function.

The double increment seems to be our mistake, i'll reflect that in the code right now, and also added the fasti2c method that the library has been using in the examples

#include <Arduino.h>
#include <Wire.h>
#include <stdint.h>
#include <LIDARLite_v3HP.h>
#include <I2CFunctions.h>

#define POWER_ENABLE_S1 12
#define POWER_ENABLE_S2 11
#define DEFAULT_ADDRESS 98
#define FAST_I2C
#define NUMERO_LIDARS 2

LIDARLite_v3HP Sensor1;
LIDARLite_v3HP Sensor2;


int detectedAddreses[NUMERO_LIDARS];
int currentAdd;
int deviceCount = 0;
int i = 0;

void scanI2C()
{
    int nDevices = 0;
    while (i < NUMERO_LIDARS)
    {
        for (byte addr = 1; addr < 127; ++addr)
        {
            Wire.beginTransmission(addr);
            byte error = Wire.endTransmission();
            if (error == 0)
            {
                Serial.print("Se encontro un dispositivo en ");                
                Serial.print(addr);
                Serial.println("  !");
                ++nDevices;
                detectedAddreses[i] = addr;
                if (addr == DEFAULT_ADDRESS)
                {
                    configSensors(i, 66 + deviceCount, addr);
                    detectedAddreses[i] = addr;
                    i++;
                }else{
                  detectedAddreses[i] = addr;
                  i++;
                }
            }
            else if (error == 4)
            {
                Serial.print("Error desconocido en ");
                Serial.println(addr);
            }
        }
        if (nDevices == 0)
        {
            Serial.println("No se encontraron dispositivos\n");
        }
        else
        {
            Serial.println("Terminado\n");
        }
    }
}

void configSensors(int sensor, uint8_t new_address, uint8_t old_address)
{
    switch (sensor)
    {
    case 0:
        Sensor1.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S1, LOW);
        //detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor1.configure(0,new_address);
        break;

    case 1:
        Sensor2.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S2, LOW);
        //detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor2.configure(0,new_address);
        i = 999;
        break;

    case 2:
        break;
    }
}

void setup()
{
    Serial.begin(115200);
    #ifdef FAST_I2C
        #if ARDUINO >= 157
            Wire.setClock(400000UL); // Set I2C frequency to 400kHz (for Arduino Due)
        #else
            TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
        #endif
    #endif
    pinMode(POWER_ENABLE_S1, OUTPUT);
    pinMode(POWER_ENABLE_S2, OUTPUT);
    digitalWrite(POWER_ENABLE_S1, HIGH);
    digitalWrite(POWER_ENABLE_S2, HIGH);
    Wire.begin();
    scanI2C();
    digitalWrite(POWER_ENABLE_S1,HIGH);
    digitalWrite(POWER_ENABLE_S2,HIGH);
    Sensor1.configure(3,detectedAddreses[0]);
    Sensor2.configure(3,detectedAddreses[1]);
}

void measure(){
  float s1;
  float s2;  
  Sensor1.waitForBusy();
  Sensor1.takeRange();
  Sensor1.waitForBusy();
  s1 = Sensor1.readDistance(detectedAddreses[0]);  
  Sensor2.waitForBusy();
  Sensor2.takeRange();
  Sensor2.waitForBusy();
  s2 = Sensor2.readDistance(detectedAddreses[1]);
  Serial.println("Sensor 1: " + String(s1) + "; Sensor 2: " + String(s2));
  }

void loop()
{
 /*Serial.println(detectedAddreses[0]);
 Serial.println(detectedAddreses[1]);*/
measure();
  
}

Both sensors are "working" to an extent, if you blind the label master sensor both of them go to zero, and the distance that we are getting is completly wrong in one of the sensors which apears to be the slave in this case

We did some more research and tried to hook up just the VCC of the sensors to an external power suply (External battery 11.1V with a 5V regulator), also some more tweaking to the code itself. This ended up working. Here is the code that worked in our case

#include <Arduino.h>
#include <Wire.h>
#include <stdint.h>
#include <LIDARLite_v3HP.h>
#include <I2CFunctions.h>

#define POWER_ENABLE_S1 12
#define POWER_ENABLE_S2 11
#define DEFAULT_ADDRESS 98
#define FAST_I2C
#define NUMERO_LIDARS 2

LIDARLite_v3HP Sensor1;
LIDARLite_v3HP Sensor2;


int detectedAddreses[NUMERO_LIDARS];
int currentAdd;
int deviceCount = 0;
int i = 0;

void scanI2C()
{
    int nDevices = 0;
    while (i < NUMERO_LIDARS)
    {
        for (byte addr = 1; addr < 127; ++addr)
        {
            Wire.beginTransmission(addr);
            byte error = Wire.endTransmission();
            if (error == 0)
            {
                Serial.print("Se encontro un dispositivo en ");                
                Serial.print(addr);
                Serial.println("  !");
                ++nDevices;
                detectedAddreses[i] = addr;
                if (addr == DEFAULT_ADDRESS)
                {
                    configSensors(i, 66 + deviceCount, addr);
                    detectedAddreses[i] = addr;
                    i++;
                }else{
                  detectedAddreses[i] = addr;
                  i++;
                }
            }
            else if (error == 4)
            {
                Serial.print("Error desconocido en ");
                Serial.println(addr);
            }
        }
        if (nDevices == 0)
        {
            Serial.println("No se encontraron dispositivos\n");
        }
        else
        {
            Serial.println("Terminado\n");
        }
    }
}

void configSensors(int sensor, uint8_t new_address, uint8_t old_address)
{
    switch (sensor)
    {
    case 0:
        Sensor1.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S1, LOW);
        //detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor1.configure(0,new_address);
        break;

    case 1:
        Sensor2.setI2Caddr(new_address, 0, old_address);
        digitalWrite(POWER_ENABLE_S2, LOW);
        //detectedAddreses[sensor] = new_address;
        deviceCount++;
        Sensor2.configure(0,new_address);
        i = 999;
        break;

    case 2:
        break;
    }
}

void setup()
{
    Serial.begin(115200);
    #ifdef FAST_I2C
        #if ARDUINO >= 157
            Wire.setClock(400000UL); // Set I2C frequency to 400kHz (for Arduino Due)
        #else
            TWBR = ((F_CPU / 400000UL) - 16) / 2; // Set I2C frequency to 400kHz
        #endif
    #endif
    pinMode(POWER_ENABLE_S1, OUTPUT);
    pinMode(POWER_ENABLE_S2, OUTPUT);
    digitalWrite(POWER_ENABLE_S1, HIGH);
    digitalWrite(POWER_ENABLE_S2, HIGH);
    Wire.begin();
    scanI2C();
    digitalWrite(POWER_ENABLE_S1,HIGH);
    digitalWrite(POWER_ENABLE_S2,HIGH);
    Sensor1.configure(3,detectedAddreses[0]);
    Sensor2.configure(3,detectedAddreses[1]);
}

void measure(){
  float s1;
  float s2;  
  digitalWrite(POWER_ENABLE_S1,HIGH);
  digitalWrite(POWER_ENABLE_S2,LOW);
  delay(25);
  Sensor1.waitForBusy();
  Sensor1.takeRange();
  Sensor1.waitForBusy();
  s1 = Sensor1.readDistance(detectedAddreses[0]);
  digitalWrite(POWER_ENABLE_S1,LOW);
  digitalWrite(POWER_ENABLE_S2,HIGH);  
  delay(25);
  Sensor2.waitForBusy();
  Sensor2.takeRange();
  Sensor2.waitForBusy();
  s2 = Sensor2.readDistance(detectedAddreses[1]);
  Serial.println("Sensor 1: " + String(s1) + "; Sensor 2: " + String(s2));
  }

void loop()
{
 /*Serial.println(detectedAddreses[0]);
 Serial.println(detectedAddreses[1]);*/
measure();
  
}```