The MPU6050 is a versatile and powerful 6-axis motion tracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a temperature sensor. This microelectromechanical system (MEMS) is widely used in various applications, including drones, robotics, gaming devices, and virtual reality systems, to detect and measure motion, orientation, and acceleration.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (2.3V to 3.4V) |
2 | GND | Ground |
3 | SCL | I2C clock line |
4 | SDA | I2C data line |
5 | XDA | Auxiliary I2C data line (optional) |
6 | XCL | Auxiliary I2C clock line (optional) |
7 | AD0 | I2C address selection pin |
8 | INT | Interrupt output |
#include <Wire.h>
// MPU6050 I2C address (AD0 pin low is 0x68, high is 0x69)
const int MPU_ADDR = 0x68;
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Start serial communication at 9600 baud
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x6B); // Write to the power management register
Wire.write(0); // Set to zero to wake the MPU6050
Wire.endTransmission(true);
}
void loop() {
Wire.beginTransmission(MPU_ADDR);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU_ADDR, 14, true); // Request 14 registers
// Read accelerometer and gyroscope values
int16_t accX = Wire.read() << 8 | Wire.read();
int16_t accY = Wire.read() << 8 | Wire.read();
int16_t accZ = Wire.read() << 8 | Wire.read();
int16_t temp = Wire.read() << 8 | Wire.read();
int16_t gyroX = Wire.read() << 8 | Wire.read();
int16_t gyroY = Wire.read() << 8 | Wire.read();
int16_t gyroZ = Wire.read() << 8 | Wire.read();
// Print out the values
Serial.print("Accelerometer: ");
Serial.print("X = "); Serial.print(accX);
Serial.print(" | Y = "); Serial.print(accY);
Serial.print(" | Z = "); Serial.println(accZ);
Serial.print("Gyroscope: ");
Serial.print("X = "); Serial.print(gyroX);
Serial.print(" | Y = "); Serial.print(gyroY);
Serial.print(" | Z = "); Serial.println(gyroZ);
Serial.print("Temperature: ");
Serial.println(temp / 340.00 + 36.53); // Calculate and print temperature
delay(1000); // Delay for readability
}
Q: How do I change the I2C address of the MPU6050? A: The I2C address can be changed by altering the logic level of the AD0 pin. If AD0 is low (connected to GND), the address is 0x68. If high (connected to VCC), the address is 0x69.
Q: Can the MPU6050 be used with a 5V microcontroller? A: Yes, but a level shifter is recommended for the I2C lines, and the VCC should be supplied with 3.3V.
Q: How can I reduce noise in the sensor readings? A: Implementing a digital low-pass filter or a complementary filter can help reduce noise. Additionally, ensure the MPU6050 is mounted securely without vibrations.
This documentation provides a comprehensive guide to integrating and using the MPU6050 sensor with an Arduino UNO. For further assistance or advanced applications, refer to the manufacturer's datasheet and additional resources.