Yet Another I2C Hanging Issue

Hi all,

Last year I attended a robotics competition with our code loaded onto an arduino mega. We had been programming it from a lab machine at our university using version 20 or 21. Everything was working fine for us. Once we got to our event, I tried uploading our code from my laptop running 22 and now it always freezes at wire.endtransmission. I've tried it from 3 other computers (all Windows 7 machines), 3 other arduinos (newer mega, dualaminove and uno) and have tried every I2C sensor I have, and it always hangs. I've tried it with and without a logic level converter/pull up resistors, as well as several different versions of the arduino IDE. I believe I've attempted every fix that is out there on google, which there are tons of results for this issue, unless I'm missing something obvious.

Also, I was looking for the compiled libraries for wire and I can't find a twi.o or wire.o anywhere on my computer, despite some people recommending recompiling them.

tldr; I've been at this issue since May last year. Basically, the arduino hangs whenever it gets to wire.endtransmission.

Thanks in advance

Can you show your code? there might be some easy fix?

Have you checked - Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino - ? it contains all you ever wanted to know about I2C but were afraid to ask :wink:
More seriously, it tells about the right pull ups and it has an I2C scanner which is a very handy debugging tool.

I had found that previously when searching for an I2C scanner, which also hangs. Basically, I've tried tons of code floating around online. These were 2 I2C scanners and then code from tutorials/sparkfun for my particular devices. I will give that page an actual read tonight and see if there's anything I'm doing wrong. I also have an aeroquad shield that has the same problems.

I had a few minutes before leaving for makerfaire. I tried his master/slave code with two arduinos, but threw in some serial.printlns to debug, it still hangs on wire.endtransmission.

Wire functions have a return value which is seldom checked. Can add a few vars to print these return values and post them?

Another option is to download Arduino version 21 and see if the failure exists there too?

That's the thing though is that it never returns. Same result on versions 20 and 21 as well. I appreciate all the help.

Try running your sketch with nothing wired to the data and clock pins. My real time clock sketch doesn’t hang even with nothing wired to the I2C pins, it just returns 0 values for all data. If your sketch doesn’t hang in that state then I would think that you have to look at the I2C device and wiring rather then the code.

Lefty

With nothing connected it still hangs.

Damorian:
With nothing connected it still hangs.

Then I think it's time to post your code. Hopefully something paired down to minimum size required that exhibits/demonstrates the hanging symptom.

Lefty

And your circuit. :slight_smile:

The inbuilt I2C library depends on certain interrupts happening when they should. Otherwise they go into an infinite loop.

One approach is to build a timeout into the library, the other is to fix the problem with the circuit.

Another approach again is to build in a watchdog timer to recover. But if this is happening all the time, the circuit is likely to blame.

Damorian:
I had a few minutes before leaving for makerfaire. I tried his master/slave code with two arduinos, but threw in some serial.printlns to debug, it still hangs on wire.endtransmission.

They might be causing the problem, those debugging displays. Please post your code.

Damorian:
With nothing connected it still hangs.

Try the library I wrote for I2C here (dsscircuits.com is for sale | HugeDomains) it greatly reduces the use of interrupts in the code.

I can link to the code, since it happens with anything using endtransmission.
There's the aeroquad project found here: http://aeroquad.com/ This project has a special shield that worked great, until I uploaded code from another machine.

On this page, mentioned above, there's a link. He showed how to wire two arduinos together along with master/slave code. That had the same result.

For the following I've tried a variety of ways of hooking them up. With/without pull up resistors, with/without logic level converter etc. I believe the correct way to do it is with a logical level converter with the SDA and SCL lines going through the two TX since they're bidirectional. This is the specific one I'm using: http://www.sparkfun.com/products/8745
I have 3.3v going to the Low voltage, 5v going to the high voltage, 3.3v to my device again with the SDA and SCL lines going from device -> low voltage side of logic level converter then to the arduino. Grounds going into the arduino from the device/converter. This is merely my debug setup to make the aeroquad project work. I could sketch it if you'd like.

Code I've tried:
Mentioned above, that I2C scanner.

This scanner: I2CScanner: Arduino as I2C bus scanner – todbot blog

This magnetometer code/library which doesn't use wire at all: HMC5843 Magnetometer Library for Arduino | eclecticc

This gyro code/setup: ITG-3200 Hookup Guide - learn.sparkfun.com

There were others, but these are the main ones I'm testing with now.

As for this library, I'll check it out. The only thing, though, is that if it isn't working for the magnetometer library, I don't want to blame the software yet. It could be a bad setup on my part.

Damorian:
With nothing connected it still hangs.

Does the I2CScanner from Todbot also hang when nothing is connected? If it does then there may be something wrong with your Arduino.

I have tried two arduinos and they both hang right after the "starting scanning of the i2c bus from 1 to 100...". Again I appreciate all the help.

Damorian:
I have tried two arduinos and they both hang right after the "starting scanning of the i2c bus from 1 to 100...". Again I appreciate all the help.

That would concern me because with nothing but the USB cable connected to the Arduino the scanner should not hang.

Damorian:
I have tried two arduinos and they both hang right after the “starting scanning of the i2c bus from 1 to 100…”. Again I appreciate all the help.

Can you take a photo please? Show both Arduinos and the way they are wired together. Also state exactly which scanner you are using which hangs.

You misunderstand, I was not trying the scanner with two arduinos attached (although I think I’ll try that), I’m trying them independently. However, because of this and a few other reasons, we have decided to change platforms, although I’d still like to figure out what’s going on.

So you uploaded the scanner to a Arduino Mega, with nothing at all connected except the USB port, so you could see the serial port output?

I tried, on my Arduino Mega 2560, with the scanner I wrote on this page:

After about a second it displayed this (on the serial monitor):

I2C scanner. Scanning ...
Done.
Found 0 device(s).

If you can't reproduce those results there is something seriously wrong. I wouldn't change platforms just because of that, there seem to be more underlying problems.

Yes, your scanner hangs as well, with nothing hooked to the mega with the exception of the usb cable. It simply stops here:

I2C scanner. Scanning ...

I would like to think it's something wrong with how the wire library is compiled, but honestly I don't have much to go on.