MPU-6050 6-Axis IMU Sensor

Complete Tutorial: Motion Sensing with Gyroscope & Accelerometer

📖 15 min read 🔧 Intermediate 💻 Arduino / ESP32

1. Introduction

The MPU-6050 is a popular 6-axis Inertial Measurement Unit (IMU) that combines a 3-axis gyroscope and a 3-axis accelerometer on a single chip. Made by InvenSense (now TDK), it's widely used in drones, robots, game controllers, and motion-tracking applications.

This sensor can detect linear acceleration (movement) and angular velocity (rotation), making it perfect for determining orientation, detecting gestures, balancing robots, or stabilizing camera gimbals.

📦 What's Inside the MPU-6050

  • 3-Axis Accelerometer: Measures linear acceleration (g-force)
  • 3-Axis Gyroscope: Measures angular velocity (rotation speed)
  • Temperature Sensor: For internal calibration
  • Digital Motion Processor (DMP): Onboard processing for sensor fusion

2. Technical Specifications

Parameter Specification
Operating Voltage 2.375V – 3.46V (module: 3.3V – 5V)
Interface I2C (up to 400kHz) / SPI
Gyroscope Range ±250, ±500, ±1000, ±2000 °/s
Accelerometer Range ±2g, ±4g, ±8g, ±16g
I2C Address 0x68 (AD0=LOW) or 0x69 (AD0=HIGH)
ADC Resolution 16-bit
Current Consumption 3.9mA (normal mode)

Understanding the Axes

The MPU-6050 measures motion in 3 dimensions:

  • X-axis: Left/Right movement or roll rotation
  • Y-axis: Forward/Backward movement or pitch rotation
  • Z-axis: Up/Down movement or yaw rotation

💡 Accelerometer vs Gyroscope

Accelerometer: Measures linear acceleration including gravity. When stationary, it reads 1g on the Z-axis (9.8 m/s²).

Gyroscope: Measures rotation speed (degrees per second). When stationary, it reads 0 on all axes.

3. Wiring Guide

MPU-6050 Pin Description ESP32 Arduino Uno
VCC Power (3.3V-5V) 3.3V 5V
GND Ground GND GND
SCL I2C Clock GPIO 22 A5
SDA I2C Data GPIO 21 A4
AD0 Address Select GND (for 0x68) GND (for 0x68)
INT Interrupt (optional) Any GPIO Any Digital Pin
MPU-6050 wiring with ESP32

Figure 1: MPU-6050 I2C wiring with ESP32

4. Project: Motion & Orientation Monitor

Required Library

Install Adafruit MPU6050 and Adafruit Unified Sensor via Arduino Library Manager.

Complete Code

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup() {
    Serial.begin(115200);
    Serial.println("MPU-6050 Motion Monitor");

    if (!mpu.begin()) {
        Serial.println("ERROR: MPU-6050 not found!");
        while (1) delay(10);
    }

    // Configure sensor ranges
    mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
    mpu.setGyroRange(MPU6050_RANGE_500_DEG);
    mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

    Serial.println("MPU-6050 Ready!");
    delay(1000);
}

void loop() {
    sensors_event_t a, g, temp;
    mpu.getEvent(&a, &g, &temp);

    // Accelerometer (m/s^2)
    Serial.print("Accel X:"); Serial.print(a.acceleration.x, 2);
    Serial.print(" Y:"); Serial.print(a.acceleration.y, 2);
    Serial.print(" Z:"); Serial.print(a.acceleration.z, 2);

    // Gyroscope (rad/s)
    Serial.print(" | Gyro X:"); Serial.print(g.gyro.x, 2);
    Serial.print(" Y:"); Serial.print(g.gyro.y, 2);
    Serial.print(" Z:"); Serial.print(g.gyro.z, 2);

    // Temperature
    Serial.print(" | Temp:"); Serial.print(temp.temperature, 1);
    Serial.println(" C");

    delay(100);
}

Tilt Detection Example

// Simple tilt detection using accelerometer
void loop() {
    sensors_event_t a, g, temp;
    mpu.getEvent(&a, &g, &temp);

    // Calculate tilt angles (in degrees)
    float pitch = atan2(a.acceleration.y, a.acceleration.z) * 180.0 / PI;
    float roll = atan2(-a.acceleration.x, 
                  sqrt(a.acceleration.y*a.acceleration.y + 
                       a.acceleration.z*a.acceleration.z)) * 180.0 / PI;

    Serial.print("Pitch: "); Serial.print(pitch, 1);
    Serial.print("° | Roll: "); Serial.print(roll, 1);
    Serial.println("°");

    delay(100);
}

5. Common Applications

  • Self-balancing robots: Use accelerometer + gyro for balance control
  • Drone stabilization: Flight controller feedback loop
  • Gesture recognition: Detect hand movements or shakes
  • Step counter: Detect walking patterns
  • Gaming controllers: Motion-based input
  • Camera gimbals: Stabilize video footage

6. Troubleshooting

MPU-6050 not detected

  • Check I2C wiring (SDA, SCL)
  • Verify I2C address (0x68 or 0x69)
  • Run I2C scanner sketch

Readings are drifting

  • Gyroscope naturally drifts - use sensor fusion (complementary or Kalman filter)
  • Allow sensor to warm up before calibrating

Noisy readings

  • Lower the filter bandwidth setting
  • Apply software low-pass filter

7. Summary

  • 6-axis IMU: 3-axis accelerometer + 3-axis gyroscope
  • I2C interface (address: 0x68 or 0x69)
  • Configurable ranges for different applications
  • Use sensor fusion for accurate orientation