0

I am testing the MPU6050, and I noticed that the values keep changing after I start moving the device; please have a look at the code.

after starting the serial monitor, everything will be zeros; if I hold the device and start playing with it, I can see a slight shift in the values, which will increase until the device will present non-flat values even if I return it to the table in a flat position



#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

// ============== LEDs Setup (Imad) ================
int roll_Left = 9;
int pitch_Up = 10;
int center = 11;
int pitch_Down = 12;
int roll_Right = 13;
// =================================================

// Timers
unsigned long timer = 0;
float timeStep = 0.01;

// Pitch, Roll and Yaw values
float pitch = 0;
float roll = 0;
float yaw = 0;

void setup() 
{
  Serial.begin(115200);

    //============= Light On Imad ===========
  pinMode(roll_Left, OUTPUT);
  pinMode(pitch_Up, OUTPUT);
  pinMode(center, OUTPUT);
  pinMode(pitch_Down, OUTPUT);
  pinMode(roll_Right, OUTPUT);
  //======================================

  // Initialize MPU6050
  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }
  
  // Calibrate gyroscope. The calibration must be at rest.
  // If you don't want calibrate, comment this line.
  mpu.calibrateGyro();

  // Set threshold sensivty. Default 3.
  // If you don't want use threshold, comment this line or set 0.
  mpu.setThreshold(3);
}

void loop()
{
  timer = millis();
/*
  //================ Imad Lights ==========
 digitalWrite(roll_Left, HIGH);
 digitalWrite(pitch_Up, HIGH);
 digitalWrite(center, HIGH);
 digitalWrite(pitch_Down, HIGH);
 digitalWrite(roll_Right, HIGH);
//=======================================
*/
  // Read normalized values
  Vector norm = mpu.readNormalizeGyro();

  // Calculate Pitch, Roll and Yaw
  pitch = pitch + norm.YAxis * timeStep;
  roll = roll + norm.XAxis * timeStep;
  yaw = yaw + norm.ZAxis * timeStep;

  // Output raw

  if ((pitch <= 2) && (pitch >= -2) && (roll <= 2 )&& (roll >= -2)) {
    digitalWrite(center, HIGH);
  }else {
    digitalWrite(center, LOW);
  }
  
  Serial.print(" Pitch = ");
  Serial.print(pitch);
  if (pitch > 3) {
    digitalWrite(pitch_Up, HIGH);
  }else if (pitch < -3) {
    digitalWrite(pitch_Down, HIGH);
  }else {
    digitalWrite(pitch_Up, LOW);
    digitalWrite(pitch_Down, LOW);
  }
  Serial.print(" Roll = ");
  Serial.print(roll);
    if (roll > 3) {
    digitalWrite(roll_Right, HIGH);
  }else if (roll < -3) {
    digitalWrite(roll_Left, HIGH);
  }else {
    digitalWrite(roll_Left, LOW);
    digitalWrite(roll_Right, LOW);
  }
  Serial.print(" Yaw = ");
  Serial.println(yaw);

  // Wait to full timeStep period
  delay((timeStep*1000) - (millis() - timer));
}

Is there anything we need to add to keep the values the same if we place the device back in the same place

Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
Wolfsbane
  • 13
  • 3

1 Answers1

0

Serial.print is creating a huge amount of lag in you code, making the calculation for roll, pitch and yaw inaccurate. Step 4 of this article talks about this lag: https://www.instructables.com/How-to-Make-a-Robot-Car-Drive-Straight-and-Turn-Ex/

The article also includes the Arduino code for MPU6050.

J Z
  • 26
  • 4