What is the MPU-6050?
The MPU-6050 is a six-axis inertial measurement unit (IMU) that combines a 3-axis accelerometer and a 3-axis gyroscope in one chip. The common GY-521 breakout exposes the sensor over I²C and usually includes supporting components such as a voltage regulator and pull-up resistors. The chip also includes a temperature sensor.
Use the accelerometer to measure acceleration (including gravity), the gyroscope to measure angular velocity, and I²C to read the data from a microcontroller. These raw readings do not automatically provide reliable yaw, pitch and roll; orientation estimates require calculations and usually sensor fusion.
Accelerometer vs. gyroscope
- Accelerometer (X, Y, Z): reports acceleration along three axes. When stationary, gravity contributes to the reading, which can help estimate tilt. Motion and vibration also affect the result.
- Gyroscope (X, Y, Z): reports angular velocity, typically in degrees per second or radians per second. Integrating these values estimates rotation but accumulates drift over time.
- Temperature: the chip's internal temperature measurement can help characterize the sensor, but it is not a calibrated ambient-temperature probe.
The MPU-6050 is useful for robot balancing, gesture experiments, vibration monitoring, motion logging and learning about inertial sensing. It is not a GPS, compass or direct absolute-heading sensor.
I²C address and communication
The default 7-bit address is usually 0x68. If the AD0 pin is driven HIGH, the address commonly becomes 0x69. Some example sketches and libraries use the 7-bit address, not the shifted 8-bit read/write address.
I²C requires SDA, SCL, a shared ground and suitable pull-up resistors. Many GY-521 boards already include pull-ups, but clones may connect them to different rails. Before connecting to an ESP32 or another 3.3 V MCU, verify that SDA and SCL are never pulled up to 5 V. Use a proper bidirectional I²C level shifter if the breakout's logic levels are incompatible.
Common projects
- Self-balancing robot: combine accelerometer and gyroscope data with a control algorithm and motor driver.
- Gesture controller: detect tilt or motion patterns and map them to commands.
- Vibration logger: record acceleration changes over time to compare operating conditions.
- Motion-controlled interface: use orientation estimates to trigger menu actions or control a small game.
- Data logger: send readings to serial output, an SD card or a wireless dashboard.
Start by confirming that the sensor responds over I²C, then collect stable readings while the board is stationary. Add filtering or sensor fusion only after the raw data makes sense.
Best practices
- Mount it firmly. Loose wiring or a flexing board can appear as vibration or motion.
- Keep the I²C wires short. Long leads and noisy motor wiring can cause communication errors.
- Check logic levels. This matters especially when pairing a 5 V Arduino with a 3.3 V sensor breakout or an ESP32.
- Use sensible ranges. A wider full-scale range avoids clipping but reduces sensitivity for small movements.
- Calibrate when needed. Gyroscope bias and accelerometer offsets vary between modules; collect stationary samples and account for offsets in software.
- Do not call raw gyro integration absolute orientation. Gyroscope bias accumulates; use an appropriate fusion method if you need stable attitude estimates.
- Avoid unsupported library assumptions. Use the board-specific I²C pins and verify the library version and address configuration.
Troubleshooting
| Symptom | Likely cause | Check |
|---|---|---|
| Sensor not found | Wrong address, wiring, power or ground | Run an I²C scanner; check SDA/SCL and try 0x68/0x69 as appropriate. |
| Readings stay at zero | Sensor not initialized or incorrect bus pins | Confirm mpu.begin() succeeds and use the correct Wire pins. |
| ESP32 resets or bus hangs | Incorrect logic voltage or wiring noise | Verify SDA/SCL pull-ups are 3.3 V-safe and shorten wires. |
| Accelerometer values change at rest | Board tilt, vibration or offsets | Place it still on a level surface and inspect all axes; gravity should be visible on one axis. |
| Gyroscope never reads exactly zero | Normal bias and noise | Average stationary samples and calibrate offsets if needed. |
| Values clip during movement | Selected full-scale range is too small | Choose a wider accelerometer or gyroscope range. |
Frequently asked questions
Does the MPU-6050 measure yaw, pitch and roll directly? No. It reports acceleration and angular velocity. Orientation must be estimated in software; yaw especially drifts without an external reference such as a magnetometer.
Is a library required? No library is strictly required for raw I²C access, but the Adafruit MPU6050 library makes beginner projects simpler.
What is the default I²C address? Usually 0x68, with 0x69 commonly selected by setting AD0 HIGH.
Can I connect it to an ESP32? Yes, with compatible power and 3.3 V-safe I²C lines. Do not assume a breakout's VCC input rating also guarantees 5 V-safe SDA/SCL.
Is the simulator a full IMU emulator? No. The SolderHub project is a wiring and orientation teaching aid. It does not emulate real I²C register transactions, gyroscope drift or a full sensor-fusion algorithm.
Specifications
- Sensor type
- 6-axis IMU: 3-axis accelerometer plus 3-axis gyroscope.
- Interface
- I²C; commonly address 0x68, or 0x69 when AD0 is HIGH.
- Accelerometer range
- Selectable ±2 g, ±4 g, ±8 g, or ±16 g.
- Gyroscope range
- Selectable ±250, ±500, ±1000, or ±2000 degrees/second.
- Supply / logic
- Bare chip is a low-voltage device; breakout power and logic-level compatibility depend on the specific board. Verify before connecting to 5 V logic.
- Orientation output
- Raw acceleration and angular velocity; pitch/roll/yaw require software estimation and sensor fusion.
Pinout
| Pin | Name | Description |
|---|---|---|
| VCC | Power | Supply according to the exact breakout documentation; do not assume all GY-521 clones have identical regulation. |
| GND | Ground | Connect to the controller ground. |
| SDA | I²C data | Connect to the controller SDA pin and ensure pull-ups do not exceed the sensor logic voltage. |
| SCL | I²C clock | Connect to the controller SCL pin and ensure pull-ups do not exceed the sensor logic voltage. |
| AD0 | Address select | Typically selects 0x68 when LOW and 0x69 when HIGH. |
| INT | Interrupt | Optional interrupt output; use only if the firmware configures it. |
| XDA / XCL | Auxiliary I²C | Auxiliary sensor bus pins; not required for basic host I²C readings. |
Wiring & code
Setting up MPU-6050 with ESP32
Library
Adafruit MPU6050 by Adafruit
Arduino IDE Library Manager: Adafruit MPU6050; install Adafruit Unified Sensor and Adafruit BusIO dependencies as prompted.
Connect a GY-521 MPU-6050 to a classic ESP32 DevKit and read its six motion axes over I²C. This example uses GPIO21 for SDA and GPIO22 for SCL, with the Adafruit MPU6050 library. ESP32 GPIOs are not 5 V tolerant, so confirm the breakout's I²C pull-ups before wiring.
Warning
ESP32 safety: keep SDA and SCL at 3.3 V logic. Some GY-521 clones differ in their regulator and pull-up layout. If either I²C line idles at 5 V, do not connect it directly; use a suitable bidirectional I²C level shifter or modify the pull-up arrangement correctly.
MPU-6050 to ESP32
| GY-521 pin | ESP32 connection | Notes |
|---|---|---|
| VCC | 3V3 or supported module input | Use the voltage specified for your breakout. |
| GND | GND | Common ground. |
| SDA | GPIO21 | I²C data on classic ESP32 DevKit. |
| SCL | GPIO22 | I²C clock on classic ESP32 DevKit. |
| AD0 | GND or module default | LOW commonly selects 0x68. |
Try it in the simulator
The circuit below runs a no-library sketch that wakes the MPU-6050, prints its WHO_AM_I register and then pitch and roll from the accelerometer. Press Run, open the Serial Monitor and tilt the module from its Settings. The same register reads work on real hardware, so it is a quick way to confirm wiring and identify the chip before adding a library.
Try this circuit in the SolderHub Simulator
Run this exact circuit in your browserReady-wired, code loadedConfirm simulator behavior against the real wiring and the sensor datasheet before connecting hardware.
/*
MPU-6050: read the accelerometer directly over I2C and print pitch and roll
VCC -> 3.3V, GND -> GND
SDA -> GPIO21, SCL -> GPIO22, AD0 -> GND (address 0x68)
No library needed.
*/
#include <Wire.h>
const uint8_t MPU_ADDR = 0x68; // 0x69 if AD0 is tied HIGH
void writeReg(uint8_t reg, uint8_t value) {
Wire.beginTransmission(MPU_ADDR);
Wire.write(reg);
Wire.write(value);
Wire.endTransmission();
}
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA, SCL
writeReg(0x6B, 0x00); // PWR_MGMT_1: wake up (it starts asleep)
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x75); // WHO_AM_I
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, (uint8_t)1);
Serial.print("WHO_AM_I = 0x");
Serial.println(Wire.read(), HEX); // 0x68 on a genuine MPU-6050
}
void loop() {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // ACCEL_XOUT_H: 6 bytes of X, Y, Z
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, (uint8_t)6);
int16_t ax = (Wire.read() << 8) | Wire.read();
int16_t ay = (Wire.read() << 8) | Wire.read();
int16_t az = (Wire.read() << 8) | Wire.read();
// At the default ±2 g range, 16384 counts = 1 g
float pitch = atan2(-ax, sqrt((float)ay * ay + (float)az * az)) * 180.0 / PI;
float roll = atan2(ay, az) * 180.0 / PI;
Serial.print("pitch ");
Serial.print(pitch, 1);
Serial.print(" roll ");
Serial.println(roll, 1);
delay(200);
}Install the library
In Arduino IDE Library Manager, install Adafruit MPU6050 and its required dependencies, including Adafruit Unified Sensor and Adafruit BusIO. Select the correct ESP32 board and port.
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
Adafruit_MPU6050 mpu;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // Classic ESP32: SDA=GPIO21, SCL=GPIO22
if (!mpu.begin(0x68, &Wire)) {
Serial.println("MPU-6050 not found. Check power and I2C wiring.");
while (true) delay(10);
}
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
mpu.setGyroRange(MPU6050_RANGE_500_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);
Serial.printf("Accel m/s^2: %.2f, %.2f, %.2f\n",
accel.acceleration.x, accel.acceleration.y, accel.acceleration.z);
Serial.printf("Gyro rad/s: %.2f, %.2f, %.2f\n",
gyro.gyro.x, gyro.gyro.y, gyro.gyro.z);
Serial.printf("Temp C: %.1f\n\n", temp.temperature);
delay(250);
}- 1Disconnect power and wire SDA to GPIO21, SCL to GPIO22, VCC to a supported supply, and GND to GND.
- 2Confirm both I²C lines are 3.3 V-safe before connecting them to the ESP32.
- 3Install the Adafruit MPU6050 library and its dependencies.
- 4Upload the sketch to a classic ESP32 DevKit and open Serial Monitor at 115200 baud.
- 5Keep the board still to observe gravity and small gyro offsets, then rotate it slowly.
- 6If your ESP32 variant uses different I²C pins, change
Wire.begin(SDA_PIN, SCL_PIN)and update the wiring.
Expected output
Serial Monitor shows acceleration in m/s², angular velocity in rad/s, and chip temperature. The output changes as the module is tilted or rotated. These are raw measurements, not a fused orientation estimate.
Troubleshooting
- MPU not found: check GPIO21/GPIO22, address and common ground.
- Bus lockups or ESP32 resets: check for 5 V pull-ups and motor-related noise; shorten wires.
- Different ESP32 board: select the correct SDA/SCL pins in
Wire.begin(). - Wrong address: check AD0 or run an I²C scanner.
- Readings drift: normal gyro bias accumulates when integrating angular velocity; use calibration and sensor fusion if estimating orientation.









