MPU6050 No Data..

Hi all,

Below is my code to display some simple values from my MPU6050, the code has worked in the past and has recently been playing up. When I run the code now it only returns 0’s for all values apart from the temperature which is 36.53 oC.

Any ideas as to why I would get lots of 0’s? Thanks.

#include "I2Cdev.h"
#include "MPU6050.h"
 #include "Wire.h"
const int MPU_addr=0x68;  // I2C address of the MPU-6050
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
float angleY;
float angleX;
float gyroY;
float gyroX;
MPU6050 mpu;

//      Offset Values:  XA      YA      ZA      XG      YG      ZG
int MPUOffsets[6] = {  -1730,  1710,   1326,    75,    -18,     21}; 
  
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  
  mpu.setXAccelOffset(MPUOffsets[0]);
  mpu.setYAccelOffset(MPUOffsets[1]);
  mpu.setZAccelOffset(MPUOffsets[2]);
  mpu.setXGyroOffset(MPUOffsets[3]);
  mpu.setYGyroOffset(MPUOffsets[4]);
  mpu.setZGyroOffset(MPUOffsets[5]);
  mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  
  /* 
  AFS_SEL | Full Scale Range | LSB Sensitivity |   Accelerometer Set up
     0    |      ±2g         |   16384 LSB/g   |
     1    |      ±4g         |   8192 LSB/g    |
     2    |      ±8g         |   4096 LSB/g    |
     3    |      ±16g        |   2084 LSB/g    |
     
  FS_SEL  | Full Scale Range | LSB Sensitivity |    Gyro Setup (Leave at default value FS_SEL=0)
     0    |      ±250 deg/s  |  131 LSB/deg/s  |
     1    |      ±500 deg/s  |  65.5 LSB/deg/s |
*/
  
  Wire.endTransmission(true);
  Serial.begin(9600);
}

void loop(){
  
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,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)
  
  angleY = atan2(AcY, AcZ)*4068/71;
  angleX = atan2(AcX, AcZ)*4068/71;
  gyroY = GyY/131;
  gyroX = GyX/131;
  
  
  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.print(GyZ);
  Serial.print(" | pitch = "); Serial.print(angleY);
  Serial.print(" | roll = "); Serial.println(angleX);
 
  delay(333);
  
  
  
}

Update: after uploading the same code many time it just works randomly. Using other code it works first time.