MPU6050 Arduino- This is my first getting started tutorial on the GY-521 MPU6050 six degrees of freedom 3-Axis Gyroscope and Accelerometer Module. As usual before making any intermediate or advanced level project, first I am going to explain the basics that every beginner should know. In this tutorial, I will try to keep things simple so that you can easily understand the whole mechanism.
In this episode, I will cover
- MPU6050 GY-521 technical specifications
- MPU6050 Module Pinout
- MPU6050 Circuit diagram explanation
- It’s interfacing with the Arduino
- Libraries which you will need for this Module.
- Programming to display the values on the Serial Monitor and finally number
Without any further delay, let’s get started!!!
The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:
MPU6050 Module Best Price: https://amzn.to/31MUeWF
Arduino Uno: https://amzn.to/2tVz1vu
Mega 2560: https://amzn.to/2ze1kdu
LM7805 Voltage Regulator: https://amzn.to/2IZEl5x
330 ohm resistors pack: https://amzn.to/2NtlAeh
5v 2A adaptor: https://amzn.to/2GsNfcw
2n2222 npn transistors: https://amzn.to/2EmXsIb
10k Resistor: https://amzn.to/2KTd6OW
lm7805 Voltage Regulator: https://amzn.to/2IZEl5x
330-ohm resistors pack: https://amzn.to/2NtlAeh
LDR pack: https://amzn.to/2m2khGR
female DC power jack socket: https://amzn.to/2KEo1gt
470 uf capacitors: https://amzn.to/2xDCOzf
5×7 cm vero board: https://amzn.to/2OHGbvn
female headers: https://amzn.to/2zqmtiJ
connection wires: https://amzn.to/2DpBuW7
Super Starter kit for Beginners: https://amzn.to/2KJvmKG
Jumper Wires: https://amzn.to/2KMoVXs
Bread Board: https://amzn.to/2MS4q8X
12v Adaptor: https://amzn.to/2Ntr6h1
PCB plate: https://amzn.to/2IUwpCt
Variable Supply: https://amzn.to/2MT4Qfj
Digital Multimeter: https://amzn.to/2Nvft9i
Vero Board / strip board: https://amzn.to/2MTf9jD
Soldering iron kit: “best” You guys should definitely purchase this: https://amzn.to/2zfoNuJ
Solder wire: https://amzn.to/2ufUMWf
Wire Stripper: https://amzn.to/2KOqxfU
wire cutter: https://amzn.to/2ucIq14
PCB small portable drill machine: https://amzn.to/2Nu62XF
Please Note: these are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!
About the GY-521 MPU6050 Module:
This is the GY-521 MPU6050 6DOF 3-Axis Gyroscope and Accelerometer Module. The MPU6050 communicates with the Arduino through the I2C protocol. The MPU6050 always acts as a slave to the Arduino with the SDA and SCL pins connected to the I2C bus. This module needs 3 to 5 volts. This Module can be used in a hand gesture controlled Robot, wheelchair, earthquake detection system, Self-balancing Robot, the self-balancing platform for a camera, Segway, etc.
As you can see clearly this module has a total of 8 Pins which are clearly labeled. Out of these 8 pins I will be using only the VCC, GND, SCL, SDA, and INT. Let’s have a look at the connection diagram.
MPU5060 Arduino Circuit Diagram:
This schematic is designed in Cadsoft eagle 9.1.0 version. if you want to learn how to make a schematic and PCB then watch my tutorial given below.
The circuit diagram is very simple, as you can see VCC is connected with 3.3 volts, GND is connected with the GND, SCL and SDA are connected with the Arduino’s Analog pins A5 and A4, while the INT pin of the MPU6050 module is connected with pin number 2 of the Arduino. The MPU6050 Eagle library can be downloaded which is given below.
Download MPU6050 Eagle Library: gy-521
MPU5060 Interfacing with Arduino:
The MPU6050 Module can be interfaced with the Arduino using Male to Female type jumper wires. The orange wire is connected with the VCC, Blue wire is connected with the GND, Green wire is connected with the SCL, Yellow wire is connected with the SDA, and White wire is connected with the INT pin of the MPU6050 Module.
Connect the Orange wire which is the VCC wire with 3.3 volts of the Arduino, connect the Blue wire with the GND, Connect the Green wire which is the SCL wire with the Arduino’s Analog pin A5, connect the SDA wire with the Analog pin A4 and finally connect the INT pin of the module with pin number 2 of the Arduino.
MPU5060 Arduino Programming:
Before you start the programming, first of all, make sure you download the following libraries
hartway_digital and wire.h.
the hartway_digial consists of the MPU6050.h and I2Cdev.h header files.
Download hartway_digital Library: hartway_digital
Download Wire Library: Wire
After you download the libraries zip folders then simply extract the folders, copy the folders and paste them into my document > Arduino’s > Libraries folder. If your Arduino IDE is open close it and open it again. Each time you add a new library you should reopen the Arduino IDE in order to load the library files.
vcc = 3.3v
Gnd = gnd
int = 2
scl = a5
sda = a4
int16_t ax, ay, az;
int16_t gx, gy, gz;
Serial.println(mpu.testConnection() ? "Connected" : "Connection failed");
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
valax = map(ax, -17000, 17000, 0, 255) ;
valay = map(ay, -17000, 17000, 0, 255);
valaz = map(az, -17000, 17000, 0, 255);
While the Arduino is connected with the Laptop, first Upload the program and then open the Serial Monitor and make sure you select 38400 as the baud rate.
In the demonstration video given below, you will see that the ax, ay, and az value changes as I rotate the MPU6050 Module.
With connections like these, it’s really difficult to handle this sensor, and later it will be really difficult to calibrate this sensor, as in my upcoming tutorials I am going to use this sensor in a Self-balancing bicycle, Segway, Driver drowsiness detection, Wheelchair, and Hand Gesture controlled Robot.
For the easy calibration, I am going to fix this sensor on a Vero Board.
You can watch the soldering in the video given below.
Now you can see the MPU6050 Sensor is sandwiched between the Vero Boards and now it’s really easy to handle.
The pins of the MPU650 are soldered with the female headers. Now I will need male to male type jumper wires to connect this module with the Arduino Uno or Mega.
Note: Make sure you double-check the short circuit after you complete soldering, and keep the Soldering Iron away from Kids.
Let me again connect this module with the Arduino and see if it has any improvement.
As you can see the sensor is almost calibrated, the value of ax and ay are 127, these values fluctuate between 127 and 128 which is not a big deal.
Now I have more control over this sensor and now I can easily use this in my upcoming projects.
Watch MPU5060 Arduino Video Tutorial: