×
Robu.in
MACFOS INSTALL NOW
Free - On the App Store
! 1800 266 6123 | Customer Support
My Orders | Track your order | My Account # | !
| | " | # | $
Home " Blogs " How to Interface MPU6050 with the A
How to Interface MPU6050
with the Arduino
$ Blogs, IMU % 0 comments & 5668 Views '
Friday, February 19, 2021
In this blog, we will learn about the MPU6050
sensor. It calculates the angle and motion values
of the accelerometer and the gyroscope. With
this, we will also see its wide application range.
Introduction
Have you ever wonder how your mobile screen adjusts
automatically in a frame when you move your smartphone
vertically or horizontally? We can achieve this task with the
help of IMU(Inertial Measurement Unit) MPU6050. In this
tutorial, we will talk about MPU6050 and its interfacing
with Arduino. MPU6050 has been developed by
InvenSense company. It is designed for obtaining values of
accelerometer and gyroscope.
What is MPU6050?
MPU6050 is an IMU device that stands for Inertial
Measurement Unit. It is a six-axis motion tracking device
that calculates a three-axis accelerometer and three-axis
gyroscope data. The biggest advantage of this board it
comes with a digital motion processor. The significance of
DMP(Digital motion processor) is, it performs very complex
calculations/operations from its side, thus freeing up the
microcontroller’s work. It provides motion data like roll,
pitch, yaw, angles, landscape, and portrait sense, Etc.
It is very precise while converting analog data into digital
data bits because it has 16 bits assigned for each channel.
Digital Motion Processor or the DMP(Digital Matrix
Processor) is an embedded processor that can reduce the
host processor’s computational load by acquiring and
processing data from Accelerometer, Gyroscope, and an
external Magnetometer.
We will look towards some of its main features like:
3- axis gyroscope
3- axis accelerometer
16-bit ADC conversion for each channel
1024 bit FIFO buffer
Digital output temperature sensor
Integrated Digital Motion Processor
Inbuilt temperature sensor
Pinout
VCC: This pin is used to provide power(5v /3.3v)
GND: As usual for providing 0v
SDA: This pin is used for obtaining data serially from
the sensor
SCL: This pin is used for serial clock input
XDA: This is used as an SDA for configuring and
reading from an external sensor(Optional)
XCL: This is used as an SCL for configuring and
reading from external sensor (Optional)
AD0: This is I2c slave address Bus, (least significant
bit)
INT: This one is an Interrupt pin for an indication of
data ready
How does it Work?
How does the Accelerometer work?
An accelerometer works on the principle of the
piezoelectric effect. Consider a cuboidal box with a ball
inside it. If we move or tilt the box ball inside the box, it will
fall on the box’s walls because of gravity. Similarly, IMU like
MPU6050 also contains such MEMS (micro-
electromechanical systems), which exactly work just like
the cuboidal box with a ball inside it.
When the device/box is tilted, a ball inside the box falls
upon the piezoelectric walls. Also, the analog data is
captured from that wall’s channel and proceeds into digital
data. It is often how an accelerometer works, and its
output is so precise because it has 16-bit ADC present for
every channel.
How does the Gyroscope work?
It works on the principle of Coriolis acceleration. Imagine
that there's a fork-like structure that's in an exceedingly
constant back-and-forth motion. Whenever you try to tilt
this arrangement, the crystals experience a force within
the direction of inclination. It often causes a result of the
inertia of the moving fork. The crystals thus produce a
current in consensus with the piezo effect, and the current
is amplified.
Interfacing with the Arduino
As MPU6050 is an I2C communication device, the
connections with the Arduino are pretty simple. The
following circuit diagram will show you how the
connections are made with Arduino.
Connections of MPU6050 with Arduino is as follows:
Arduino MPU6050
5v/3v VCC
GND GND
A5/ SCL pin SCL
A4/SDA pin SDA
pin 2 INT
Programming
To obtain the data from MPU6050, you will need to install
the wire.h library.
You can download both the libraries from the following link:
MPU6050
Wire.h library
From this code, we will get three basic parameters called
Roll, Pitch, Yaw. Basically, what do these three things
mean? When you are going through a car, you can measure
its motion as forward, backward, left, and right. But, it will
not be considered the same scenario when it comes to
drones or flying vehicles. Flying control boards have
different terminologies like yaw, pitch, and Rolls.
The axis that runs front to the back of the drone is
called the roll axis. The rotation around this axis is
called roll motion.
The axis that runs left to the right of the drone is
called the pitch axis. The rotation around this axis is
called pitch motion.
The axis that runs top to the bottom of the drone is
called the yaw axis. The rotation around this axis is
called the yaw motion.
In the short words, we can say that,
Rotation around the front-to-back axis is called roll.
Rotation around the side-to-side axis is called pitch.
Rotation around the vertical axis is called yaw.
For explaining the things, I will show you an image for a
better understanding.
#include <Wire.h>
const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ;
float GyroX, GyroY, GyroZ;
float accAngleX, accAngleY, gyroAngleX,
gyroAngleY, gyroAngleZ;
float roll, pitch, yaw;
float AccErrorX, AccErrorY, GyroErrorX,
GyroErrorY, GyroErrorZ;
float elapsedTime, currentTime, previousTime;
int c = 0;
void setup() {
Serial.begin(19200);
Wire.begin(); // Initialize
comunication
Wire.beginTransmission(MPU); // Start
communication with MPU6050 // MPU=0x68
Wire.write(0x6B); // Talk to
the register 6B
Output
From the above code, we have successfully got the Data
Roll/ pitch/ Yaw.
Roll, pitch, yaw are the manipulated data obtained by
gyroscopic angles of MPU6050. If you want to calculate
basic accelerometer and gyroscopic values, you can
receive them in some steps. For that, first, you have to
install the library MPU6050_tockn.h.
After downloading this library, install the library with the
following path.
Browse the library from its path directory. It is the process
of adding the library to MPU6050. After including the
library to Arduino IDE, you can select the code from the
path.
Once the MPU-6050 library is added to Arduino IDE, we
have a list of examples to choose from, like
GetAllData
GetAngle
Open code GetAllData.
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu6050(Wire);
long timer = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
void loop() {
mpu6050.update();
if(millis() - timer > 1000){
Uploading this code into Arduino the MPU6050 will give
you the values. You can see the output in the following
image:
Output for Accelerometer and
Gyroscope
You will get complete data of the accelerometer and
gyroscope from the above output, including current
temperature values.
Applications
Here are some applications of the MPU6050 sensor:
3D remoter controller
In 3D mice controller
In wearable health-tracking, fitness-tracking devices
In drones and quadcopters, MPU6050 is used for
position control.
Used in controlling Robotic Arm
Hand gesture control devices
In Self-balancing robot
In Humanoid robot for tilt, rotation, and balancing
In smartphones for adjusting the frame
Used in gimbals system for stabilization on drones
Final Words
In this tutorial, it has been concluded that the MPU6050
chip is a highly applicable device in many systems. It is
used in wide applications and is considered the most
crucial parameter of the system. Without this sensor, we
can not make most of the complicated processes in
robotics and the embedded fields. If you have any queries
related to any doubt, you can drop them in the comment
section.
& % ' (
About Robu
India's biggest robotics E-commerce company. Robu
deals with Arduino, Raspberry Pi, Sensors, Drone
parts, 3D printer parts, E-bike accessories and
Electronics components etc. Click here to explore
range of Robotics products available at Robu. Also,
do not forget to follow us on Facebook, Instagram
and YouTube because we are constantly
contributing to the community by creating content
around Arduino, Raspberry Pi, Drones, Sensors etc.
Related Posts
Interfacing ACS712 Current Sensor with
Arduino - Step by Step Guide to
Measure Current
Thursday January 28, 2021 & 6942 Views
The article covers the definition of the current sensor, it's
working and interfacing of the current sensor with Arduino. ...
Read More
Sheet Metal Design Basics in 2021 –
Complete List of Things That You Must
Know
Tuesday May 11, 2021 & 3745 Views
Casing and enclosures made from Sheet Metals are used in a
wide variety of applications. From home appliances to the
aerospace industry, sheet metals are found everywhere! This
proves that... Read More
Interfacing of Piezoelectric Sensor with
Arduino
Friday June 5, 2020 & 12438 Views
The tutorial, covers the working and interfacing of
piezoelectric sensor with Arduino.... Read More
Original Arduino Nano Every:
Empowering Creativity in a Compact
Package
Tuesday June 6, 2023 & 716 Views
In the exciting realm of electronics and microcontrollers, the
Arduino brand has become synonymous with innovation and
creativity. Whether you’re a passionate hobbyist or a
seasoned professional... Read More
Leave a comment
Your email address will not be published. Required fields
are marked *
Comment *
Name *
Email *
Website
أنا لست برنامج روبوت
reCAPTCHA
البنود- الخصوصية
Post Comment
Subscribe to our Newsletter
First Name First Name Email
Email id
Subscribe me
Information !
My Account !
Why Choose us !
Policies !
Download Our App
google-play-badge
! % " # $
Got Questions ? Call us
between 9:15 AM to 6:15 PM
Monday-Saturday
1800 266 6123,
020 68197600