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)?
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!
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 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
}