MPU 6050 gyro throws out random readings

Hi there, arduino newbie here. I am currently using a MPU 6050 gyro to control two pwm servo motors. When I have just the gyro hooked up to my arduino the readings stay consistent and accurate to the physical state of the gyro, but when I attach either one or both of my servos the gyro will at random spit out completely false readings causing my gyros to spin uncontrollably.

Here are my serial monitor readings that show when the gyro first goes haywire:

The 090/180 at the start of the lines refers to the state of my two servos. The readings are given in yaw-pitch-roll notation.

I am using a modified version of the MPU6050_DMP6 example.

// I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
// for both classes must be in the include path of your project
#include "I2Cdev.h"

#include "MPU6050_6Axis_MotionApps20.h"
//#include "MPU6050.h" // not necessary if using MotionApps include file

// Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
// is used in I2Cdev.h
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
   #include "Wire.h"
#endif

// class default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for SparkFun breakout and InvenSense evaluation board)
// AD0 high = 0x69
MPU6050 mpu;
//MPU6050 mpu(0x69); // <-- use for AD0 high

#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] = { '

Wiring wise I have 5V to vcc gnd to gnd SCL to SCL SDA to SDA int to dig2 and one servo to dig6 and the other to dig7. I took out most of the irrelevant code in the embedded code section due to the 9000 char post limit but attached the full .ino in case you need to look at that. Any help would be greatly appreciated!

SensorsAndServos.ino (16.2 KB), 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, ‘\r’, ‘\n’ };

// ================================================================
// ===                    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) {
       // other program behavior stuff here
       // .
       // .
       // .
       // if you are really paranoid you can frequently test in between other
       // stuff to see if mpuInterrupt is true, and if so, “break;” from the
       // while() loop to immediately process the MPU data
       // .
       // .
       // .
   }

// 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
       mpu.resetFIFO();
       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;

#ifdef OUTPUT_READABLE_YAWPITCHROLL
           // display Euler angles in degrees
           mpu.dmpGetQuaternion(&q, fifoBuffer);
           mpu.dmpGetGravity(&gravity, &q);
           mpu.dmpGetYawPitchRoll(ypr, &q, &gravity);
           Serial.print(“ypr\t”);
           Serial.print(ypr[0] * 180/M_PI);
           Serial.print("\t");
           Serial.print(ypr[1] * 180/M_PI);
           Serial.print("\t");
           Serial.println(ypr[2] * 180/M_PI);
       #endif

double temp = (ypr[0] * 180/M_PI);
       int pos = 90;
       if(temp < -0.5){
         pos = 180;
       }
       else if(temp > 0.5)
       {
         pos = 0;
       }
       myservo.write(pos);
       Serial.print(pos);
       Serial.print("\t");

double temp2 = (ypr[2] * 180/M_PI);
       int pos2 = 90;
       if(temp2 < -0.5){
         pos2 = 180;
       }
       else if(temp2 > 0.5)
       {
         pos2 = 0;
       }
       myservo2.write(pos2);
       Serial.print(pos2);
       Serial.print("\t");

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


Wiring wise I have 5V to vcc gnd to gnd SCL to SCL SDA to SDA int to dig2 and one servo to dig6 and the other to dig7. I took out most of the irrelevant code in the embedded code section due to the 9000 char post limit but attached the full .ino in case you need to look at that. Any help would be greatly appreciated!

[SensorsAndServos.ino|attachment](upload://3dDcxDvCLlEZ20LTr3ubtR1fOx8.ino) (16.2 KB)

You forgot to post a wiring diagram, but I will guess that you are trying to power the motors/servos either from the 5V Arduino output, or from the same power supply.

Neither option will work well, and the first can actually destroy the Arduino. Use a separate power supply for motors and servos, and connect the grounds.

Sorry that I forgot the diagram, slipped my mind. Thank you so much for the suggestion, I didn't realize that could have been the problem but I changed the servos to a different power supply and that seems to have fixed my issue from what I can tell so far. Thanks again!