How to wire an MPU6050 to an ESP32
To wire an MPU6050 to an ESP32, connect VCC to 3V3, GND to GND, SCL to GPIO 22 (D22) and SDA to GPIO 21 (D21). Power the GY-521 from 3.3 V so its pull-ups hold SDA and SCL at 3.3 V, the board’s own level. The sketch below uses no library beyond what the board includes. It wakes the sensor and prints the raw acceleration on three axes every half second.
- 3.3 V logic
- Library: Wire.h
- 4 wires
MPU6050 to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| MPU6050 pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | 3V3 | red | The GY-521 board has a 3.3 V regulator, so it takes 3.3 or 5 V. |
| GND | GND | blue | Common ground. |
| SCL | GPIO 22 (D22) | cyan | I2C clock. |
| SDA | GPIO 21 (D21) | violet | I2C data. |
| AD0 | not connected | n/a | Low (the board’s own pull-down): address 0x68. Tie it high for 0x69. |
Power and logic levels for MPU6050 and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The MPU6050 takes 3.3 V from 3V3. That pin is the ESP32’s own regulated 3.3 V output.
This pair
On an ESP32 DevKit, Wire.begin() with no arguments puts SDA on GPIO 21 and SCL on GPIO 22, labeled D21 and D22. Powered from 3V3, the GY-521’s pull-ups hold SDA and SCL at 3.3 V, the ESP32’s own level. Its regulator still works from 3.3 V, with a little less headroom.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: none, only Wire.h: the sketch reads the registers itself. This is the sketch the circuit above runs.
#include <Wire.h>
const int MPU = 0x68; // AD0 low
int16_t read16() {
int16_t high = Wire.read();
return (high << 8) | Wire.read();
}
void setup() {
Serial.begin(115200);
Wire.begin(); // SDA GPIO 21 (D21), SCL GPIO 22 (D22)
Wire.beginTransmission(MPU);
Wire.write(0x6B); // PWR_MGMT_1
Wire.write(0); // wake up: it starts asleep
Wire.endTransmission();
Serial.println("MPU6050 awake");
}
void loop() {
Wire.beginTransmission(MPU);
Wire.write(0x3B); // ACCEL_XOUT_H, then Y, Z, temperature, gyro
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6);
int16_t ax = read16();
int16_t ay = read16();
int16_t az = read16();
// At the default range, 16384 counts is 1 g.
Serial.print("Accel x: ");
Serial.print(ax);
Serial.print(" y: ");
Serial.print(ay);
Serial.print(" z: ");
Serial.println(az);
delay(250);
}
What you should see
Open the serial monitor at 115200 baud. The sketch prints MPU6050 awake and then the raw acceleration on x, y and z. Lying flat, x and y read near zero and z reads about 16384, which is 1 g at the default range.
Tilt the sensor in the editor and the numbers follow. The AD0 pin is left open, which puts the sensor at address 0x68.
The serial monitor prints this, recorded from the circuit above lying flat:
MPU6050 awake
Accel x: 0 y: 0 z: 16384Common mistakes wiring an MPU6050 to an ESP32
Not waking it up
Out of reset the chip is asleep and returns zeros on every axis. Write 0 to register 0x6B first. It is the single most common MPU6050 fault.
Wiring to the ESP8266’s I2C pins
Many MPU6050 tutorials were written for the ESP8266, with SDA on GPIO 4 and SCL on GPIO 5. On an ESP32 DevKit Wire.begin() uses GPIO 21 (D21) and GPIO 22 (D22).
Expecting an angle from the gyro
The gyroscope measures how fast the board turns, in degrees per second, not which way it points. For tilt, use the arctangent of two acceleration axes; for a steady angle while moving, combine both with a complementary filter.
Build your own MPU6050 project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.