I'm trying to pull gyro data off an MPU 6050 using an Arduino Nano R4. When I run the code, it returns extremely accurate values, but then angle X locks at -90 degrees ang AngleY locks at 90 degrees. Sometimes the code runs perfectly for 5 minutes before it freezes, and sometimes it runs for half a second. Hitting the reset button locks it up instantly. I have to unplug power and plug it back in for it to run correctly for any amount of time. Any guesses on how to fix this? I've heard that grounding the AD0 pin on the gyroscope can sometimes work to fix this issue. As I was typing this, it ran for 5 minutes. When I restarted it to take a video, it ran for 30 seconds. It doesn't matter how much or little the gyro is moved. It always locks up. #include <MPU6050_tockn.h> #include <Wire.h>
I took a look at the library you are using, MPU6050_tockn. The code is wrong, often fails and is not worth ten minutes of your time, except possibly as an unfortunate learning experience. Take a look at all these issues, not fixed after many years.
Make sure your connections are cleanly soldered to the sensor board, and try this simple code to read out just the raw data. Keep in mind that the gyro reports rotation rates, not angles.
// MPU-6050 Short Example Sketch
// By Arduino User JohnChi
// August 17, 2014
// Public Domain
#include<Wire.h>
const int MPU_addr = 0x68; // I2C address of the MPU-6050
int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
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)
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); // request a total of 14 registers
int16_t t = Wire.read();
AcX = (t << 8) | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
t = Wire.read();
AcY = (t << 8) | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
t = Wire.read();
AcZ = (t << 8) | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
t = Wire.read();
Tmp = (t << 8) | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
t = Wire.read();
GyX = (t << 8) | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
t = Wire.read();
GyY = (t << 8) | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
t = Wire.read();
GyZ = (t << 8) | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
t = Wire.read();
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);
}
I uploaded that code, and while the MPU6050 is sitting still, the values are jumping all over the place. I'll upload pics of gyro connections in a couple minutes.