There is a “bug”/missing non-standard feature in the official Arduino Wire library that caused potential situations where unresponsive slave devices could lock up the I2C bus. In the Wire library, there are some while loops that basically just sit there and wait for the situation to get resolved, but there are some instances where it doesn’t happen, so we currently use a different I2C library called the Arduino I2C Master library, written by Wayne Truchsess. It has some extended functionality and better reliability than Wire, an example of which is the timeout function which releases the SDA/SCL lines and reinitializes the I2C interface in the event of a bus lockup. In our code base, this I2C library is contained in CI2C.h/cpp.
The way you use it is very similar to the wire library.
// Include the library
uint8_t deviceAddress = 0x64;
uint8_t registerAddress = 0xAA;
uint8_t dataByte = 0xFF;
// Non-zero return values are failures. A return value of 0 is success.
uint8_t ret = 0;
// To write a register byte to an I2C device:
ret = I2c.write( deviceAddress, registerAddress );
// To write a data byte to a register on an I2C device:
ret = I2c.write( deviceAddress, registerAddress, dataByte );
// To read a byte from a device:
ret = I2c.read( deviceAddress, registerAddress, 1 );
uint8_t readByte = I2c.receive();
// To read multiple bytes from a device:
uint8_t numBytes = 5;
uint8_t *dataBytes = new uint8_t();
ret = I2c.read( deviceAddress, registerAddress, numBytes, dataBytes );
There are other variations of read() and write() that provide extended functionality in the CI2C class that you can look at as well. Because this library uses the same physical two wire interface as the Wire library, you can’t use both at the same time. Eventually, it would be nice for the Wire library to add support for the timeout and bus recovery functions so we could just use that. I think people have made pull requests for it in the past, but they weren’t pulled in, though I haven’t checked to see if things have changed in the last couple Arduino releases. We are also considering modifying the Wire library ourselves and providing that modified version with the project so people can still use standard Arduino Wire functions. For now, it would be best to use our CI2C class for your lidar sensor to make sure there are no bus lockup issues that freeze up the whole program.
Let me know if you need any additional help; I know it isn’t very documented at the moment.