I updated the code and implemented this statements for a better overview:
#include <Wire.h>
#include <LIDARLite.h>
LIDARLite lidar_left;
LIDARLite lidar_front;
LIDARLite lidar_right;
const int sensorLeft_EN = 8;
const int sensorFront_EN = 9;
const int sensorRight_EN = 10;
#define UNIT_ID_HIGH 0x16 // Serial number high byte
#define UNIT_ID_LOW 0x17 // Serial number low byte
#define I2C_ID_HIGH 0x18 // Write serial number high byte for I2C address unlock
#define I2C_ID_LOW 0x19 // Write serial number low byte for I2C address unlock
#define I2C_SEC_ADDR 0x1a // Write new I2c address after unlock
#define I2C_CONFIG 0x1e // Default address response control
#define LIDAR_STD_ADDRESS 0x62 // Standard I2C-address for all sensors
#define LIDAR_LEFT_ADDRESS 0x41 // New I2C-address right
#define LIDAR_FRONT_ADDRESS 0x42 // New I2C-address front
#define LIDAR_RIGHT_ADDRESS 0x43 // New I2C-address left
void setup() {
Serial.begin(115200); // Initialize serial connection
// Initialize Wire library
Wire.begin();
// define the enable Pin as Output
pinMode(sensorLeft_EN, OUTPUT);
pinMode(sensorFront_EN, OUTPUT);
pinMode(sensorRight_EN, OUTPUT);
// Turn off all sensors
digitalWrite(sensorLeft_EN, LOW);
digitalWrite(sensorFront_EN, LOW);
digitalWrite(sensorRight_EN, LOW);
// To change the I2C address, the unique serial number of the unit must be read and then
// written back to the device before setting the new address. The process is as follows:
// Turn on the right sensor
digitalWrite(sensorRight_EN, HIGH);
Serial.println("Right sensor turned on");
// Step 1: Read the two byte serial number from 0x96 (high byte 0x16 and low byte 0x17)
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(0x96);
Wire.requestFrom(LIDAR_STD_ADDRESS, 2); // 2 Bytes lesen, wie in der Anleitung angegeben
byte highByte_Right = Wire.read();
byte lowByte_Right = Wire.read();
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 1 right failed");
// Step 2. Write the serial number high byte to 0x18
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_ID_HIGH);
Wire.write(UNIT_ID_HIGH);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 2 right failed");
// Step 3: Write the serial number low byte to 0x19
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_ID_LOW);
Wire.write(UNIT_ID_LOW);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 3 right failed");
// Step 4: Write the desired new I2C address to 0x1a
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_SEC_ADDR);
Wire.write(LIDAR_RIGHT_ADDRESS);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 4 right failed");
// Step 5: Write 0x08 to 01e to disable the default address
Wire.beginTransmission(LIDAR_RIGHT_ADDRESS);
Wire.write(I2C_CONFIG);
Wire.write(0x08);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 5 right failed");
// Print change message in serial monitor
Serial.println("Address of right sensor changed");
// Turn on front sensor (standard address)
digitalWrite(sensorFront_EN, HIGH);
Serial.println("Front sensor turned on");
// Step 1: Read the two byte serial number from 0x96 (high byte 0x16 and low byte 0x17)
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(0x96);
Wire.requestFrom(LIDAR_STD_ADDRESS, 2); // 2 Bytes lesen, wie in der Anleitung angegeben
byte highByte_Front = Wire.read();
byte lowByte_Front = Wire.read();
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 1 front failed");
// Step 2. Write the serial number high byte to 0x18
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_ID_HIGH);
Wire.write(UNIT_ID_HIGH);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 2 front failed");
// Step 3: Write the serial number low byte to 0x19
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_ID_LOW);
Wire.write(UNIT_ID_LOW);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 3 front failed");
// Step 4: Write the desired new I2C address to 0x1a
Wire.beginTransmission(LIDAR_STD_ADDRESS);
Wire.write(I2C_SEC_ADDR);
Wire.write(LIDAR_FRONT_ADDRESS);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 4 front failed");
// Step 5: Write 0x08 to 01e to disable the default address
Wire.beginTransmission(LIDAR_FRONT_ADDRESS);
Wire.write(I2C_CONFIG);
Wire.write(0x08);
Wire.endTransmission();
if (!Wire.endTransmission()) Serial.println("Step 5 front failed");
// Print change message in serial monitor
Serial.println("Address of right sensor changed");
// Configuration of the sensors
lidar_right.begin(0, true); // Set configuration to default and I2C to 400 kHz
lidar_right.configure(0); // Standard configuration
lidar_front.begin(0, true);
lidar_front.configure(0);
Serial.println("Ready!");
delay(2000);
}
void loop() {
// Measure the distance and print it in the serial monitor
Serial.print(lidar_right.distance());
Serial.print(",");
Serial.println(lidar_front.distance());
delay(1000);
}
My serial monitor prints the error messages. Meaning all steps except 5 failed and the transmission of the message didn't work.
Right sensor turned on
Step 1 right failed
Step 2 right failed
Step 3 right failed
Step 4 right failed
Address of right sensor changed
Front sensor turned on
Step 1 front failed
Step 2 front failed
Step 3 front failed
Step 4 front failed
Address of right sensor changed
Ready!
I think I followed the correct procedure:
- Turn off all sensors
- Turn on one sensor and change its address
- Turn on the next sensor and change its address
I wrote the seperate steps in the code for a better overview, ChatGPT then corrected it so every step has its own beginTransmission and endTransmission. I think shouldn't be a problem and helps for a better overview of the single steps. Can you see what's wrong?