The MPU6050 is a 6-axis motion tracking device that integrates a 3-axis accelerometer and a 3-axis gyroscope into a single compact package. It is widely used for measuring orientation, acceleration, and angular velocity in various applications such as robotics, drones, gaming devices, and motion tracking systems. Its compatibility with the Wokwi simulation platform makes it an excellent choice for prototyping and learning.
The MPU6050 communicates via the I2C protocol, making it easy to interface with microcontrollers like the Arduino UNO. It also features a Digital Motion Processor (DMP) for advanced motion processing.
Pin Name | Description |
---|---|
VCC | Power supply (3.3V to 5V) |
GND | Ground |
SCL | I2C clock line |
SDA | I2C data line |
AD0 | I2C address select (low: 0x68, high: 0x69) |
INT | Interrupt pin (optional, for DMP) |
To use the MPU6050 with an Arduino UNO, follow these steps:
Below is an example code to read accelerometer and gyroscope data from the MPU6050 using the Arduino IDE. This code uses the Wire
library for I2C communication.
#include <Wire.h>
// MPU6050 I2C address
const int MPU6050_ADDR = 0x68;
// Registers for accelerometer and gyroscope data
const int ACCEL_XOUT_H = 0x3B;
const int GYRO_XOUT_H = 0x43;
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Start serial communication
// Wake up the MPU6050 (set power management register to 0)
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(0x6B); // Power management register
Wire.write(0); // Set to 0 to wake up the sensor
Wire.endTransmission();
}
void loop() {
int16_t accelX, accelY, accelZ;
int16_t gyroX, gyroY, gyroZ;
// Read accelerometer data
accelX = readMPU6050(ACCEL_XOUT_H);
accelY = readMPU6050(ACCEL_XOUT_H + 2);
accelZ = readMPU6050(ACCEL_XOUT_H + 4);
// Read gyroscope data
gyroX = readMPU6050(GYRO_XOUT_H);
gyroY = readMPU6050(GYRO_XOUT_H + 2);
gyroZ = readMPU6050(GYRO_XOUT_H + 4);
// Print data to the Serial Monitor
Serial.print("Accel: ");
Serial.print(accelX); Serial.print(", ");
Serial.print(accelY); Serial.print(", ");
Serial.print(accelZ); Serial.print(" | ");
Serial.print("Gyro: ");
Serial.print(gyroX); Serial.print(", ");
Serial.print(gyroY); Serial.print(", ");
Serial.println(gyroZ);
delay(500); // Delay for readability
}
// Function to read 16-bit data from MPU6050
int16_t readMPU6050(int reg) {
Wire.beginTransmission(MPU6050_ADDR);
Wire.write(reg); // Specify the register to read from
Wire.endTransmission(false);
Wire.requestFrom(MPU6050_ADDR, 2, true); // Request 2 bytes of data
int16_t data = Wire.read() << 8 | Wire.read(); // Combine high and low bytes
return data;
}
MPU6050
or MPU6050_DMP6
for advanced features like DMP processing.No data or incorrect readings:
Device not detected:
Unstable or noisy readings:
Q: Can I use the MPU6050 with a 3.3V microcontroller?
A: Yes, the MPU6050 operates at 3.3V logic levels and can be powered with 3.3V.
Q: How do I change the accelerometer or gyroscope range?
A: Modify the appropriate configuration registers (0x1C for accelerometer, 0x1B for gyroscope) in your code.
Q: Is the MPU6050 compatible with Wokwi?
A: Yes, the MPU6050 is fully compatible with the Wokwi simulation platform, allowing for easy prototyping and testing.