Difficulty using Wire.endTransmission() with MPU6050

I recently got an MPU6050 accelerometer for a project I'm starting with an Arduino UNO. I was able to find this sample code and attempted to run it only to find that the code would freeze up at Wire.endTransmission(). I've done some searching, and it appears this issue was discussed a while ago in various threads. It didn't look like anything ever came of those discussions other than suggesting another i2c library. If this is still the case, how do I use these 12c library functions in place of the ones here? I'm new to Arduino, so please bear with me if I'm slow to understand.

Here's the code I'm using:

#include<Wire.h>
const int MPU=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void loop(){
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  Serial.print("AcX = "); Serial.print(AcX);
  Serial.print(" | AcY = "); Serial.print(AcY);
  Serial.print(" | AcZ = "); Serial.print(AcZ);
  Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
  Serial.print(" | GyX = "); Serial.print(GyX);
  Serial.print(" | GyY = "); Serial.print(GyY);
  Serial.print(" | GyZ = "); Serial.println(GyZ);
  delay(333);
}

Thanks!

Nafxl123:
I recently got an MPU6050 accelerometer for a project I'm starting with an Arduino UNO. I was able to find this sample code and attempted to run it only to find that the code would freeze up at Wire.endTransmission(). I've done some searching, and it appears this issue was discussed a while ago in various threads. It didn't look like anything ever came of those discussions other than suggesting another i2c library. If this is still the case, how do I use these 12c library functions in place of the ones here? I'm new to Arduino, so please bear with me if I'm slow to understand.

Here's the code I'm using:

#include<Wire.h>

const int MPU=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
void setup(){
 Wire.begin();
 Wire.beginTransmission(MPU);
 Wire.write(0x6B);  // PWR_MGMT_1 register
 Wire.write(0);     // set to zero (wakes up the MPU-6050)
 Wire.endTransmission(true);
 Serial.begin(9600);
}
void loop(){
 Wire.beginTransmission(MPU);
 Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
 Wire.endTransmission(false);
 Wire.requestFrom(MPU,14,true);  // request a total of 14 registers
 AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
 AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
 AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
 Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
 GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
 GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
 GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
 Serial.print("AcX = "); Serial.print(AcX);
 Serial.print(" | AcY = "); Serial.print(AcY);
 Serial.print(" | AcZ = "); Serial.print(AcZ);
 Serial.print(" | Tmp = "); Serial.print(Tmp/340.00+36.53);  //equation for temperature in degrees C from datasheet
 Serial.print(" | GyX = "); Serial.print(GyX);
 Serial.print(" | GyY = "); Serial.print(GyY);
 Serial.print(" | GyZ = "); Serial.println(GyZ);
 delay(333);
}




Thanks!

The Wire library will hang forever in Wire.endTransmission(), Wire.requestFrom() if SCL is low.

If your code is hanging in either of these calls you have a hardware problem.

You have two solutions.

  • Change the hardware so that SCL is not held low forever
  • use a modified version of Wire that handles this error.

I use I2C for communication to sensors that are/can be destroy during normal use. The physical destruction of these sensors can short GND to SCL,SDA, or VDD to SCL,SDA, or any combination. To handle these failures I designed my I2C bus with current limited VDD, individual I2C branches that can be disconnected, modified the Wire library to return bus error timeouts.

Here is my Wire library

Chuck.

Nafxl123:
I recently got an MPU6050 accelerometer for a project I'm starting with an Arduino UNO. I was able to find this sample code and attempted to run it only to find that the code would freeze up at Wire.endTransmission(). I've done some searching, and it appears this issue was discussed a while ago in various threads. It didn't look like anything ever came of those discussions other than suggesting another i2c library. If this is still the case, how do I use these 12c library functions in place of the ones here?

Does the arduino board detect your mpu6050 ?

The first thing to do is to run the

http://playground.arduino.cc/Main/I2cScanner

If you need working libraries, then you could grab the ones at this link here (message #8):

http://forum.arduino.cc/index.php?topic=243276.msg2317218#msg2317218

The I2Cdev directory and the Wire directory are the ones you need. I guess you could pop all those libraries into your arduino libraries directory.

In any case, it's important to first make sure that the I2Cscanner runs, and is able to detect your MPU6050 device at say address 0x68.