Motion sensing is one of the most important capabilities in modern electronics. From smartphones and drones to robotics and gaming controllers, motion sensors allow devices to understand movement, orientation, vibration, and rotation.
In this tutorial, you will learn how to use the MPU-6050 6-axis motion tracking sensor with Arduino. By the end of this guide, you will be able to:
It detects:
Sketch → Include Library → Manage Libraries
Search and install:
MPU6050 by Electronic Cats
(or any compatible MPU6050 library)
Upload the code and open Serial Monitor.
Move the sensor and watch the values change.
These represent acceleration in 3D space.
When flat on a table:
These represent rotation speed.
If the sensor is not rotating → values ≈ 0.
Add this to loop():
Now you will see the actual tilt angle in degrees.
Tilt the sensor and observe!
Common issues:
This is called sensor fusion and is the foundation of advanced robotics.
In this tutorial, you will learn how to use the MPU-6050 6-axis motion tracking sensor with Arduino. By the end of this guide, you will be able to:
- Understand how accelerometers and gyroscopes work
- Read motion data from the MPU-6050
- Measure tilt and rotation
- Build the foundation for robotics, drones, wearables, and gesture-based projects
What is the MPU-6050?
The MPU-6050 is a 6-axis Inertial Measurement Unit (IMU) that combines:- 3-axis accelerometer
- 3-axis gyroscope
- Digital Motion Processor (DMP)
- Built-in temperature sensor
- I²C communication interface
Why this sensor is popular
The MPU-6050 is widely used because it is:- Very affordable
- Easy to interface via I²C
- Small and lightweight
- Accurate enough for most DIY and robotics projects
Understanding Accelerometer vs Gyroscope
Understanding these two sensors is essential.Accelerometer (Measures Tilt & Movement)
An accelerometer measures acceleration force in three directions:- X axis → left/right
- Y axis → forward/backward
- Z axis → up/down
- Tilt angle
- Free fall
- Vibration
- Gravity direction
Gyroscope (Measures Rotation)
A gyroscope measures angular velocity (how fast something rotates).It detects:
- Rotation speed (degrees per second)
- Direction of rotation
- Accelerometer → tells you where gravity points
- Gyroscope → tells you how fast you are rotating
Applications of MPU-6050
This sensor is used in many real-world systems:- Self-balancing robots
- Drones and quadcopters
- Gesture controllers
- VR/AR controllers
- Step counters and fitness devices
- Smart alarm and theft detection systems
Required Components
- Arduino Uno
- MPU-6050 Module
- Breadboard
- Jumper wires
MPU-6050 Pinout
| Pin | Function |
|---|---|
| VCC | 3.3V-5V power |
| GND | Ground |
| SCL | I²C Clock |
| SDA | I²C Data |
| XDA | Auxiliary SDA |
| XCL | Auxiliary SCL |
| AD0 | I²C address select |
| INT | Interrupt output |
For basic projects, we only need 4 pins.
Wiring (Arduino Uno)
- VCC - 5V
- GND - GND
- SDA - A4
- SCL - A5
- INT - D2
Installing Required Libraries
Open Arduino IDE:Sketch → Include Library → Manage Libraries
Search and install:
MPU6050 by Electronic Cats
(or any compatible MPU6050 library)
Basic Arduino Code
This program reads raw accelerometer and gyroscope data.
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
if (mpu.testConnection())
Serial.println("MPU6050 connected!");
else
Serial.println("Connection failed");
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
Serial.print("Accel: ");
Serial.print(ax); Serial.print(" ");
Serial.print(ay); Serial.print(" ");
Serial.print(az); Serial.print(" ");
Serial.print("Gyro: ");
Serial.print(gx); Serial.print(" ");
Serial.print(gy); Serial.print(" ");
Serial.println(gz);
delay(500);
}
Upload the code and open Serial Monitor.
Move the sensor and watch the values change.
Understanding the Output
You will see two groups of values:Accelerometer values
ax ay azThese represent acceleration in 3D space.
When flat on a table:
- X ≈ 0
- Y ≈ 0
- Z ≈ +1g (gravity)
Gyroscope values
gx gy gzThese represent rotation speed.
If the sensor is not rotating → values ≈ 0.
Calculating Tilt Angle
We can calculate tilt using accelerometer data.Add this to loop():
float Ax = ax;
float Ay = ay;
float Az = az;
float roll = atan2(Ay, Az) * 180/PI;
float pitch = atan2(-Ax, sqrt(Ay*Ay + Az*Az)) * 180/PI;
Serial.print("Roll: ");
Serial.print(roll);
Serial.print(" Pitch: ");
Serial.println(pitch);
Tilt the sensor and observe!
Understanding Sensor Noise
IMU sensors are not perfect.Common issues:
- Small random fluctuations
- Drift over time
- Sudden spikes
- Moving average filters
- Complementary filters
- Kalman filters
This is called sensor fusion and is the foundation of advanced robotics.
Troubleshooting Guide
Sensor not detected
Check:- SDA → A4
- SCL → A5
- Correct library installed
Random noisy values
This is normal. Use filtering if needed.Angles drifting
Gyroscope drift is expected. Combine gyro + accelerometer using complementary filter.Project Ideas Using MPU-6050
Now that you can read motion data, try:- Self-balancing robot
- Gesture-controlled robot arm
- Fall detection system
- Step counter
- Anti-theft alarm
- Drone stabilization
Conclusion
In this tutorial you learned how to:- Interface the MPU-6050 with Arduino
- Understand accelerometer and gyroscope concepts
- Read motion data from the sensor
- Calculate tilt angles
- Understand sensor noise and filtering
FAQ
Is MPU-6050 good for beginners?
Yes. It is one of the best starter IMU sensors.Can it measure position?
Not accurately. It measures acceleration and rotation, not precise position.Why combine accelerometer and gyroscope?
- Accelerometers measure tilt well but react to vibration.
- Gyroscopes measure rotation well but drift over time.
- Combining both gives better accuracy.



Comments
Post a Comment