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.