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

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.


| Module pin | UNO pin |
|---|---|
| VCC | 5V |
| GND | GND |
| SCL | A5 |
| SDA | A4 |
Leave XDA, XCL, AD0 and INT unconnected.
Build this circuit in EEMENTOR Lab
Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.
Build it yourself
- Wire the parts using the wiring table above. Pins with no wire show red.
- Press Run, move the tilt sliders under Selected, and watch the Serial monitor.
- Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.
Stuck? See the finished circuit →
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

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.