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.


DIY Robocars via Twitter
RT @chr1sa: Just a week to go before our next @DIYRobocars race at @circuitlaunch, complete with famous Brazilian BBQ. It's free, fun for k…
DIY Robocars via Twitter
How to use the new @donkey_car graphical UI to edit driving data for better training
Nov 28
DIY Robocars via Twitter
RT @SmallpixelCar: Wrote a program to find the light positions at @circuitlaunch. Here is the hypothesis of the light locations updating ba…
Nov 26
DIY Robocars via Twitter
RT @SmallpixelCar: Broke my @HokuyoUsa Lidar today. Luckily the non-cone localization, based on @a1k0n LightSLAM idea, works. It will help…
Nov 25
DIY Robocars via Twitter
@gclue_akira CC @NVIDIAEmbedded
Nov 23
DIY Robocars via Twitter
RT @luxonis: OAK-D PoE Autonomous Vehicle (Courtesy of zonyl in our Discord:
Nov 23
DIY Robocars via Twitter
RT @f1tenth: It is getting dark and rainy on the F1TENTH racetrack in the @LGSVLSimulator. Testing out the new flood lights for the racetra…
Nov 23
DIY Robocars via Twitter
RT @JoeSpeeds: Live Now! Alex of @IndyAChallenge winning @TU_Muenchen team talking about their racing strategy and open source @OpenRobotic…
Nov 20
DIY Robocars via Twitter
RT @DAVGtech: Live NOW! Alexander Wischnewski of Indy Autonomous Challenge winning TUM team talking racing @diyrobocars @Heavy02011 @Ottawa…
Nov 20
DIY Robocars via Twitter
Incredible training performance with Donkeycar
Nov 9
DIY Robocars via Twitter
RT @JoeSpeeds: Sat Nov 6 Virtual DonkeyCar (and other cars, too) Race. So bring any car? @diyrobocars @IndyAChallenge…
Oct 31
DIY Robocars via Twitter
RT @JoeSpeeds: @chr1sa awesomely scary to see in person as our $1M robot almost clipped the walls as it spun at 140mph. But it was also awe…
Oct 29
DIY Robocars via Twitter
RT @chr1sa: Hey, @a1k0n's amazing "localize by the ceiling lights" @diyrobocars made @hackaday! It's consistently been the fastest in our…
Oct 25
DIY Robocars via Twitter
RT @IMS: It’s only fitting that @BostonDynamics Spot is waving the green flag for today’s @IndyAChallenge! Watch LIVE 👉…
Oct 23
DIY Robocars via Twitter
RT @IndyAChallenge: Congratulations to @TU_Muenchen the winners of the historic @IndyAChallenge and $1M. The first autonomous racecar comp…
Oct 23
DIY Robocars via Twitter
RT @JoeSpeeds: 🏎@TU_Muenchen #ROS 2 @EclipseCyclone #DDS #Zenoh 137mph. Saturday 10am EDT @IndyAChallenge @Twitch
Oct 23