Wire.begin() not working for i2c devices

Hello,

I have been working on a project with several i2c breakouts from Adafruit, used on multiple arduino Mega 2560's (old and new).

Recently, all of my sketches loaded from my desktop show a recurring error on i2c sensors. I have tracked the problem to receiving a returned value of 'false' from the respective [insert sensor object here].begin() in the setup section of the sketch.

There are no warnings from the compiler (even in verbose mode), and appears to be restricted to my desktop, as my labmate can load the same code onto the same Mega using the same breakout and wiring (I'll literally give him the breadboard as-is), and the code runs as intended.

This problem is also independent of breakout (I have at least 5 of each breakout) and Mega (I have 8 Megas). An i2c scanner (code include below) loaded onto a Mega from my desktop returns no i2c devices connected. Since this code bypasses the adafruit sensor libraries and just goes directly to Wire communication, I can only conclude that the problem must lie with the implementation of the Wire library.

I have un-installed and re-installed the arduino IDE (and its associated program files) several times (where the Wire library lives), with no improvement. Being that this is a university desktop, there is a lot of other software installed, and I'm wondering if something installed (like a C or C++ IDE) could be messing with the arduino IDE compiler settings and causing this problem (at this point I'm guessing because the problem is well beyond my ability to understand and fix).

Any thoughts or known IDE conflicts would be greatly appreciated.

i2c scanner code used:

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

Diagnostic check for your MEGA Board.

Disconnect all external circuits from the MEGA Board. Upload the following sketch. If there is no internal fault within the I2C Logic/Connector of the MEGA Board, the program will print this message: The codes have generated START condition.

#include<Wire.h>
void setup() 
{
  Serial.begin(9600);
  Wire.begin();

  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //interrupt bit clear, TWI Bus is enabled, assert START
  while(!(TWCR & (1<<TWINT)))  //wait until the process is complete 
  {
    ;
  }

  if((TWSR & 0xF8) != 0x08)  //check that status word 0x08 has been generated due to START condition
  {
    Serial.print("Internal Bus Error!");
    while(1);
  }
  Serial.println("The codes have generated START condition."); 
}

void loop() 
{

}

Thanks for responding!

So I tried your code and got the "The codes have generated START condition." message. This would seem to line up with the problem not being hardware related, as the Megas and breakouts work with the same code uploaded on a different machine.

Any other ideas?

Any other ideas?

I guess you're running a Windows machine, so un-installing the IDE doesn't remove the installed third-party libraries. If you re-install the IDE it will use your (probably buggy) libraries again.

As I don't have a Windows machine I cannot tell you where to look for these files but you can activate verbose output during the compilation phase. The IDE will tell you which file it uses for the different libraries. Check the complete list, my guess is you'll find a path not pointing to your current IDE installation location. Remove the Wire library there and you will probably get a correct system back again.

@OP
In my Windows based PC, I have Arduino's libraries installed/included in the following paths:

c:\users\GM\Documents\Arduino\libraries|library folders (Wire is not here)
c:\Program Files\Arduino\hardware\arduino\avr\libraries\library folders (Wire is here)
c:\Program Files\Arduino\libraries\library folders

Find the location of the Wire folder and delete it.

Collect this Wire-master.zip library folder.

From the IDE, install the above library. The steps that I follow:
IDE ---> Sketch ---> Include Library ---> Add .ZIP Library... and select the path of the Wire-master.zip and then click on open.

Wire-master.zip (27.5 KB)

@GolamMostafa
Thanks for replying! I tried what you said, and as before, anything that #include's the Wire.h library header compiles just fine, but the i2c devices connected cannot be seen.

If I try to #include the library from the IDE menu instead of just typing out '#include Wire.h', it tries to include the WireKinetis.h header as well, and throws compiler errors left and right.

Either way, the problem still exists. :sob:

GolamMostafa:
Diagnostic check for your MEGA Board.

Disconnect all external circuits from the MEGA Board. Upload the following sketch. If there is no internal fault within the I2C Logic/Connector of the MEGA Board, the program will print this message: The codes have generated START condition.

#include<Wire.h>

void setup()
{
 Serial.begin(9600);
 Wire.begin();

TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //interrupt bit clear, TWI Bus is enabled, assert START
 while(!(TWCR & (1<<TWINT)))  //wait until the process is complete
 {
   ;
 }

if((TWSR & 0xF8) != 0x08)  //check that status word 0x08 has been generated due to START condition
 {
   Serial.print("Internal Bus Error!");
   while(1);
 }
 Serial.println("The codes have generated START condition.");
}

void loop()
{

}

BTW, I have verified that everything you've provided here does not require the Wire library, and provides the "The codes have generated START condition." output even if 'Wire.begin()' is commented out. I think you might be directly accessing hardware registers on the atmega chip, and thus the Wire library is not needed.

If I try to #include the library from the IDE menu instead of just typing out '#include Wire.h', it tries to include the WireKinetis.h header as well, and throws compiler errors left and right.

And you know where that WireKinetis.h library comes from? Check that path and delete all libraries there. Then try with a fresh IDE installation.

@OP

If you have an I2CLCD, please connect it with your Arduino MEGA Board. Upload the following sketch and check that the Hello! message has appeared on the LCD. This is the LiquidCrystal_I2C.h Library. (If not working, change the address to 0x3F.)

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2); 

void setup() 
{
 lcd.init();
 lcd.backlight();
 lcd.setCursor(0, 0); //DP0 of Top Line
 lcd.print("Hello!");
}


void loop() 
{

}

LiquidCrystal_I2c.zip (19.9 KB)

pylon:
And you know where that WireKinetis.h library comes from? Check that path and delete all libraries there. Then try with a fresh IDE installation.

The WireKinetis library comes form this post:

GolamMostafa:
@OP
In my Windows based PC, I have Arduino's libraries installed/included in the following paths:

c:\users\GM\Documents\Arduino\libraries|library folders (Wire is not here)
c:\Program Files\Arduino\hardware\arduino\avr\libraries\library folders (Wire is here)
c:\Program Files\Arduino\libraries\library folders

Find the location of the Wire folder and delete it.

Collect this Wire-master.zip library folder.

From the IDE, install the above library. The steps that I follow:
IDE ---> Sketch ---> Include Library ---> Add .ZIP Library... and select the path of the Wire-master.zip and then click on open.

So per the quoted post above, I have deleted the stock Wire library folder form my Program Files\Arduino\hardware\arduino\avr\libraries path, and installed the attached Wire library replacement provided. I think restarted my IDE, and all sketches with a prior reference to Wire.h compile just fine, and don't work just like the stock Wire library.

The only difference in folder contents between the stock Wire library folder and the folder supplied by GolamMostafa is the added WireKinetis.h and associated WireKinetis.cpp files. I haven't actually looked through the CPP files in detail, but I have verified that WireKinetis is for a teensy application, and thus not relevant to my hardware. I'm assuming its referenced in Wire or in TWI (one of the Wire utility C files) and thus should not be #include'd in my sketch...but for completeness sake I decided to try it anyway.

In that same spirit, I have gone into the library and deleted WireKinetis.h and the associated .cpp file, and re-compiled/reloaded the sketch (after restarting the IDE). The sketches once again compile just fine, but fail to recognize any i2c device. Same response if I then reinstall the IDE and try again.

Could any other IDE installed on the desktop have changed the way the compiler handles C or CPP files in a way that the wire.ccp or twi.c files would no longer function the way they are intended to...without getting compiler warnings or errors? At this point I'm starting to suspect my hardware is possessed :o

Could any other IDE installed on the desktop have changed the way the compiler handles C or CPP files in a way that the wire.ccp or twi.c files would no longer function the way they are intended to...without getting compiler warnings or errors? At this point I'm starting to suspect my hardware is possessed :o

Do you have a folder called "Arduino" in your "Documents" folder? If yes, delete it completely and build and upload the I2C scan sketch again, but use a fresh install of the IDE and not the one that was mangled by the post of GolamMostafa. Post the results.

I did that with a several combinations of single i2c breakouts, and multiple i2c breakouts on the same bus...and every time I get the following output...

I2C Scanner
Scanning...
No I2C devices found

The key is in finding the difference between the working and the non-working systems.

Please do this:

  • File > Preferences
  • Check "Show verbose output during: compilation"
  • Click OK
  • Open your sketch that uses the Wire library
  • Sketch > Verify/Compile
  • After compilation finishes, click on the black console window at the bottom of the Arduino IDE window.
  • Press "Ctrl+A". This will select all the output in the console window.
  • Press "Ctrl+C". This will copy the output to the clipboard.
  • Paste the output in a reply here using code tags </> button on the toolbar. If the output exceeds the forum's 9000 character limit, save the output as a text file and then post it here as an attachment. If you click the "Reply" button, you will see the "Attachments and other options" link.

@agmotes

Which IDE version ?
Just in case it is specific to something.
Is it just the Adafruit libs ?
Arduino does not hold control over those as they are termed CONTRIBUTED or similar.

So I realized last night that my test circuit (which was last changed after lunch yesterday) had a shoddy connection on one of the wires. One of the several things we were doing in this thread fixed my software problem yesterday, but I have no idea what it was, and thus no idea what actually went wrong. This problem has been going on for 2 weeks or so, so thankfully I'm not a complete dummy, I was just a dummy for a few hours.

Anyway, glad to know that the problem is resolved, thanks to everyone involved!