0
0
Iot-protocolsHow-ToBeginner · 4 min read

How to Use MPU6050 with Raspberry Pi: Step-by-Step Guide

To use the MPU6050 sensor with a Raspberry Pi, connect it via I2C pins and enable I2C on the Pi. Then, use Python libraries like smbus2 to read accelerometer and gyroscope data from the sensor.
📐

Syntax

The basic steps to use the MPU6050 with Raspberry Pi are:

  • Enable I2C interface on Raspberry Pi.
  • Connect MPU6050 SDA and SCL pins to Pi's SDA (GPIO 2) and SCL (GPIO 3).
  • Use Python smbus2 library to communicate over I2C.
  • Read sensor registers to get accelerometer and gyroscope data.

Each part is essential: enabling I2C allows communication, wiring connects the hardware, and Python code reads sensor values.

python
import smbus2
import time

# MPU6050 device address
MPU6050_ADDR = 0x68

# Registers
PWR_MGMT_1 = 0x6B
ACCEL_XOUT_H = 0x3B

# Initialize I2C bus
bus = smbus2.SMBus(1)

# Wake up MPU6050
bus.write_byte_data(MPU6050_ADDR, PWR_MGMT_1, 0)

# Function to read raw data
def read_raw_data(addr):
    high = bus.read_byte_data(MPU6050_ADDR, addr)
    low = bus.read_byte_data(MPU6050_ADDR, addr+1)
    value = (high << 8) | low
    if value > 32767:
        value = value - 65536
    return value
💻

Example

This example reads accelerometer X, Y, Z values and prints them every second. It shows how to initialize the sensor and get live data.

python
import smbus2
import time

MPU6050_ADDR = 0x68
PWR_MGMT_1 = 0x6B
ACCEL_XOUT_H = 0x3B
ACCEL_YOUT_H = 0x3D
ACCEL_ZOUT_H = 0x3F

bus = smbus2.SMBus(1)
bus.write_byte_data(MPU6050_ADDR, PWR_MGMT_1, 0)

def read_raw_data(addr):
    high = bus.read_byte_data(MPU6050_ADDR, addr)
    low = bus.read_byte_data(MPU6050_ADDR, addr+1)
    value = (high << 8) | low
    if value > 32767:
        value = value - 65536
    return value

try:
    while True:
        acc_x = read_raw_data(ACCEL_XOUT_H)
        acc_y = read_raw_data(ACCEL_YOUT_H)
        acc_z = read_raw_data(ACCEL_ZOUT_H)

        # Convert to g (gravity)
        Ax = acc_x / 16384.0
        Ay = acc_y / 16384.0
        Az = acc_z / 16384.0

        print(f"Accelerometer: X={Ax:.2f}g, Y={Ay:.2f}g, Z={Az:.2f}g")
        time.sleep(1)
except KeyboardInterrupt:
    print("Program stopped")
Output
Accelerometer: X=0.01g, Y=0.00g, Z=1.00g Accelerometer: X=0.02g, Y=-0.01g, Z=0.99g Accelerometer: X=0.00g, Y=0.01g, Z=1.01g ...
⚠️

Common Pitfalls

  • I2C not enabled: Forgetting to enable I2C in Raspberry Pi settings will cause communication failure.
  • Wrong wiring: SDA and SCL pins must be connected correctly; swapping them breaks data reading.
  • Power issues: MPU6050 needs 3.3V or 5V power; wrong voltage can damage the sensor.
  • Incorrect device address: MPU6050 usually uses 0x68, but check with i2cdetect -y 1 to confirm.
  • Not waking sensor: The sensor is asleep by default; writing 0 to PWR_MGMT_1 register wakes it up.
python
## Wrong way: Not waking sensor
bus.write_byte_data(MPU6050_ADDR, PWR_MGMT_1, 0x40)  # Puts sensor to sleep

## Right way: Wake sensor
bus.write_byte_data(MPU6050_ADDR, PWR_MGMT_1, 0x00)  # Wake up sensor
📊

Quick Reference

Summary tips for using MPU6050 with Raspberry Pi:

  • Enable I2C via sudo raspi-config under Interface Options.
  • Connect SDA to GPIO 2 and SCL to GPIO 3 on Pi.
  • Use smbus2 Python library for I2C communication.
  • Wake sensor by writing 0 to PWR_MGMT_1 register.
  • Read accelerometer and gyroscope data from their respective registers.

Key Takeaways

Enable I2C on Raspberry Pi before connecting MPU6050 sensor.
Connect MPU6050 SDA and SCL pins correctly to Raspberry Pi GPIO 2 and 3.
Use Python smbus2 library to read sensor data via I2C.
Wake up the MPU6050 by writing 0 to the power management register.
Check device address with i2cdetect to avoid communication errors.