Wire library

Anyone know any work-around for the problem that wire library will cause a freeze if there is no reply on the I2C bus?

What do mean by reply? I2C defines an ACK or a NAK as the response to transmission. You can't not reply since the line is either HIGH (NAK) or low (ACK).

There are also software implementations of the Wire library that add a timeout feature. Google is your friend.

Pull-up resistors of 4.7k on SDA and SCL normally stop blocking behavior on I2C.


I think that the problem is that the protocol allows a slave to stretch the clock ‘forever’ and your master arduino code will hang forever waiting for the ACK/NACK when that happens.

The reason why the master Arduino thinks that clock stretching is the case can be anything hardware related in any part of the system.


First try the above by @6v6gt.

The I2C master library is possibly what @blh64 is referring to. See also http://www.dsscircuits.com/articles/arduino-i2c-master-library

In this case it is a DAC, so that's why I wrote "reply".
If an error happens, no matter if it is hardware realated, I would like the software not to hang. Is the only way to use an external watchdog to drive the reset signal?

I use 4.7k pull-up external resistors. They are required by I2C. (Internal pull-ups are not reliable as they can differ a lot in resistance).

I2C can even tolerate attempts to contact missing devices, assuming the pullup resistors are not on the missing device. That is how the I2C scanner can test the entire address space looking for attached devices.

What it can't tolerate is a rogue device holding say the clock line low and therefore blocking the bus. What would you want to do in such a circumstance, electrically cut the rogue device loose and carry on with what remains ?

In this case I think it is either indirect ESD causing an error in the I2C transmission or a voltage dip, causing the I2C transmission to fail. It does not happen very often, but it does sometimes. It seems other people have the same problem with I2C freezes (for other reasons) Arduinio: If I2C crashes, Wire library can not recover (does not try to reconnect) · Issue #236 · OpenAgricultureFoundation/openag_brain · GitHub

No matter what the cause for the I2C transmission error is, error handling would be good practice, like a timeout.

So I guess the only workaround would be an external watchdog?

If you want to employ such a work around, the internal watchdog timer should be enough to handle a freeze. In AVR systems (at least) it is separate enough and has its own oscillator.
In principle, you set it up with a timeout. Every so often, and within the timeout period, you reset it (say every X loop iterations). If there is a freeze, the reset operation can't proceed and at the end of the timeout, the watchdog timer forces a restart.

Obviously, though, it is better to eliminate the root cause of the freeze.