PiicoDev Motion Sensor MPU-6050 - Raspberry Pi Pico Guide

Updated 27 March 2023

Introduction

This guide will help you read motion data from your PiicoDev® Motion Sensor and a Raspberry Pi Pico

To follow along, it's best to have:

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

Connect the PiicoDev sensor to your Pico

Plug your Pico into the Expansion Board, connect your distance sensor to the Expansion Board via the PiicoDev cable, and finally connect your Pico to your computer with a USB lead.

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

piicodev-motion-sensor-mpu6050-connections-rpi-pico

Download MicroPython modules

We will need three files to easily read data from the Motion Sensor:

  • Download the PiicoDev Unified LibraryPiicoDev_Unified.py (right-click, "save link as").
  • Download the device module: PiicoDev_MPU6050.py (right-click, "save link as")
  • Download the example script: main.py (right-click, "save link as")

It will be best to keep these files wherever you like to keep your coding projects eg. Documents > PiicoDev

Accelerometer Example

We'll be working in Thonny - if you're unfamiliar working with Thonny see our guide for Thonny and Raspberry Pi Pico.

Open Thonny, connect to your Pico and upload the three files (that we just downloaded) to your Pico. (Hint: View the files menu with View > Files)

Restart your Pico (Keyboard shortcut, Ctrl+D) and 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.

piicodev-motion-sensor-mpu6050-example-script-raspberry-pi-pico

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:

example-motion

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.

acceleration-plot

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:

uncomment-next-example

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

z-axis-rotation    angular-velocity-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.

Feedback

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.