Can the Wire library recognize when a device isn't respondin

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
ajakef
 
Posts: 35
Joined: Fri Jan 27, 2012 2:35 am

Can the Wire library recognize when a device isn't respondin

Post by ajakef »

I have the Adafruit ADS1115 board connected to my Arduino Uno with I2C. At some point, the ADS1115 stopped working and the sketch crashed.

I'd really like to add a snippet of code to recognize when the I2C device stops responding and report the error to save debugging time. Does anyone know if the Wire library allows this?

Right now, Wire.read() seems to be reporting zeroes when dealing with an unresponsive device. Unfortunately, zero is a very common reading in this application, so that's not enough to diagnose a problem.

Thanks in advance!

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can the Wire library recognize when a device isn't respo

Post by adafruit_support_mike »

Generally speaking, no.. I2C kind of depends on not knowing who else is on the bus.

What you really want to do is watch for the ACK bits that are generated by an I2C slave device on the 9th clock tick after the master transmits a byte. That's the official mechanism for letting the master know that a slave device is listening.

Unfortunately, the Wire library hides most of the signal-level details associated with transmitting data over an I2C bus. It's one of those cases where making things simple means removing information that could be useful in special cicumstances.

The best feedback you'll get is the return value from the Wire.write() command. I had to trace this out on the logic analyzer recently, so I happen to know that Wire aborts a transmission if it doesn't get an ACK after each byte.

The return value of Wire.write() is the number of bytes that were transmitted and ACK'd, so a return value of zero means nobody responded to the command byte.

User avatar
ajakef
 
Posts: 35
Joined: Fri Jan 27, 2012 2:35 am

Re: Can the Wire library recognize when a device isn't respo

Post by ajakef »

I just tested this and Wire.write simply returned the number of bytes sent, not ACKed. It could be connected to no device at all and it would still return one byte sent.

But, Wire.available and Wire.requestFrom did return useful output in my tests.

However, I found that this only worked sometimes. In some cases (with a severely broken device) it simply froze when the Wire.endTransmission command ran. Other people around the internet seem to have encountered this issue too.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can the Wire library recognize when a device isn't respo

Post by adafruit_support_mike »

Hmm.. I thought the sketch I used for testing returned zero. I didn't save the code though.

User avatar
ajakef
 
Posts: 35
Joined: Fri Jan 27, 2012 2:35 am

Re: Can the Wire library recognize when a device isn't respo

Post by ajakef »

Just realized my reply might not have been totally clear: Wire.available and Wire.requestFrom returned the number of bytes the device was providing, which is useful for diagnosing devices that aren't responding.

Thanks for your advice. It's possible that I messed up my code, or the hardware associated with it, and that Wire.write works differently from how it appears. In any case, it was very useful to learn that the I2C device does acknowledge commands from the arduino.

User avatar
adafruit_support_mike
 
Posts: 67485
Joined: Thu Feb 11, 2010 2:51 pm

Re: Can the Wire library recognize when a device isn't respo

Post by adafruit_support_mike »

Oh yeah.. the ACK bit is built into the I2C protocol.

I2C was developed by Philips in the 1980s, and in 2006 the semiconductor business was spun off as a separate business unit now known as NXP. They own the trademark on "I2C" and set the rules for what the protocol does. That's why you see so many devices talking about "TWI" or "2-wire" interfaces.. they're using the same basic ideas but not paying a trademark fee to NXP.

The spec for the protocol is here, and is worth reading:
http://www.nxp.com/documents/user_manual/UM10204.pdf

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”