DEV Community

張旭豐
張旭豐

Posted on

5 MPU-6050 Accelerometer Projects That Sense Motion and Tilt

5 MPU-6050 Accelerometer Projects That Sense Motion and Tilt

Build motion detection systems: self-balancing robot, posture corrector, 3D cube visualizer, earthquake detector, and gesture remote

Hero

Project 1: Self-Balancing Robot

Goal: Keep a two-wheel robot upright using PID control

Self-Balancing Robot

Hardware

MPU-6050, Arduino Mega, 2x DC motors, L298N

Code

float pitch;
void calcPitch() {
  // Complementary filter: gyro + accel
  pitch = 0.98 * (pitch + gyroRate * dt) + 0.02 * accelAngle;
}

void loop() {
  calcPitch();
  int motorSpeed = Kp * pitch + Ki * errorSum + Kd * (pitch - lastPitch);
  motor1.run(motorSpeed);
  motor2.run(motorSpeed);
}
Enter fullscreen mode Exit fullscreen mode

Project 2: Posture Correction Device

Goal: Vibrate when slouching detected

Posture Correction Device

Hardware

MPU-6050, Arduino Nano, Vibration motor

Code

#define SLOPE_THRESHOLD 0.3
void loop() {
  float tilt = atan2(ay, az) * 180 / PI;
  if (tilt > SLOPE_THRESHOLD) {
    digitalWrite(VIB_PIN, HIGH);
    delay(500);
    digitalWrite(VIB_PIN, LOW);
  }
}
Enter fullscreen mode Exit fullscreen mode

Project 3: 3D Cube Visualizer

Goal: Rotate a 3D cube on OLED to match physical module orientation

3D Cube Visualizer

Hardware

MPU-6050, Arduino Nano, 128x64 OLED

Code

void loop() {
  // Read and process MPU data
  // Apply rotation matrices for X, Y, Z
  // Draw cube faces on OLED
  display.display();
}
Enter fullscreen mode Exit fullscreen mode

Project 4: Earthquake Detector

Goal: Detect table vibrations and display magnitude

Earthquake Detector

Hardware

MPU-6050, Arduino Nano, LCD, LEDs

Code

#define QUAKE_THRESHOLD 1.5
float maxAccel = 0;

void loop() {
  float totalAccel = sqrt(ax*ax + ay*ay + az*az) / 16384.0;
  if (totalAccel > maxAccel) maxAccel = totalAccel;
  if (totalAccel > QUAKE_THRESHOLD) {
    digitalWrite(RED_LED, HIGH);
    lcd.print(maxAccel);
  }
}
Enter fullscreen mode Exit fullscreen mode

Project 5: Gesture Remote Control

Goal: Control devices by tilting in different directions

Gesture Remote Control

Hardware

MPU-6050, Arduino Nano, IR LED or Bluetooth

Code

void loop() {
  if (ay > 8000) { sendCommand(CMD_UP); }
  else if (ay < -8000) { sendCommand(CMD_DOWN); }
  else if (ax > 8000) { sendCommand(CMD_LEFT); }
  else if (ax < -8000) { sendCommand(CMD_RIGHT); }
}
Enter fullscreen mode Exit fullscreen mode

How It Works

// WF1 Run #046 - Basic MPU-6050
#include <Wire.h>
#define MPU_ADDR 0x68

void setup() {
  Wire.begin();
  Serial.begin(115200);
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
}

void loop() {
  Wire.beginTransmission(MPU_ADDR);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_ADDR, 14, true);
  int16_t ax = Wire.read() << 8 | Wire.read();
  int16_t ay = Wire.read() << 8 | Wire.read();
  int16_t az = Wire.read() << 8 | Wire.read();
  Serial.print("X:"); Serial.print(ax/16384.0);
  Serial.print(" Y:"); Serial.print(ay/16384.0);
  Serial.print(" Z:"); Serial.println(az/16384.0);
  delay(100);
}
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Problem Cause Fix
No data from MPU I2C address wrong Default I2C address is 0x68; some modules use 0x69
Drift over time Gyro integration error Use complementary filter or Mahony AHRS algorithm
Wire library conflict Multiple I2C devices Ensure each device has unique address; use separate buses if needed

Start Here

Affiliate disclosure: As an Amazon Associate, I earn from qualifying purchases.

MPU-6050 Module
Arduino Nano
L298N Motor Driver
OLED 0.96 inch

Next Step: From Scene to Sensor, Without Writing Code

I offer a personalized interactive device design guide at Fiverr:

👉 https://www.fiverr.com/phd_hfchang/generate-an-arduino-interactive-prototypef

Tags: Arduino MPU6050 Accelerometer Gyroscope Motion

Top comments (0)