Sensor Values are drifting

So I wrote a sketch using Dr Owens tutorial to see the sensor values the ardupilot 2.6 was outputting. When I upload the code to the ardupilot and run serial monitor, my pitch goes to -2.1 and stays there. My roll goes to -1.4 and stops there. My yaw is the worst. It just keeps incrementing towards negative. I do not move the APM at all. It is completely stationary when I initialise it. Could it be the code I'm using. I also took off the cover of the board and put it back on. Could that be it. Has anyone else had this problem before?

PS, I using the libraries Dr Owen posted on his "How to proram your own Autopilot" tutorial.

Also, my Pitch is confined to -88<P<88. 

I wasnt having any issues yesterday

My code is below


#include <AP_Common.h>
#include <AP_Math.h>
#include <AP_Param.h>
#include <AP_Progmem.h>
#include <AP_ADC.h>
#include <AP_InertialSensor.h>

#include <AP_HAL.h>
#include <AP_HAL_AVR.h>

// MPU6050 accel/gyro chip
AP_InertialSensor_MPU6000 ins;

const AP_HAL::HAL& hal = AP_HAL_AVR_APM2; // Hardware abstraction layer

void setup()

// Turn off Barometer to avoid bus collisions
hal.gpio->pinMode(40, GPIO_OUTPUT);
hal.gpio->write(40, 1);

// Turn on MPU6050 - quad must be kept still as gyros will calibrate

// initialise sensor fusion on MPU6050 chip (aka DigitalMotionProcessing/DMP)
hal.scheduler->suspend_timer_procs(); // stop bus collisions

// We're ready to go! Now over to loop()


void loop()

// Ask MPU6050 for orientation
float roll,pitch,yaw;
ins.quaternion.to_euler(&roll, &pitch, &yaw);
roll = ToDeg(roll) ;
pitch = ToDeg(pitch) ;
yaw = ToDeg(yaw) ;

// Ask MPU6050 for gyro data
Vector3f gyro = ins.get_gyro();
float gyroPitch = ToDeg(gyro.y), gyroRoll = ToDeg(gyro.x), gyroYaw = ToDeg(gyro.z);

PSTR("P:%4.1f R:%4.1f Y:%4.1f\n"),



You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –


  • I am also curious about this. I ran a similar test on an APM yaw drift was about 0.45 degrees per second....this is using the Arducopter AP_InertialSensor libraries.

    I remember using a standalone MPU6050 with an Arduino once using the standard MPU6XXX libraries and I was getting much better results...I don't remember exactly, but I feel like it was around 1 degree per minute on the yaw if left alone on a desk and that was after doing the recommended calibrations.

    What bugs me though is the pitch being limited to +-88~90 degrees. I'm sure there is a reason for why it is the way it is, but I would rather have -180 to 180.

    I haven't tried looking in the libraries yet, but is there an easy way to modify the pitch range to -180 to 180?

  • Hello Ozy. I used your code and my values were not drifting. They were initially but stabilized later on. My pitch is varying from -90<P<90. I think yours will too if you orient the quad properly. It seems like it is less than 90 but if you rotate slightly in the yaw and roll directions, the pitch will also change.

This reply was deleted.


sam liked Jimmy Oliver's profile
Aug 25
Mike Whitney liked Mike Whitney's profile
Jul 19
Abdulatif Tlaib liked Ryan's profile
Jun 24