There is a known issue where I2C library would hang the board when there's an issue communicating with a slave device.
That's because Wire.endTransmission()
does not support timeouts, it will wait indefinitely which hangs the board.
Solution is to use an alternate library called I2C. In case this site ever goes down, a copy of the most recent version of this library is available here.
Fortunately, it's easy to convert code that used Wire, to use I2C library. There's an demo code located in the same ZIP file as a library itself, which contains a sample Wire-based code and its equivalent with I2C lib.