PiicoDev Motion Sensor MPU-6050 - Raspberry Pi Guide

Updated 27 March 2023


This guide will help you read motion data from your PiicoDev® Motion Sensor with a Raspberry Pi single-board computer.

To follow along, it's best to have:

In the video we use a PiicoDev Platform to keep everything safe and secure.

For this tutorial, we'll assume you're familiar using a Raspberry Pi like a desktop computer - connected to a keyboard, mouse, monitor and with an internet connection. If you need help getting started, have a look at Chapter 1 of the Raspberry Pi Workshop for Beginners.

If you prefer not to use the Expansion Board for Raspberry Pi, there are other connection options in our PiicoDev Connection Guide.


Connect the PiicoDev sensor to your Pi

Mount the PiicoDev® Adapter on your Raspberry Pi and plug connect your motion sensor to the Adapter via the PiicoDev cable.

The adapter connects to the 40-pin GPIO header on the Raspberry Pi -  ensure the header is mounted correctly, with the "Ethernet" label on the same side as the Pi's ethernet adapter.

If you're unfamiliar with connecting PiicoDev modules, read the PiicoDev Connection Guide before proceeding.


Pictured above: A Raspberry Pi 4 Model B with USB Power and HDMI (two white leads). The PiicoDev Adapter is placed on the 40-pin header, and connects to the sensor with a PiicoDev Cable.

Enable I2C

Power on your Raspberry Pi. Open the Raspberry Pi Configuration Menu, select the Interfaces tab and ensure I2C is enabled.

You only need to do this step for your first PiicoDev project - from here on you probably won't have to repeat this step when using PiicoDev hardware.


Install PiicoDev

Open Thonny (Pi Start Menu > Programming > Thonny IDE) and open the Manage Packages menu (Tools > Manage Packages)


Search for 'piicodev' and install or upgrade if necessary.

search-for-piicodev-pypi     install-piicodev-package

Download the Example Script

Download this example script: main.py (right-click, "save link as")

Save this file wherever you like to keep your programming projects. For this tutorial I'll save mine in /home/pi/PiicoDev and change the name to motion.py to give it some context.

Accelerometer Example

Open the example script in Thonny and run the script with the green play button. (Hint: View the files menu with View > Files). You should see acceleration data streaming up the shell. Open the plotter with View > Plotter and see how the data reacts when you move the sensor.


Above: Acceleration data streams up the Shell, while the Plot shows historic data. Here the sensor is sitting flat on my desk, so the Z-axis reads about 9.8m/s^2 or about 1g.

This example plots acceleration if m/s^2. If you prefer, you can return acceleration in g by calling motion.read_accel_data(g=True) 

Now, we'll perform a maneuver and observe the data. Start with the sensor laying on a level surface, then roll it so the X arrow points up, as below:


In the plot below, I've labelled the axes (x,y,z). The plot begins with my sensor sitting flat, and face-up on my desk - the Z-axis reads about 10m/s^2 and the others read zero. That makes sense, because only the z-axis is experiencing inertial forces (gravity). I then rotate the sensor so the X arrow is pointing up; we see the z-acceleration fall to zero, and the x-acceleration climb to about 10m/s^2.


Gyroscope Example

Now, we'll gather angular-velocity data from the gyroscope. In the example script, comment out the "Accelerometer Data" block and uncomment the "Gyroscope Data" block. The script should now look like this:


Data returned here is the angular velocity measured in degrees per second. Rotate about your sensor on a level surface and observe the plot:


A twist in the anticlockwise direction (positive Z) first creates a positive slope, as the twist speeds up. The slope becomes negative and falls to zero as we come to a stop. A similar shape appears when we twist to the clockwise direction (negative Z), only the data is all negative. Of course, this works in the other axes also - this is just a simple way to isolate rotation to one axis only.

Other Examples

The example script also contains a couple more examples - give them a try!

  • Temperature: Read the MPU-6050's internal temperature sensor. This isn't particularly accurate, and the device may also self-heat a little - but you can do it!
  • G-Force: Returns the absolute acceleration in g. Where the accelerometer example returns the acceleration components x, y, and z; this example combines the acceleration components into a single magnitude. This could be useful for detecting freefall, when g-froce is near or at zero.

If you have any questions or uncertainty, start the discussion below. We're full-time makers and here to help!

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.


Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.