Help: Mpu6050 Code repair but still unstable! "Partially resolved"

Hello, Another newbie full of doubts, thank you in advance for your attention.
I made a copy of the original imu code, and I’m trying to use it with MPU 6050. Here’s the code, it compiles and returns OK, but in agopen it’s unstable, here’s the arduino code. Thanks in advance if I get any help.

Code:

#include <Wire.h>

#define MPU_ADDRESS 0x68

uint8_t data[] = {0x80, 0x81, 0x7D, 0xD3, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F};

int16_t dataSize = sizeof(data);

bool useMPU = false;

void setup()
{
Serial.begin(38400); // Start serial port
Wire.begin();

//test if UPD working
uint8_t error;
Serial.println(“Checking for MPU”);
Wire.beginTransmission(MPU_ADDRESS);
error = Wire.endTransmission();

if (error == 0)
{
Serial.println(“Error = 0”);
Serial.print(“MPU ADDRESS: 0x”);
Serial.println(MPU_ADDRESS, HEX);
Serial.println(“MPU Ok.”);
useMPU = true;
}
else
{
Serial.println(“Error = 4”);
Serial.println(“MPU not Connected or Found”);
}
}

void loop()
{
if(useMPU)
{
Wire.beginTransmission(MPU_ADDRESS);
Wire.write(0x3B);
Wire.endTransmission();

  Wire.requestFrom(MPU_ADDRESS, 14); 
  while(Wire.available() < 14);       

  //the heading x10
  data[6] = Wire.read();
  data[5] = Wire.read();

  Wire.beginTransmission(MPU_ADDRESS);  
  Wire.write(0x1C);                    
  Wire.endTransmission();

  Wire.requestFrom(MPU_ADDRESS, 14);  
  while(Wire.available() < 14);        

  data[8] = Wire.read();
  data[7] = Wire.read();

}

   int16_t CK_A = 0;

  for (int16_t i = 2; i < dataSize - 1; i++)
  {
      CK_A = (CK_A + data[i]);
  }

  data[dataSize - 1] = CK_A;

  String hexString = "";
  for (int i = 0; i < dataSize; i++) {
    hexString += String(data[i], HEX);
    hexString.toUpperCase();
    hexString += " ";
  }

 Serial.write(data,dataSize);
  
  Serial.println(hexString);
  delay(95); 

}

return in serial:
Checking for MPU
Error = 0
MPU ADDRESS: 0x68
MPU Ok.

1 Like

the mpu must output information that must be processed/converted before being sent to AOG, you must refer to the datasheet.

1 Like

Thank you, I will check the data sheet information.

You can watch what I did to convert data from imu wit. since they are also based on mpu6050 or 9250, the output data may be in the same format

2 Likes

Thank you!
I noticed the extra “if” you introduced, cool.