MPU6050 Interfacing: Raspberry Pi

 Model Picture

1. Introduction

The MPU6050 is a powerful sensor that combines a 3-axis accelerometer and 3-axis gyroscope. It can measure motion, tilt, and rotation, making it ideal for robotics and motion-based applications with the Raspberry Pi.


2. Components

  • Raspberry Pi 4 Model B
  • MPU6050
  • Jumper wires
  • Breadboard (optional)

3. Circuit and Connections

Raspberry Pi Pinout Diagram
Uploading: 187893 of 187893 bytes uploaded.


MPU6050 Accelerometer & Gyroscope Pinout Diagram
MPU6050 Accelerometer & Gyroscope Pinout Diagram

I2C Connection Table

MPU6050 PinRaspberry Pi
VCC3.3V
GNDGND
SDAGPIO2 (Pin 3)
SCLGPIO3 (Pin 5)

👉 Default I2C Address: 0x68 (or 0x69)


4. Detailed Step By Step Circuit Working

  1. MPU6050 detects acceleration and angular rotation.
  2. Internal ADC converts motion data into digital values.
  3. Data is sent to Raspberry Pi via I2C protocol.
  4. Pi reads raw values from registers.
  5. Values represent motion in X, Y, Z axes.

5. Libraries to be Included

sudo apt update
sudo apt install python3-smbus i2c-tools
pip3 install smbus2

Enable I2C:

sudo raspi-config

→ Interface Options → I2C → Enable


6. Code (Python)

import smbus2
import time

bus = smbus2.SMBus(1)
addr = 0x68

# Wake up MPU6050
bus.write_byte_data(addr, 0x6B, 0)

def read_word(reg):
high = bus.read_byte_data(addr, reg)
low = bus.read_byte_data(addr, reg + 1)
value = (high << 8) + low
if value > 32768:
value = value - 65536
return value

while True:
acc_x = read_word(0x3B)
acc_y = read_word(0x3D)
acc_z = read_word(0x3F)

gyro_x = read_word(0x43)
gyro_y = read_word(0x45)
gyro_z = read_word(0x47)

print("ACC:", acc_x, acc_y, acc_z)
print("GYRO:", gyro_x, gyro_y, gyro_z)
print("----------------------")

time.sleep(1)

7. Detailed Step By Step Code Working

  • Initializes I2C communication
  • Wakes up sensor using register 0x6B
  • Reads high & low bytes from registers
  • Combines values into signed integers
  • Displays acceleration and gyro data

8. Tips

  • Use i2cdetect -y 1 to confirm address
  • Keep sensor stable for accurate readings
  • Avoid vibrations during testing
  • Calibrate sensor for better accuracy
  • Use libraries for angle calculation if needed

9. Uses

  • Self-balancing robots
  • Gesture control systems
  • Motion tracking
  • Drone stabilization
  • Gaming controllers

10. Conclusion

The MPU6050 is a powerful motion sensor that enables advanced applications like robotics and gesture control. Interfacing with Raspberry Pi using I2C is simple and effective.

à´¤ുà´Ÿà´•്à´•à´•്à´•ാർക്à´•ാà´¯ുà´³്à´³ ഇലക്à´Ÿ്à´°ോà´£ിà´•്à´¸് ലളിതമാà´¯ി പഠിà´•്à´•ാം.

Empowering students in Kerala with hands-on technical skills.