Skip to content

Raspberry Pi 4 MPU6050: Read Motion Data in Python | ShillehTek

November 05, 2023

Video Tutorial (Optional)

Watch first if you want to follow along and see the MPU6050 readings coming into Python on a Raspberry Pi 4.

Project Overview

Raspberry Pi 4 + MPU6050: In this quick tutorial you connect an MPU6050 accelerometer/gyroscope to a Raspberry Pi 4 over I2C and read sensor values in Python.

You will enable I2C on the Pi, wire the sensor, install the required library, and run the provided example script.

  • Time: 15 to 30 minutes
  • Skill level: Beginner
  • What you will build: A Python setup that reads MPU6050 motion data on a Raspberry Pi via I2C

Parts List

From ShillehTek

External

  • Amazon Store ShillehTek - alternate place to buy the MPU6050 module mentioned in the original post
  • Raspberry Pi 4 - the Linux single-board computer running Python and I2C
  • Jumper wires - for connecting the MPU6050 to the Pi GPIO header

Note: The MPU6050 should be pre-soldered with header pins so you can easily connect it with jumper wires.

Step-by-Step Guide

Step 1 - Enable I2C on the Raspberry Pi

Goal: Turn on the Raspberry Pi I2C interface so Python can communicate with the MPU6050.

What to do: Open a terminal and run:

sudo raspi-config

Then follow these menu options:

  • Select Interfacing Options > I2C
  • Select Yes when prompted to enable the I2C interface
  • Select Yes when prompted to automatically load the I2C kernel module
  • Select Finish
  • Select Yes when prompted to reboot

You can also reboot by running sudo reboot in the command line.

Raspberry Pi terminal showing raspi-config Interfacing Options menu to enable I2C for an MPU6050 connection
Enable I2C from raspi-config.
Raspberry Pi raspi-config screen showing the prompt to enable the I2C interface for reading an MPU6050 sensor
Confirm enabling the I2C interface and loading the kernel module.

Expected result: The Pi reboots with I2C enabled.

Step 2 - Physically connect the MPU6050

Goal: Wire the MPU6050 to the Raspberry Pi 4 so it can communicate over I2C.

What to do: Connect the MPU6050 respectively as shown in the diagram below.

Raspberry Pi 4 wired to an MPU6050 sensor module using I2C lines (SDA/SCL) and power pins on a breadboard
Wiring diagram for connecting the MPU6050 to the Raspberry Pi over I2C.

Expected result: The MPU6050 is powered and connected to the Pi I2C pins per the diagram.

Step 3 - Install the Python library and run the example code

Goal: Install the required dependencies and run the provided test script to read values.

What to do: Install the library with the following commands:

sudo apt install python3-smbus

pip install mpu6050-raspberrypi

Download and run the example script from the original link:

https://github.com/shillehbean/youtube-p2/blob/main/mpu6050_test.py

The video walks through the code in more detail. The script creates an MPU6050 object and prints values every second. You can run it from any editor. In the original tutorial, Thonny was used.

Expected result: You see MPU6050 readings updating in your Python output.

Conclusion

You enabled I2C on a Raspberry Pi 4, wired an MPU6050 accelerometer/gyroscope module, installed the required Python packages, and ran a test script to start getting readings.

Want the exact parts used in this build? Grab them from ShillehTek.com. If you want help customizing this project or building something for your product, check out our IoT consulting services.

More resources from the original post: https://github.com/m-rtijn/mpu6050 (library link). Other tutorials mentioned: https://www.youtube.com/watch?v=5xLHZEl0h10 https://www.youtube.com/watch?v=hoNeIaXc0vs https://www.youtube.com/watch?v=BkqZWeC_xCg https://www.youtube.com/watch?v=H9e1Up7xHjc