0
0
AutocadHow-ToBeginner · 4 min read

How to Use MPU6050 Accelerometer with Arduino: Simple Guide

To use the MPU6050 accelerometer with Arduino, connect it via I2C pins (SDA to A4, SCL to A5 on Uno), then use a library like MPU6050 or Wire to read acceleration data. Initialize the sensor in your code and read the accelerometer values to use them in your project.
📐

Syntax

Using the MPU6050 with Arduino typically involves these steps:

  • Include the Wire.h library for I2C communication.
  • Include the MPU6050.h library to simplify sensor interaction.
  • Create an MPU6050 object.
  • Initialize the sensor with mpu.initialize().
  • Check connection with mpu.testConnection().
  • Read accelerometer data using mpu.getAcceleration(&ax, &ay, &az).
arduino
#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup() {
  Wire.begin();
  mpu.initialize();
  if (!mpu.testConnection()) {
    // handle connection failure
  }
}

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);
  // use ax, ay, az values
}
💻

Example

This example shows how to read and print accelerometer values from the MPU6050 sensor using Arduino.

arduino
#include <Wire.h>
#include <MPU6050.h>

MPU6050 mpu;

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu.initialize();

  if (!mpu.testConnection()) {
    Serial.println("MPU6050 connection failed");
    while (1);
  } else {
    Serial.println("MPU6050 connected successfully");
  }
}

void loop() {
  int16_t ax, ay, az;
  mpu.getAcceleration(&ax, &ay, &az);

  Serial.print("Accel X: "); Serial.print(ax);
  Serial.print(" | Accel Y: "); Serial.print(ay);
  Serial.print(" | Accel Z: "); Serial.println(az);

  delay(500);
}
Output
MPU6050 connected successfully Accel X: 123 | Accel Y: -456 | Accel Z: 16384 Accel X: 120 | Accel Y: -460 | Accel Z: 16380 ... (updates every 500ms)
⚠️

Common Pitfalls

Common mistakes when using MPU6050 with Arduino include:

  • Incorrect wiring: Make sure SDA connects to A4 and SCL to A5 on Arduino Uno.
  • Not initializing Wire.begin() before sensor setup.
  • Ignoring sensor connection check with mpu.testConnection().
  • Reading raw values without scaling or calibration can cause confusing results.
  • Not adding pull-up resistors on I2C lines if your board or sensor module lacks them.
arduino
// Wrong: Missing Wire.begin()
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
  mpu.initialize(); // Missing Wire.begin() causes failure
}

// Right way:
#include <Wire.h>
#include <MPU6050.h>
MPU6050 mpu;
void setup() {
  Wire.begin();
  mpu.initialize();
}
📊

Quick Reference

Here is a quick summary for using MPU6050 with Arduino:

StepDescription
Connect SDA to A4, SCL to A5Wiring for I2C communication on Arduino Uno
Include Wire.h and MPU6050.hLibraries needed for sensor communication
Call Wire.begin() in setup()Initialize I2C bus
Initialize sensor with mpu.initialize()Prepare MPU6050 for reading
Check connection with mpu.testConnection()Verify sensor is connected
Read acceleration with mpu.getAcceleration(&ax, &ay, &az)Get raw accelerometer data
Print or use values as neededUse data for your application

Key Takeaways

Always connect MPU6050 SDA to Arduino A4 and SCL to A5 for I2C communication.
Initialize I2C with Wire.begin() before setting up the MPU6050 sensor.
Use the MPU6050 library to simplify reading accelerometer data.
Check sensor connection with mpu.testConnection() to avoid errors.
Raw accelerometer values need calibration or scaling for meaningful use.