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).

| Module pin | UNO pin |
|---|---|
| VCC | 5V |
| GND | GND |
| SCL | A5 |
| SDA | A4 |
Leave XDA, XCL, AD0 and INT unconnected.
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.
How it works
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.