Finding out if something is connected to i2c?

I'm using i2c to have two Arduinos talk to each other using the Wiring library, and would like to determine whether they're connected at all, and display an error message if they aren't.

My problem is that currently Wire.endTransmission() hangs the master when there is no connection. Is there any way to determine whether anything is connected on the i2c bus, or to make endTransmission() timeout or make the call non-blocking?

Thanks a lot in advance, Björn

I can't see anything in the TWI code that would hang the master (maybe someone else can?) Are you sure it hangs when there's no connection and doesn't hang when there is?

Did you call Wire.begin()?

Maybe a small sample size, but I have a removable I2C DS-1307 real time clock module and if I remove it and run my sketch it does NOT hang, just returns all highs I think.


I'm talking to a Devantech MD22 motor controller, and my code (on an Arduino Mega as i2c master) looks like this:

Serial.println("1"); Wire.beginTransmission(MOTOR_I2CADDRESS); Serial.println("2"); Wire.send(adr); Serial.println("3"); Wire.endTransmission(); Serial.println("4");

When the MD22 has power, this works fine; when it is unpowered, this prints "1\n2\n3\n", then hangs... since this project is a fairly large robot, I'd really like the main controller to never ever hang.

Can anyone enlighten me as to what might cause that hang? I looked at the Wire source, but it's fairly involved.

Thanks a lot in advance.

I'm wondering if clock stretching is going on:

If the powered-down motor controller is forcing the clock line low, the master (Arduino Mega) might be sitting there forever waiting for it to go high.

Just a theory...

-- Check out our new shield: