0

It gives reliable readings for a couple of seconds and then it becomes unreliable.

My arduino code is this:

#include<Wire.h>
const int MPU6050_addr=0x68;
int16_t AccX,AccY,AccZ,Temp,GyroX,GyroY,GyroZ;
void setup(){
  Wire.begin();
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
}
void sendVector(float x, float y, float z) {
  Serial.print(x, 4);
  Serial.print("|");
  Serial.print(y, 4);
  Serial.print("|");
  Serial.println(z, 4);
}

void loop(){
  Wire.beginTransmission(MPU6050_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU6050_addr,14,true);
  AccX=Wire.read()<<8|Wire.read();
  AccY=Wire.read()<<8|Wire.read();
  AccZ=Wire.read()<<8|Wire.read();
  Temp=Wire.read()<<8|Wire.read();
  GyroX=Wire.read()<<8|Wire.read();
  GyroY=Wire.read()<<8|Wire.read();
  GyroZ=Wire.read()<<8|Wire.read();
  //Serial.print(AccX);
  //Serial.print(AccY);
  //Serial.print(AccZ);
  //Serial.print(" || Temp = "); Serial.print(Temp/340.00+36.53);
  //Serial.print(" || GyroX = "); 
  //Serial.println(GyroX);
  //Serial.print(" || GyroY = "); 
  //Serial.println(GyroY);
  //Serial.print(" || GyroZ = "); 
  //Serial.println(GyroZ);
  sendVector(GyroX, GyroY, GyroZ);
  delay(100);
}

The above sends data to unity which controls an object. The object works with the gyroscope and then due to the random readings it rotates quickly and randomly.

0

I don't think you have given enough information to help you debug this, but i can try to give some advice:

First verify that the chip works correctly

Use the arduino plot monitor, and a sample program, to see the behavior of the chip and values over time; make sure its not a problem with the chip

Second verify that you are interpreting the values correctly

The MPU6050 outputs 16-bit data, but doesn't have 16-bit resolution, this means that the lower bits of the data are random, meaningless noise, and should be ignored/discarded.

If you are assuming that all 16 bits contain valid data, and your code is sensitive enough to be affected by those lower bits, then you will see unstable random data.

Third consider other (possible) obstacles

Try increasing the serial BAUD rate, you may be taking too much time and causing problems that way.

Maybe you are getting out of sync while reading from the chip, if there is additional data being sent, you could could build up a backlog in the buffer, and start interpreting messages incorrectly because you are reading part of one and part of another. Try using a library to talk to the chip, one that other people have used and is known to work; this could remove bugs you are blind to, or bugs that everyone will run into, but they have already been fixed in the library!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.