Skip to main content

Arduino MPU-6050 Tutorial - Accelerometer & Gyroscope Motion Tracking Guide

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:
  • 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
This tutorial is written for beginners but includes deeper explanations to help you truly understand how the sensor works.

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
This means a single small module can measure both linear motion and rotational motion.

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
It can detect:
  • Tilt angle
  • Free fall
  • Vibration
  • Gravity direction
When the sensor is stationary, it still measures gravity (1g). This is why we can use it to measure tilt.

Gyroscope (Measures Rotation)

A gyroscope measures angular velocity (how fast something rotates).
It detects:
  • Rotation speed (degrees per second)
  • Direction of rotation
Important concept:
  • Accelerometer → tells you where gravity points
  • Gyroscope → tells you how fast you are rotating
The MPU-6050 combines both for better motion tracking.

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
Researchers even use filtering and sensor fusion (like Kalman filters) to improve accuracy and remove noise from IMU sensors.

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
The module communicates using I²C protocol, so only two data wires are required.

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 az

These represent acceleration in 3D space.

When flat on a table:
  • X ≈ 0
  • Y ≈ 0
  • Z ≈ +1g (gravity)

Gyroscope values

gx gy gz

These 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);

Now you will see the actual tilt angle in degrees.
Tilt the sensor and observe!

Understanding Sensor Noise

IMU sensors are not perfect.
Common issues:
  • Small random fluctuations
  • Drift over time
  • Sudden spikes
Researchers often use:
  • Moving average filters
  • Complementary filters
  • Kalman filters
to improve accuracy.
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
This sensor opens the door to countless robotics and IoT projects involving motion and orientation tracking.

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.

Can I use it with ESP32 or Raspberry Pi?

Yes, it uses I²C, so it works with most microcontrollers.

Comments

Popular posts from this blog

Arduino Serial Communication Beginner Guide (UART Explained)

Serial communication is the backbone of most Arduino projects. It allows your Arduino to talk to your computer, Bluetooth modules, Wi-Fi modules, sensors, and even other Arduinos. If you have ever used the Serial Monitor, you have already used serial communication - now you will understand how it really works. By the end of this guide, you will learn: What serial communication is What UART means How Arduino communicates with devices How to send and receive data How baud rate works Common mistakes beginners make What is Serial Communication? Serial communication is a method of sending data one bit at a time over a single wire. Instead of sending many bits simultaneously (parallel communication), serial sends bits sequentially: Example: The letter A in binary = 01000001 These bits are sent one after another very quickly. This method is: Simple Reliable Perfect for microcontrollers What is UART? UART stands for: Universal Asynchronous Receiver Transmitter It is the hardware ins...

How to Connect HC-06 Bluetooth Module with Arduino (Step-by-Step Beginner Guide)

Bluetooth communication is one of the easiest ways to make Arduino projects wireless. Once you understand how to use a Bluetooth module, you can build robots, home automation systems, and smartphone-controlled devices. In this tutorial, you will learn how to interface the HC-06 Bluetooth module with Arduino and send data wirelessly using your phone.    By the end of this guide, you will be able to: Understand how the HC-06 Bluetooth module works Configure the module using AT commands Safely connect it to an Arduino Send and receive serial data over Bluetooth Build the foundation for wireless Arduino projects Watch the Project Overview What is the HC-06 Bluetooth Module? The HC-06 is a Bluetooth-to-Serial module designed to add wireless communication to microcontrollers such as Arduino. It uses Bluetooth 2.0 (SPP - Serial Port Protocol), which means it behaves like a wireless serial cable. Whatever is sent from the phone appears in Arduino’s Serial Monitor and vice versa. ...

How to Use SoftwareSerial on Arduino (Connect Multiple Serial Devices)

In the previous tutorial, you learned how Arduino uses hardware serial communication through pins 0 (RX) and 1 (TX). That built-in serial port is mainly used to communicate with your computer through the USB cable and Serial Monitor . But here is the problem beginners quickly face: Many popular modules use serial communication, such as: Bluetooth modules ( HC-05 / HC-06 ) GPS modules GSM modules Wi-Fi modules ( ESP8266 ) Arduino Uno has only one hardware serial port. So how can we connect these modules while still using the Serial Monitor? The solution is SoftwareSerial . In this tutorial you will learn how to create additional serial ports using normal digital pins. By the end, your Arduino will be able to talk to multiple serial devices. What is SoftwareSerial? SoftwareSerial is a special Arduino library that allows digital pins to behave like RX and TX pins. This means Arduino can create extra serial ports using software instead of hardware. Hardware Serial vs Software Seri...