Wiring guide

How to wire an MPU6050 to an Arduino Mega

To wire an MPU6050 to an Arduino Mega, connect VCC to 5V, GND to GND, SCL to pin 21 and SDA to pin 20. The GY-521 has a 3.3 V regulator and its pull-ups go to that, so the module connects directly to a 5 V board. 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.

  • 5 V logic
  • Library: Wire.h
  • 4 wires
Arduino Mega · MPU6050live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

MPU6050 to Arduino Mega 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 pinArduino Mega pinWire colorWhy
VCC5VredThe GY-521 board has a 3.3 V regulator, so it takes 3.3 or 5 V.
GNDGNDblueCommon ground.
SCLpin 21cyanI2C clock.
SDApin 20violetI2C data.
AD0not connectedn/aLow (the board’s own pull-down): address 0x68. Tie it high for 0x69.
Voltage

Power and logic levels for MPU6050 and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The MPU6050 takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

On the Mega the I2C bus is pin 20 (SDA) and pin 21 (SCL), not A4 and A5 as on an Uno. The SDA and SCL pins by AREF are the same two nets. The GY-521’s pull-ups go to its own 3.3 V regulator output, and the ATmega2560 reads 3.3 V as high on I2C, so the module connects directly at 5 V.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: none, only Wire.h: the sketch reads the registers itself. This is the sketch the circuit above runs.

MPU6050 on Arduino Mega I2C
#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(9600);
  Wire.begin();                 // SDA pin 20, SCL pin 21
  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);
}
Output

What you should see

Open the serial monitor at 9600 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: 16384
Pitfalls

Common mistakes wiring an MPU6050 to an Arduino Mega

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.

SDA and SCL on A4 and A5

That is the Uno’s bus. On the Mega it is pins 20 and 21, and on A4 and A5 the chip never answers.

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.