I have 2 Lidars which are Lidar Lite v3 and Lidar Lite v4, both of these sensor having the same i2c address. I'm trying to change my Lidar Lite v3's i2c address on arduino mega2560 using a function from LidarLite library from 0x62 to 0x63. However it doesn't seem to work, after I uploaded a code the serial monitor display
> nack
and when I'm using i2c scanner it still show a default address (0x62).
here is my arduino code
#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;
void setup(){
Serial.begin(115200); // Initialize serial connection to display distance readings
char newAddress = 0x63;
myLidarLite.setI2Caddr(newAddress , true);
Wire.begin();
}
void loop(){
**** i2c scanner code ****
}
setI2Caddr function from library
void LIDARLite::setI2Caddr(char newAddress, char disableDefault, char lidarliteAddress)
{
byte dataBytes[2];
// Read UNIT_ID serial number bytes and write them into I2C_ID byte locations
read ((0x16 | 0x80), 2, dataBytes, false, lidarliteAddress);
write(0x18, dataBytes[0], lidarliteAddress);
write(0x19, dataBytes[1], lidarliteAddress);
// Write the new I2C device address to registers
dataBytes[0] = newAddress;
write(0x1a, dataBytes[0], lidarliteAddress);
// Enable the new I2C device address using the default I2C device address
dataBytes[0] = 0;
write(0x1e, dataBytes[0], lidarliteAddress);
// If desired, disable default I2C device address (using the new I2C device address)
if (disableDefault)
{
dataBytes[0] = (1 << 3); // set bit to disable default address
write(0x1e, dataBytes[0], newAddress);
}
}
I've checked a read function and found this condition
// A nack means the device is not responding, report the error over serial
int nackCatcher = Wire.endTransmission();
if(nackCatcher != 0)
{
Serial.println("> nack");
}
the datasheet of Lidar Lite v3 is here and changing i2c address topic can be found in 5th page
I've tried to compare the function to a datasheet but still have no idea why it didn't work. So, how can I change an I2C address of Lidar Lite v3 to make them able to work together?
Update
Finally I have found the mistake. The simple way to fix is just call function Wire.begin()
or myLidarLite.begin()
(which containing Wire.begin
inside the function) before using setI2Caddr()
.
So, the code will be like this
#include <Wire.h>
#include <LIDARLite.h>
LIDARLite myLidarLite;
void setup(){
Serial.begin(115200); // Initialize serial connection to display distance readings
Wire.begin(); // or myLidarLite.begin();
char newAddress = 0x63;
myLidarLite.setI2Caddr(newAddress , true);
}
void loop(){
**** i2c scanner code ****
}
After upload to the board, the address is finally changed.
Ps. Btw, when I set 0x64 to setI2Caddr
it changed to 0x63 instead of 0x64 and still keep the old address eventhough I set disabledefault as true.