Measure tilt with the MPU-6050

Read the GY-521 MPU-6050 accelerometer and gyroscope over I2C and calculate how far the board is tilted.
  • Level:Intermediate
  • Duration: 45 min
  • Ages 12+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Measure tilt with the MPU-6050 – EEMENTOR STEM™ lesson

The MPU-6050 combines an accelerometer and a gyroscope on one tiny chip. Phones use the same kind of sensor to rotate the screen, and drones use it to stay level. In this lesson you read it over the I2C bus and calculate the tilt angle.

What you'll learn

  • How the I2C bus connects sensors with just two wires
  • How to read acceleration and rotation
  • How to calculate a tilt angle from gravity

What you need

  • UNO R3 board (or any Arduino-compatible board) and USB cable
  • GY-521 MPU-6050 module from the 45-in-1 kit
  • Breadboard
  • 4 jumper wires

Step 1: Install the libraries

In Tools → Manage Libraries, install Adafruit MPU6050. When asked, also install its dependencies (Adafruit Unified Sensor and Adafruit BusIO).

Step 2: Wiring

The GY-521 module has its own voltage regulator, so you can power it from 5V. On the UNO, the I2C pins are A4 (SDA) and A5 (SCL).

Breadboard wiring diagram: Measure tilt with the MPU-6050
Breadboard view. Check each connection against the wiring table below.

Build it step by step

Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.

Step 1 of 2: Power rails: 5V and GND from the UNO
Step 1. Power rails: 5V and GND from the UNO
Step 2 of 2: GY-521 MPU-6050 module: VCC → + rail, GND → − rail, SCL → A5, SDA → A4
Step 2. GY-521 MPU-6050 module: VCC → + rail, GND → − rail, SCL → A5, SDA → A4
Module pin UNO pin
VCC 5V
GND GND
SCL A5
SDA A4

Leave XDA, XCL, AD0 and INT unconnected.

FREE

Build this circuit in EEMENTOR Lab

Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.

The parts for this circuit laid out in EEMENTOR Lab, ready to wireBuild it yourself
  1. Wire the parts using the wiring table above. Pins with no wire show red.
  2. Press Run, move the tilt sliders under Selected, and watch the Serial monitor.
  3. Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.

EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab

Step 3: The sketch

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

Adafruit_MPU6050 mpu;

void setup() {
  Serial.begin(115200);
  if (!mpu.begin()) {
    Serial.println("MPU-6050 not found, check the wiring");
    while (true) delay(10);
  }
  mpu.setAccelerometerRange(MPU6050_RANGE_2_G);
  mpu.setGyroRange(MPU6050_RANGE_250_DEG);
  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.println("MPU-6050 ready");
}

void loop() {
  sensors_event_t accel, gyro, temp;
  mpu.getEvent(&accel, &gyro, &temp);

  float ax = accel.acceleration.x; // m/s²
  float ay = accel.acceleration.y;
  float az = accel.acceleration.z;

  // Tilt angles from the direction of gravity
  float pitch = atan2(ax, sqrt(ay * ay + az * az)) * 180.0 / PI;
  float roll = atan2(ay, az) * 180.0 / PI;

  Serial.print("Pitch: ");
  Serial.print(pitch, 1);
  Serial.print("°  Roll: ");
  Serial.print(roll, 1);
  Serial.print("°  Rotation Z: ");
  Serial.print(gyro.gyro.z, 2);
  Serial.println(" rad/s");
  delay(200);
}

Open the Serial Monitor at 115200 baud. Lay the breadboard flat (both angles near 0°), then tilt it forward, backward and sideways.

What you should see

Serial Monitor at 115200 baud showing pitch and roll near 0° when flat, about 30° pitch when the X end is lifted and −45° roll when tipped sideways.

How it works

A tilted board with gravity split into 4.9 along X and 8.5 along Z, the atan2 maths giving a 30.0° pitch, and the two I2C wires SDA on A4 and SCL on A5.

When the sensor is still, the only acceleration it feels is gravity, about 9.8 m/s² pointing down. As you tilt it, gravity spreads across the X, Y and Z axes, and atan2() turns that split into an angle. The gyroscope measures how fast the sensor is turning, which is useful for movement. The UNO talks to the chip over I2C: SDA carries the data and SCL the clock.

Try this next

  • Light the LED from the Arduino basics path when the tilt is more than 30°, like a spirit level alarm.
  • Plot pitch and roll in the Serial Plotter and watch them while you move the board.

Troubleshooting

  • "MPU-6050 not found": check that SDA is on A4 and SCL on A5, not swapped.
  • The angles drift or jump: fast movements add extra acceleration. Hold the board still for the most accurate tilt.