MPU6050 "Headmouse" yaw drift

I am an engineering student tasked with making a Headmouse using a mpu6050 and a eonardo board, I am familiar with using the mouse library and easily merged it with the example code MPU6050_DMP6
however I am running into an issue where after letting it stabilize, the yaw value starts to drift by around one degree a second.
(No I have not optimized code as memory is not currently the issue at hand)
The mouse code is near the very bottom

Thank you in advance!

#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
#include "Mouse.h"

    #include "Wire.h"

MPU6050 mpu;

#define LED_PIN 13 // (Arduino is 13, Teensy is 11, Teensy++ is 6)
bool blinkState = false;

// MPU control/status vars
bool dmpReady = false;  // set true if DMP init was successful
uint8_t mpuIntStatus;   // holds actual interrupt status byte from MPU
uint8_t devStatus;      // return status after each device operation (0 = success, !0 = error)
uint16_t packetSize;    // expected DMP packet size (default is 42 bytes)
uint16_t fifoCount;     // count of all bytes currently in FIFO
uint8_t fifoBuffer[64]; // FIFO storage buffer

// orientation/motion vars
Quaternion q;           // [w, x, y, z]         quaternion container
VectorInt16 aa;         // [x, y, z]            accel sensor measurements
VectorInt16 aaReal;     // [x, y, z]            gravity-free accel sensor measurements
VectorInt16 aaWorld;    // [x, y, z]            world-frame accel sensor measurements
VectorFloat gravity;    // [x, y, z]            gravity vector
float euler[3];         // [psi, theta, phi]    Euler angle container
float ypr[3];           // [yaw, pitch, roll]   yaw/pitch/roll container and gravity vector

// packet structure for InvenSense teapot demo
uint8_t teapotPacket[14] = { '

, 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, ‘\r’, ‘\n’ };

//Mouse movements
int MouseX, MouseY, Pitch, Yaw, adder;

// ================================================================
// ===              INTERRUPT DETECTION ROUTINE                ===
// ================================================================

volatile bool mpuInterrupt = false;    // indicates whether MPU interrupt pin has gone high
void dmpDataReady() {
    mpuInterrupt = true;

// ================================================================
// ===                      INITIAL SETUP                      ===
// ================================================================

void setup() {
    // join I2C bus (I2Cdev library doesn’t do this automatically)
        TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz)
        Fastwire::setup(400, true);
    while (!Serial); // wait for Leonardo enumeration, others continue immediately

// initialize device
    Serial.println(F(“Initializing I2C devices…”));

// verify connection
    Serial.println(F(“Testing device connections…”));
    Serial.println(mpu.testConnection() ? F(“MPU6050 connection successful”) : F(“MPU6050 connection failed”));

// wait for ready
    Serial.println(F("\nSend any character to begin DMP programming and demo: "));
    while (Serial.available() &&; // empty buffer
    while (!Serial.available());                // wait for data
    while (Serial.available() &&; // empty buffer again

// load and configure the DMP
    Serial.println(F(“Initializing DMP…”));
    devStatus = mpu.dmpInitialize();

// supply your own gyro offsets here, scaled for min sensitivity
    mpu.setZAccelOffset(1788); // 1688 factory default for my test chip

// make sure it worked (returns 0 if so)
    if (devStatus == 0) {
        // turn on the DMP, now that it’s ready
        Serial.println(F(“Enabling DMP…”));

// enable Arduino interrupt detection
        Serial.println(F(“Enabling interrupt detection (Arduino external interrupt 0)…”));
        attachInterrupt(0, dmpDataReady, RISING);
        mpuIntStatus = mpu.getIntStatus();

// set our DMP Ready flag so the main loop() function knows it’s okay to use it
        Serial.println(F(“DMP ready! Waiting for first interrupt…”));
        dmpReady = true;

// get expected DMP packet size for later comparison
        packetSize = mpu.dmpGetFIFOPacketSize();
    } else {
        // ERROR!
        // 1 = initial memory load failed
        // 2 = DMP configuration updates failed
        // (if it’s going to break, usually the code will be 1)
        Serial.print(F(“DMP Initialization failed (code “));

// configure LED for output
    pinMode(LED_PIN, OUTPUT);


// ================================================================
// ===                    MAIN PROGRAM LOOP                    ===
// ================================================================

void loop() {
    // if programming failed, don’t try to do anything
    if (!dmpReady) return;

// wait for MPU interrupt or extra packet(s) available
    while (!mpuInterrupt && fifoCount < packetSize) {


// reset interrupt flag and get INT_STATUS byte
    mpuInterrupt = false;
    mpuIntStatus = mpu.getIntStatus();

// get current FIFO count
    fifoCount = mpu.getFIFOCount();

// check for overflow (this should never happen unless our code is too inefficient)
    if ((mpuIntStatus & 0x10) || fifoCount == 1024) {
        // reset so we can continue cleanly
        Serial.println(F(“FIFO overflow!”));

// otherwise, check for DMP data ready interrupt (this should happen frequently)
    } else if (mpuIntStatus & 0x02) {
        // wait for correct available data length, should be a VERY short wait
        while (fifoCount < packetSize) fifoCount = mpu.getFIFOCount();

// read a packet from FIFO
        mpu.getFIFOBytes(fifoBuffer, packetSize);
        // track FIFO count here in case there is > 1 packet available
        // (this lets us immediately read more without waiting for an interrupt)
        fifoCount -= packetSize;

            // display Euler angles in degrees
            mpu.dmpGetQuaternion(&q, fifoBuffer);
            mpu.dmpGetGravity(&gravity, &q);
            mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
            Serial.print(ypr[0] * 180/M_PI);
            Serial.print(ypr[1] * 180/M_PI);
            Serial.println(ypr[2] * 180/M_PI);

// blink LED to indicate activity
        blinkState = !blinkState;
        digitalWrite(LED_PIN, blinkState);

Pitch = (ypr[2] * 180/M_PI); //Same code for displaying angles but used here
        Yaw = (ypr[0] * 180/M_PI); //Same code for displaying angles but used here

MouseY = Pitch/3; //Cut down by 3 for sensitivity issues
        MouseX = Yaw/3; //Cut down by 3 for sensitivity issues
        Mouse.move(MouseX, -MouseY); //MouseY negative for correct direction

You need a magnetometer to stabilize the yaw angle. The MPU6050 does not have one.

The project would be much, much easier if you used an absolute orientation sensor, like the BNO055. Any of Pololu's IMUs with decent AHRS software will work too.

Im afraid am not able to access that one, is it possible i could use some sort of offset that’s established on startup?
thank you for responding so quickly!

No, gyros drift, and the drift depends on temperature, supply voltage and other effects. The MPU9050 has a magnetometer.

I see, well thank you, do you have any suggestions of which type to use? preferably one that integrates well with a breadboard?

do you have any suggestions

Read the links in reply #1.

Will a magnetometer work in a magnetic noisy environment behind a computer screen ? In the past a number of "air" mouses had a centre button, that could be pressed to set the cursor in the middle.

Thank you for your replies! :)