Built-in project · Arduino Uno R3

MPU-6050 readings over I2C on an Arduino Uno

A GY-521 board with an MPU-6050 on A4 and A5: the sketch wakes the chip and prints its raw accelerometer, temperature and gyro readings with nothing but Wire. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

MPU-6050 readings over I2C · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// An MPU-6050 on the I2C bus: raw acceleration, temperature and rotation.
//
//   GY-521 VCC -> 5V   GND -> GND   SCL -> A5   SDA -> A4
//
// The chip answers at I2C address 0x68. It wakes up asleep, so the sketch
// first writes 0 to its power register (0x6B). After that, fourteen bytes
// starting at register 0x3B hold three accelerations, the temperature and
// three rotation rates, each a 16-bit number sent high byte first.
//
// At rest and flat, Z reads about 16384: that is 1 g, gravity, on the
// default range. Drag the board's pitch or roll and watch X and Y take some
// of it.

#include <Wire.h>

const int MPU = 0x68;

int16_t readWord() {
  int16_t high = Wire.read();
  int16_t low = Wire.read();
  return (high << 8) | low;
}

void setup() {
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: MPU-6050 on A4 (SDA) and A5 (SCL)");
}

void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14, true);
  int16_t ax = readWord();
  int16_t ay = readWord();
  int16_t az = readWord();
  int16_t raw = readWord();
  int16_t gx = readWord();
  int16_t gy = readWord();
  int16_t gz = readWord();

  Serial.print("AcX = "); Serial.print(ax);
  Serial.print(" | AcY = "); Serial.print(ay);
  Serial.print(" | AcZ = "); Serial.print(az);
  Serial.print(" | Tmp = "); Serial.print(raw / 340.0 + 36.53, 1);
  Serial.print(" | GyX = "); Serial.print(gx);
  Serial.print(" | GyY = "); Serial.print(gy);
  Serial.print(" | GyZ = "); Serial.println(gz);
  delay(500);
}

Parts list

3 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

4 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 5V; Accelerometer and gyroscope, MPU6050 pin VCC
  • Ground: Arduino Uno R3 pin GND; Accelerometer and gyroscope, MPU6050 pin GND
  • Arduino Uno R3 pin A4; Accelerometer and gyroscope, MPU6050 pin SDA
  • Arduino Uno R3 pin A5; Accelerometer and gyroscope, MPU6050 pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.