I2C - testing availability ???

Is there a way to pre-check if I2C connected devices are physically there ??

I have 2 mega's connected via I2C and they are quite happy talking to each other like "2 woman over the back fence" - no offence is intended..haha

If one is a little slower to initialize on power-up my status messages still indicates the partner is available and will attempt to send data to it causing some sort of hang !!!

Is there a library call similar to [ if (Serial) ] which can be used for the I2C bus devices in Wire.h etc ??

Regards Colin

Hi Colin

I don't believe there is a built-in function (though I'm happy to be corrected) but you could borrow the technique used in the I2C scanner programs (like this one: https://github.com/RobTillaart/Arduino/blob/master/sketches/MultiSpeedI2CScanner/MultiSpeedI2CScanner.ino which is explained here: http://forum.arduino.cc/index.php?topic=197360.0).

At the start of your program on the I2C master, periodically call Wire.beginTransmission() and Wire.endTransmission() without sending any data. If the I2C slave is active, it will respond to this "ping" and Wire.endTransmission() will return 0. If it does not respond correctly, the function will return a value greater than 0.

//  Assumes you have done Wire.begin() previously
Wire.beginTransmission(0x42);  // Put in correct 7-bit I2C address for your slave
boolean deviceFound = (Wire.endTransmission() == 0);

This assumes that your slave program is ready to go as soon as it as done its Wire.begin(xxx).

Regards

Ray