Arduino peripherals guide series
Getting useful motion data from the MPU-6050 device
Modern gadgets are mobile and can be "aware" of their motion with the use of motions sensors. There are integrated devices that combine (fuse) multiple motion sensors to produce accurate and reliable motion information for your gadget.
If you have played with motion sensors, like the MPU-6050 6-axis gyroscope and accelerometer device, you may have noticed that the raw data that you can read from them contain too much noise to be useful.
The values can fluctuate wildly, even when the device is sitting motionless on the floor.
This happens because the raw data that are generated by the device contains a significant amount of noise.
Clearly, using the raw data is not an option. There's not much you can do with them other than printing them to the serial monitor. Applications such as an autopilot for a drone are out of the question.
A simple solution: averaging
What you need to do is to process the raw data in order to extract useful information.
Processing raw motion sensor data can be done in a variety of ways.
At the bottom end of the complexity scale, you can use simple techniques such as smoothing or averaging the raw accelerometer values (also, see here). Such techniques can help clear up the noise and generate more reliable and useful motion information.
A better solution: fusion
The most reliable motion information come by combining multiple types of motion sensors.
The MPU6050 contains an accelerometer and a gyroscope.
There are various algorithms that take input from both the accelerometer and the gyroscope on the sensor to calculate reliable and useful motion information. Because these algorithms take input from multiple sensor types, we often refer to them as "fusion" algorithms.
You can then use the output of these algorithms to control a drone or or to navigate aircraft (see inertial navigation system).
This page contains an excellent tutorial on how to combine data from the gyro and accelerometer to derive useful values.
You may also want to read about the Kalman filter; this is an algorithm used widely for guidance, navigation, and control of all sorts of vehicles, from planes to spacecraft.
The problem is that these algorithms are hard to understand for most of us since they contain a lot of mathematics and tend to be complicated; they are also demanding in RAM because of the amound of calculations that they entail so that they are barely able to work on an Arduino Uno.
The MPU-6050 Digital Motion Processor
Luckily, there are motion sensing devices that solve this problem by running fusion algorithms internally. This means that your Arduino will be able to receive clean and reliable "fused" motion information from the module, instead of noisy motion data.
The MPU-6050 is such a device.
It contains an integrated Digital Motion Processor (DMP); this is a module that does all sorts of motion-related calculations inside the chip, very efficiently and quickly.
The MPU-6050 DMP is relatively sophisticated. It supports 3D motion processing and gesture recognition algorithms. It even reports temperature, and if you connect an external magnetometer, the DMP can also give you heading information. The external magnetometer can be connected to the MPU-60505 via an auxiliary I2C interface, so again you don't have to tie up the Arduino for this additional sensor.
An MPU-6050 library for Arduino
Even though using the DMP is not as straightforward as just getting the raw data (for some reason, this critical feature is not well documented in the datasheet), brilliant people have managed to create a library for the Arduino that makes use of it. It comes full of sample sketches with which you can play.
If you are serious about creating gadgets that make use of motion sensor data, you need to understand some of the mathematics involved, even if you use a DMP that does the grunt work for you. However, with the MPU-6050 and a good library, you will be able to create reliable motion-sensing gadgets without needing a degree in higher applied mathematics.
If you want to learn how to use the MPU-6050 with your Arduino, consider my course Arduino Step by Step Getting Serious, where I cover the sensor in Section 3.
New to the Arduino?
Arduino Step by Step Getting Started is our most popular course for beginners.
This course is packed with high-quality video, mini-projects, and everything you need to learn Arduino from the ground up. We'll help you get started and at every step with top-notch instruction and our super-helpful course discussion space.
Jump to another article
2. Basics of the TimerOne library
3. How to find device I2C address
4. Getting started with I2C on the Arduino
5. Using I2C: True digital to analog conversion on the Arduino Uno
6. How accurate are thermometer sensors?
7. MCP9808: an accurate thermometer module for your Arduino
8. Getting useful motion data from the MPU-6050 device
9. What to do with unused pins on an Atmega328P or Attiny85?
Done with the basics? Looking for more advanced topics?
Arduino Step by Step Getting Serious is our comprehensive Arduino course for people ready to go to the next level.
Learn about Wi-Fi, BLE and radio, motors (servo, DC and stepper motors with various controllers), LCD, OLED and TFT screens with buttons and touch interfaces, control large loads like relays and lights, and much much MUCH more.