MPU-6050 Triple-Axis Accelerometer & Gyroscope Module Documentation
The MPU-6050 is a popular, low-cost, and high-performance triple-axis accelerometer and gyroscope module. It combines a 3-axis accelerometer and a 3-axis gyroscope in a single chip, providing a comprehensive motion sensing solution for a wide range of applications, including robotics, drones, IoT devices, and wearable electronics.
Accelerometer:
+ Measurements: 2g, 4g, 8g, or 16g
+ Resolution: 16 bits
Gyroscope:
+ Measurements: 250/s, 500/s, 1000/s, or 2000/s
+ Resolution: 16 bits
Communication: I2C (up to 400 kHz) or SPI (up to 1 MHz)
Power Supply: 2.4V to 3.6V
Operating Temperature: -40C to 85C
The following code examples demonstrate how to use the MPU-6050 module with Arduino and Raspberry Pi.
### Example 1: Arduino - Basic Accelerometer and Gyroscope Readings
This example uses the Arduino MPU-6050 library to read accelerometer and gyroscope data from the module.
```cpp
#include <Wire.h>
#include <MPU6050.h>
void setup() {
Serial.begin(115200);
Wire.begin();
mpu.initialize();
}
void loop() {
Vector rawAccel = mpu.readRawAccel();
Vector rawGyro = mpu.readRawGyro();
Serial.print("Acceleration (x, y, z): ");
Serial.print(rawAccel.X);
Serial.print(", ");
Serial.print(rawAccel.Y);
Serial.print(", ");
Serial.println(rawAccel.Z);
Serial.print("Gyroscope (x, y, z): ");
Serial.print(rawGyro.X);
Serial.print(", ");
Serial.print(rawGyro.Y);
Serial.print(", ");
Serial.println(rawGyro.Z);
delay(50);
}
```
### Example 2: Raspberry Pi - Python Script for Accelerometer and Gyroscope Data
This example uses the Python library `smbus` to communicate with the MPU-6050 module via I2C.
```python
import smbus
import time
bus = smbus.SMBus(1) # I2C bus 1 on Raspberry Pi
mpu_addr = 0x68 # MPU-6050 address
def read_accel():
bus.write_byte(mpu_addr, 0x3B) # Accelerometer data register
data = bus.read_i2c_block_data(mpu_addr, 0x3B, 6) # Read 6 bytes (x, y, z)
return data
def read_gyro():
bus.write_byte(mpu_addr, 0x43) # Gyroscope data register
data = bus.read_i2c_block_data(mpu_addr, 0x43, 6) # Read 6 bytes (x, y, z)
return data
while True:
accel_data = read_accel()
gyro_data = read_gyro()
print("Acceleration (x, y, z): ", accel_data[0], accel_data[1], accel_data[2])
print("Gyroscope (x, y, z): ", gyro_data[0], gyro_data[1], gyro_data[2])
time.sleep(0.05)
```
These examples demonstrate the basic usage of the MPU-6050 module for reading accelerometer and gyroscope data. You can build upon these examples to create more complex applications, such as gesture recognition, motion tracking, and stabilization systems.