Sulabh Sethi · Blog ← Main Site

MPU6050: Accelerometer and Gyroscope

The MPU6050 motion sensor: a 3-axis accelerometer plus a 3-axis gyroscope with an onboard Digital Motion Processor. What it measures, where it is used, and an Arduino example to read it over I2C.

Embedded Systems · 9 August 2023 · 2 min read · Updated 11 August 2023

MPU6050 module

The MPU6050 is a sensor that tells you how an object is moving and how it is tilted. It consists of a 3-axis accelerometer and a 3-axis gyroscope.

The accelerometer can tell direction, like up, down, left, or right. The gyroscope senses rotation, like when you spin a top. When you combine the two, you can work out how the device is moving and which way it is facing. This is really useful for drones, robots, or game controllers that need to know how they are being moved.

The accelerometer measures acceleration along each of the three axes (x, y, z) and can determine the direction and magnitude of motion. The gyroscope measures angular velocity, or how quickly the device is rotating around each axis.

The onboard Digital Motion Processor (DMP) combines accelerometer and gyroscope data to give a more accurate picture of motion and orientation. It can output quaternions or Euler angles, which can be used to determine position, velocity, and orientation. It communicates with microcontrollers over I2C, SPI, and UART.

Applications

  1. Robotics: position and orientation sensing for navigation and interaction.
  2. Industrial automation: precise movement and process control.
  3. Virtual reality: orientation sensing so users can look around a virtual environment.
  4. Gaming: motion sensing to control games with natural movement.
  5. Fitness tracking: monitoring daily activity.
  6. Motion capture: capturing movement for realistic animation.
  7. Smartphones and tablets: rotating the screen with orientation.
  8. Augmented reality: overlaying virtual objects on the real world.
  9. Medical devices: monitoring movement and orientation.
  10. Drones: stabilization and orientation sensing.

Interfacing with Arduino

The MPU6050 uses the I2C bus to talk to the Arduino. Connect the SDA and SCL pins per your wiring diagram, then install the “Wire” and “MPU6050” libraries. The example below reads accelerometer and gyroscope values and prints them to the serial monitor.

#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu.initialize();
}
void loop() {
int16_t ax, ay, az;
int16_t gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
float axg = ax / 16384.0;
float ayg = ay / 16384.0;
float azg = az / 16384.0;
float gxds = gx / 131.0;
float gyds = gy / 131.0;
float gzds = gz / 131.0;
Serial.print("Accel: ");
Serial.print(axg); Serial.print(", ");
Serial.print(ayg); Serial.print(", ");
Serial.print(azg);
Serial.print(" Gyro: ");
Serial.print(gxds); Serial.print(", ");
Serial.print(gyds); Serial.print(", ");
Serial.println(gzds);
delay(100);
}

This converts the raw values into meaningful units (g for acceleration, degrees/second for rotation). The MPU6050 can be put into sleep mode to cut current draw to around 10µA when idle. It can be affected by magnetic interference from nearby motors or electronics, so shield it or place it away from such sources.

Originally published on sslabs.in.