Problems getting I2C to work

Hi,

I have trouble getting I2C to work. I have bought a compass of type HMC5883L but have not managed to get any communication up and running. I have quadruple checked wiring and it is correct. Even without anything connected the I2C bus the Arduino gets stuck in the code and needs to be resetted to get back to work.

The problem seems to boil down to the wire.h communication. As soon as I use Wire.endTransmission() the arduino gets stuck (so non of the I2C scanners does not produce any results).

My specific questions:

  • Should an I2C scanner code run even without anything connected to A4 and A5?
  • Could my Arduino be faulty since the code is not running?
  • What else could I try to find the error (with or without the HMC5883L)?

Thanks so much for your help,
mroggi

Wire.endTransmission() can return an errorcode
so please try

int x = Wire.endTransmission() ;
Serial.println(x, DEC);

in your code.

Post the value of x (and possibly the rest of your code) so we can diagnose...

Thanks so much for your quick reply.

I just tried your code but do not get any value as the code is never executed beyond the line x = Wire.endTransmission(). It seems that the Arduino gets stuck in that function.
It would be great if you could me
Thanks!

which version of Arduino?
Which board?

Please post the complete code or a minimal version that shows the problem.

Hi,
I have an Arduino R3 (original board). Here is the minimal code:

void setup() {
Serial.begin(9600);
Serial.println("\nI2C Scanner");
Wire.beginTransmission(0x1E);
}

void loop() {
[...]
Serial.println("x1");
Wire.beginTransmission(address);
Serial.println("x2");
error = Wire.endTransmission();
Serial.println("x3");
[...]

Code never gets to "x3" but gets stuck in endTransmission(); I have done lots of research on this forum and found the issue multiple times. However, nobody really posted a solution except changing harware. Feels to me that it is a software bug though?

Should the code run through if I pull A4 and A5 to 5V?

Thanks,
Matthias

void setup() {
  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
  Wire.beginTransmission(0x1E);
}

void loop() {
[...]
    Serial.println("x1");
    Wire.beginTransmission(address);
    Serial.println("x2");
    error = Wire.endTransmission();
    Serial.println("x3");
[...]

this is not executable code I can run to recreate the error @home. please post a compilable version that shows the bug.

At least missing is a Wire.begin(); call in setup and an declaration of int error.

Hi,

thanks for your patience and sorry for not posting the full code right away. As said, the code runs until " error = Wire.endTransmission();", so the next line is never executed.

My questions are:

  • Should the code run with simple pull ups to A4 and A5? (I tried this and it doesn’t work, still hangs)
  • What other error checking can I do to find the problem?

Thanks,
Matthias

#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++ )
  {
    Serial.println("x1");
    Wire.beginTransmission(address);
    Serial.println("x2");
    error = Wire.endTransmission();
    Serial.println("x3");

    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
}

Code works on my UNO without pull ups.

but I miss something in your sketch, can you try this one?

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

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

  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    Serial.println("x1");
    Wire.beginTransmission(address);
    Serial.println("x2");
    error = Wire.endTransmission();
    Serial.println("x3");

    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  // <<<<<<<<<<<<<<<< removed this comparison to capture all errors.
    {
      Serial.print("Unknow error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);   // <<<<<<<<<<<<<<<< three lines here
      Serial.print("\t");
      Serial.println(error,DEC);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

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

In your setup( ) function, it should be Wire.begin() not Wire.beginTransmission( )

I think that you have a wiring problem. Did you understand correctly how the I2C clock and signal wires are supposed to be connected ?