Hello, I am trying to connect MPU9250 to Arduino UNO board and read sensor data from it via I2C.
1.arduino board : UNO R3
2.sensor : MPU9250
3.IDE : 2.3.2
<What I did & Problem>
1.I soldered MPU9250 with header pins (I gonna call it MPU9250#1)
2.I connected it to the arduino using bread board (VCC-5V, GND-GND, SCL-A5, SDA-A4)
3. I run I2C detection example code
#include <Wire.h>
#define WIRE Wire
void setup() {
WIRE.begin();
Serial.begin(9600);
while (!Serial)
delay(10);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; 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("Unknown error at address 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
} else {
Serial.print("No device found at 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
resetI2CBus();
} else {
Serial.println("done\n");
}
delay(5000); // wait 5 seconds for next scan
}
void resetI2CBus() {
Serial.println("Resetting I2C bus...");
WIRE.end();
delay(100);
WIRE.begin();
}
- but It shows only "I2C scanner", "Scanning..." messages only.
- So I added println function to check what is the problem
#include <Wire.h>
#define WIRE Wire
void setup() {
WIRE.begin();
Serial.begin(9600);
while (!Serial)
delay(10);
Serial.println("\nI2C Scanner");
}
void loop() {
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
WIRE.beginTransmission(address);
Serial.println(address);
error = WIRE.endTransmission();
Serial.println("end Transmission");
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("Unknown error at address 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
} else {
Serial.print("No device found at 0x");
if (address < 16) Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0) {
Serial.println("No I2C devices found\n");
resetI2CBus();
} else {
Serial.println("done\n");
}
delay(5000); // wait 5 seconds for next scan
}
void resetI2CBus() {
Serial.println("Resetting I2C bus...");
WIRE.end();
delay(100);
WIRE.begin();
}
and now Serial monitor shows message like :
I2C Scanner
Scanning...
I2C Scanner
Scanning...
1(code seems not propagate from here)
which menas, the code seems fall in the loop when endTransmission() is called.
and when I pull the jumper cable out, the code start to propagate and show's there's no I2C device found. So I thought that my sensor was out of function for some reason. and actually, it was exposed to too high temperature for a very long time due to my mistake.
6.So I ordered a new sensor with same model. (MPU9250) and connected with same pin config and It succeed..! (Serial monitor says I2C device was found at 0x68, which is known address of MPU9250) and I was so happy
- but after repeating the code for several times, I end up with facing same problem that I stucked. It does not propagate when calling Wire.endTransmission()
8.So I suspected anoter source of trouble, like loose connection. this is the list of what I did to resolve the problem
- I remove the bread board and directly connect MPU9250 and arduino by jumper
- I tried another SCL SDL pin of arduino, not A5 and A4 but the guys next to "AREF" pin
after trying this and run same code, I was shown following message like
104
end Transmission
I2C device found at address 0x68 !
105
end Transmission
No device found at 0x69
106
which means my sensor is alive!!!
9.but another problem is happen, It takes too much time to check each address and it stucks somewhere when repeatedly running the code. and some times It doesn't find MPU9250
Conclusion
So I am thinking that sensor is alright, but My arduino board is the source of problem. So I purchased new one. but i am still not confident what is the problem..
I would like to really thank you if you suggest the cause of trouble and what should I try.