Arduino MPU6050 Tilt Sensor: Wiring, Code and Simulator

To build this Arduino MPU6050 Tilt Sensor project you need an Arduino Uno and an MPU6050 accelerometer. Wake an MPU6050 and read its accelerometer registers over I2C, converted to g. Make the 4 connections in the wiring table, upload the Arduino C++ code, and move the accel sliders in the Inspector: tilting the board shows up as gravity sharing out between X, Y and Z.

Open this circuit in the free Arduino Uno simulator

Parts list

QtyPartWhat it does
1Arduino Unothe microcontroller board
1MPU6050 accelerometerAccelerometer + gyroscope on I²C at 0x68 (0x69 with AD0 set). Write 0 to register 0x6B to wake it, then read 14 bytes from 0x3B.

Wiring

#FromTo
1Arduino Uno 5VMPU6050 accelerometer VCC
2Arduino Uno GNDMPU6050 accelerometer GND
3Arduino Uno A4MPU6050 accelerometer SDA
4Arduino Uno A5MPU6050 accelerometer SCL

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an MPU6050 accelerometer from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to MPU6050 accelerometer VCC.
  4. Wire 2. Connect Arduino Uno GND to MPU6050 accelerometer GND.
  5. Wire 3. Connect Arduino Uno A4 to MPU6050 accelerometer SDA.
  6. Wire 4. Connect Arduino Uno A5 to MPU6050 accelerometer SCL.
  7. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  8. Run it. Press Run. Move the accel sliders in the Inspector: tilting the board shows up as gravity sharing out between X, Y and Z.

Arduino C++ code

// Tilt meter: wake an MPU6050 and read its accelerometer over I2C.
// At the default +-2 g range, 16384 counts are 1 g.
#include <Wire.h>

const byte MPU = 0x68;

int16_t read16() { int16_t hi = Wire.read(); return (hi << 8) | Wire.read(); }

void setup() {
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);  // PWR_MGMT_1
  Wire.write(0);     // wake up
  Wire.endTransmission();
}

void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);  // ACCEL_XOUT_H
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, (byte)6);
  float x = read16() / 16384.0, y = read16() / 16384.0, z = read16() / 16384.0;
  Serial.print("ax=");
  Serial.print(x, 2);
  Serial.print(" ay=");
  Serial.print(y, 2);
  Serial.print(" az=");
  Serial.println(z, 2);
  delay(250);
}

What to try

Move the accel sliders in the Inspector: tilting the board shows up as gravity sharing out between X, Y and Z.

Frequently asked questions

How do I wire the MPU6050 Tilt Sensor project on an Arduino Uno?

Make 4 connections: Arduino Uno 5V to MPU6050 accelerometer VCC; Arduino Uno GND to MPU6050 accelerometer GND; Arduino Uno A4 to MPU6050 accelerometer SDA; Arduino Uno A5 to MPU6050 accelerometer SCL.

What parts do I need for this Arduino project?

An Arduino Uno and an MPU6050 accelerometer.

Can I simulate this Arduino project online for free?

Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.

What does the code do?

Wake an MPU6050 and read its accelerometer registers over I2C, converted to g.

More Arduino Uno projects