I2C bus seems to be hanging.

I'm not sure if this is the right section, as I really don't know if this is an i2c problem or an electrical one. I'll try to explain. I have a project the started out with a single i2c OLED display, so I didn't mess with pull-ups, as everything seemed to work fine. As the project grew, I added a data logger, which consisted of a second Mega with a datalogger shield, communicating with the main processor via i2c. At this point, I added a pair of 4.7k pullups. Again, everything worked fine. Then I added a compass chip, also i2c. Once more, all works as it should....

Now, here is the stinker, the data logger is no longer needed, but if I remove it, nothing works. The system hangs either during setup() or almost immediately after, and seldom in the same place in the sketch. I know this because there are a number of checks and status reports posted to the display, and the system hangs randomly during this process.

What I have done so far is try different pullups. I recently noticed the OLED display has a pair of 4.7K pullups on it, in addition to the pair I added, so I removed the ones I added. At that point, it wouldn't start at all. Next, I removed them from the OLED board, and added 2.7K pullups to the main circuit board. Again, nothing. Currently, it has only the 2, 4.7K pullups on the main circuit board, and the datalogger, compass, and OLED all connected, and it runs fine.

Obviously, I am missing something, but am at somewhat of a loss as to how to proceed. It is obvious that the data logger adds something to the circuit that is essential, but what? I tried connecting just a bare mega, but it requires the data logger attached for the system to work.

I am happy to provide any other necessary details, but don't know what is necessary at this point. Thanks in advance....

Post a wiring diagram and links to all used hardware!

The Mega has on-board pullups (10k) for the I2C bus.

How long are the wires of your complete I2C bus?

pylon:
Post a wiring diagram and links to all used hardware!

The Mega has on-board pullups (10k) for the I2C bus.

How long are the wires of your complete I2C bus?

Thanks for the quick reply, and I will do that, but first, for anyone reading this (and apologies for off-topic), but obviously I need a oscilloscope; what do you folks recommend that won't break the bank?

Would this scope be ok for reading the SCL and SDA lines?

Update: I replaced the Wire library with the WSWire library that adds timers to the TWI while loops, and after some finagling around (the Arduino IDE hides and scatters libraries all over the OS, it seems, LOL) I got it to compile and, while not solving the underlying problem, it now allows the sketch to work without interruption. In reading the documentation, I noticed WSWire shuts off the Mega’s internal i2c pullups, and I left them off. There are now just the 4.7K pullups (that value came from one of Nick Gammon’s writups) on the entire i2c bus, and everything works fine with or without the datalogger installed. I ordered an oscilloscope so I can look at the signals and hopefully get to the root of the problem.

When I stopped panicking, and started thinking, I came to the realization that the troubles started not with the addition of the HMC588L compass, but rather when I switched to the HMC5883L_simple library instead of the original Adafruit libraries I had been using. I have not tried reverting libraries because: A, it now works, and B, the simple library gives much more stable readings.

I noticed WSWire shuts off the Mega's internal i2c pullups

The Mega has on-board pull-ups (10k) which cannot be turned off by code.

pylon:
The Mega has on-board pull-ups (10k) which cannot be turned off by code.

Thanks, I didn't know that. I guess the library author must have been referring to the Uno. I'll have my scope Thursday, and hopefully that will shed some light on what is going on, although I may need some advice interpreting it.....

Update.....I got my oscilloscope, and it is a handy piece of equipment, for sure. The i2c waveforms looked pretty sad, as rise-times were way too long. I added some 2.2K pullups and that cleaned them up a lot. Things were better, but I was still getting lockups once or twice a day, which was a vast improvement. The total length of wires attached to the I2C bus was about 10 feet, which I realize now is pushing it pretty hard. Stronger pullups than 2.2K caused the HMC5883L chip to fail immediately, so I left them alone.

I wound up putting the OLED on software SPI, thanks to Oliver's library. It was kind of funny how simple it was. I downloaded a software spi library (I don't remember which one) and had things royally borked for a bit, LOL
Then I figured, what the heck, I stripped out all the software spi cruft and did what I should have done in the first place, and that was change the U8X8 constructor line, substituting the pins I wanted to use instead of the ones listed, and that was that. It worked like a charm, and the display seems to refresh much faster now. The sketch has been running 2 days now without a hiccup, and I have my fingers crossed....

To anyone reading this, hooking up the 2.42" OLED display to software spi only needed 3 of the Mega's pins. I used #4 for the SCL (clock) pin, #5 for the SDA (data) pin, and #43 for the DC pin. The Reset pin is connected to the Mega's reset, and the VDD and CS pins to ground, VCC to 5V+.
The U8X8 constructor line is: U8X8_SSD1306_128X64_NONAME_4W_SW_SPI u8x8(4,5,U8X8_PIN_NONE,43,U8X8_PIN_NONE);

Edit...Grounding the CS pin works as long as the display is the only thing on the SPI bus, and I may have been a bit premature connecting the display's reset to the Mega's reset. It hasn't been a problem yet, but research seems to indicate that the display reset may need to be actively controlled. If any one sees this, and I am mistaken, please correct me. TIA.