MPU6050 with Arduino – Accelerometer and Gyroscope

In this post we will go over how to use the MPU6050 accelerometer and gyroscope module (GY-521) with Arduino. We’ll focus on basics, module pins, how to connect to Arduino, programming in Arduino IDE, and testing. We will also show how to use the sensor along with processing IDE to visualize the readings from the accelerometer / gyroscope.

MPU6050 test with Processing IDE

What is an MPU6050

The MPU6050 is a sensor module with a 3-axis accelerometer and a 3-axis gyroscope, meaning it can be used to measure acceleration in the X, Y, and Z axes, and also measure rotational velocity along those same axes.

What is a gyroscope

A gyroscope sensor is a used to measure angular velocity. When combined with an accelerometer sensor, we can obtain relatively accurate results in regards to absolute angular position.

What is an accelerometer

An accelerometer is a sensor used to measure acceleration (rate of change of an body’s velocity).

MPU6050 pins and axes

MPU6050 Specs, Pins, and Connection to Arduino

Specs:

Accelerometer Ranges: ±2g, ±4g, ±8g, ±16g
Gyroscope Range (deg/s): ±250, ±500, ±1,000, ±2,000
Input Voltage: 3.3V- 5.0V

Pins:

VCC – Power Input pin
GND – Ground pin
SCL – I2C Clock pin
SDA – I2C Data pin
XDA – External I2C Data pin
XCL – External I2C Clock pin
ADO – I2C address change pin
INT – Interrupt pin

MPU6050 pins
MPU6050 connection to Arduino UNO

Click to enlarge

Required Arduino Libraries

To use the MPU6050 sensor with Arduino, you will need to install the MPU6050 library.

Installing the MPU6050 Library

To install the MPU6050 library go to the top menu, click on “Sketch”, then “Include Library”, “Manage Libraries…”. Then type “MPU6050”, then click install on the row for the MPU6050 library.

Click to enlarge

Arduino IDE install MPU6050 library

Click to enlarge

Arduino Code

In this section we will go over 2 pieces of code. One is used for a stand-alone application to obtain the accelerations and roll/pitch/yaw angles. The other is used to visualize the angular orientation of an object using Processing IDE.

Stand-alone application

The code below was slightly modified from an example within the MPU6050 library. If you want to access this example yourself (after installing the library), follow these steps: Click on “File” on the top menu, then go to “Examples”, then under “Examples from Custom Libraries” go to “MPU6050”, then click on “MPU6050_DMP6”. This will open the example file that was used for the code below. The only difference between the code below and the example is that I uncommented line #110.

This allows the code to output the sensors acceleration relative to the sensor’s reference frame (the x direction stays consistent from the sensor’s point of view). If you need the acceleration adjusted to the world frame of reference then don’t uncomment line #110. Instead uncomment line #116.

MPU6050 example in Arduino IDE

Click to expand

If you just want to copy and paste the code to test your MPU6050, then click on the gray section below to expand the Arduino code.

Stand-alone application output

On the right we have a screenshot of the output in the Arduino IDE serial monitor after running the code to test the MPU6050 based on the code above showing angular orientation (Yaw/Pitch/Roll) and acceleration (relative to the MPU6050’s orientation). I overlaid an image of the sensor and reference axis for reference.

Angular Orientation is shown as ypr (Yaw, Pitch, Roll) in degrees relative to the starting position of the sensor’s X, Y, and Z axes.

Acceleration is shown as a number that can go as high as 32,750, where 32,750 is the maximum acceleration. The meaning of the 32,750 value will depend on the chosen acceleration limit. Options are 2g, 4g, 8g, and 16g, where g = 9.81 m/s2 (or 32.19 ft/s2). The default setting for the accelerometer is 2g. Meaning 1g = 16,375. The output will be acceleration along the sensor’s x-axis, then the y-axis, finally the z-axis.

MPU6050 output in Arduino IDE

Click to enlarge

3D Model Visualization in Processing using values from MPU6050 with Arduino.

In this section we will show you how to do some 3D Model Visualization using the outputs from the MPU6050 so you can get something similar to the GIF shown on the right!

In order to do this, you will need to install Processing IDE, which will allow you to do the actual 3D visualization.

MPU6050 test with Processing IDE

Installing Processing IDE

To download Processing IDE, go to to the following LINK and click the download option that corresponds to your computer’s operating system. Once you do, a ZIP file will start being downloaded. Once downloaded, extract the files from the ZIP file. Next, you will need to install a Library for Processing IDE.

Install Processing IDE

Installing Toxic Library for Processing IDE

To install the Toxic library within Processing IDE, follow these steps:

  • Open Processing IDE – Go to the folder that was extracted from the ZIP File and open it, within this folder, you will see a file called “processing.exe”, open it.
  • Click Add Library – Once you are in Processing IDE:
    • Go to the top menu.
    • Go to Sketch.
    • Go Import Library.
    • Then click on Add Library.
  • Search and Install Toxic Library – Once the Contributions Manager opens:
    • Type “Toxic” in the search library.
    • Select the Toxiclibs library.
    • At the bottom of the Contributions Manager click “Install”. It will first download and then begin installing automatically.
Processing IDE install library

Click to enlarge

Click to enlarge

Arduino Code for 3D Visualization

You will need to use the Arduino code below to enable sending the MPU6050 data to Processing IDE instead of sending it to the Serial Monitor.

The code below was slightly modified from an example within the MPU6050 library. If you want to access this example yourself (after installing the library), follow these steps within Arduino IDE: Click on “File” on the top menu, then go to “Examples”, then under “Examples from Custom Libraries” go to “MPU6050”, then click on “MPU6050_DMP6”. This will open the example file that was used for the code below. The only difference between the code below and the example is that I uncommented line #120.

You also need to make sure that lines 90, 96, 103, 110, and 116 are all commented out with a “//”.

If you just want to copy and paste the code to test your MPU6050, then click on the gray section below to expand the Arduino code.

Code for Processing IDE to run with Arduino MPU6050 code

Once you have deployed the code above in your Arduino, go back to Processing IDE and go to File, then Open. Go to the folder where the MPU6050 Arduino library was installed. Once you get to the folder, open the Processing file with the name “MPU Teapot”.

Once you have deployed the code above in your Arduino, go back to Processing IDE and go to File, then Open. Go to the folder where the MPU6050 Arduino library was installed. Once you get to the folder, open the Processing file with the name “MPU Teapot”.

To find this file, go to the folder where all Arduino main files are located, then follow this path: Documents\Arduino\libraries\MPU6050\examples\MPU6050_DMP6\Processing\MPUTeapot.

If you have trouble finding the location of the main Arduino folder, within Arduino, click on File, then click on Preferences. Go to this folder and follow this path within Processing IDE (after clicking File –> Open).

Once you open the file, you will see the code displayed. There is no need to make any changes, but please do make sure that the Serial Monitor in Arduino IDE is closed, or simply close Arduino IDE altogether. Next, proceed to click the Run button in Processing IDE. Once you click, wait approximately 10 seconds before the 3D model shows up on a separate screen. By default, you will see the 3D model of an airplane. For display purposes, I went ahead and 3D printed a similar looking airplane object and attached an MPU6050 sensor to it. Below is a quick demonstration (same one I showed earlier in this post):

Main Arduino folder

Click to enlarge

Run Processing IDE code

Click to enlarge

Components used in this example

*As an Amazon & Ebay Associate I earn from qualifying purchases.

ComponentLink
Arduino UNOhttps://ebay.us/veZdKX
https://amzn.to/3uYVAMC
MPU-6050 Accelerometer/Gyroscope (GY-521)https://ebay.us/P5pZOv
https://amzn.to/3aDP4Dz

Leave a Reply

Your email address will not be published. Required fields are marked *