PiicoDev Motion Sensor MPU-6050 - Micro:bit Guide

Updated 27 March 2023


This guide will help you read motion data from your PiicoDev® Motion Sensor and a Micro:bit.

To follow along, it's best to have:

We'll program with Thonny in this tutorial. If you haven't used Thonny before, check out our guide for Programming a Micro:bit with Thonny. If you prefer, you can program your Micro:bit using python.microbit.org instead (see our guide).

If you prefer not to use the Micro:bit adapter, there are other connection options in our PiicoDev Connection Guide.

Connect the PiicoDev sensor to your Micro:bit

Plug your Micro:bit into the PiicoDev adapter (buttons + LED matrix facing up), connect your motion sensor to the adapter via the PiicoDev cable and connect your Micro:bit to your computer with a USB lead.

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


Download the PiicoDev Modules and Example Code

Download the following files and save them to your working directory (Right Click > "Save Link As")

It will be best to keep this file 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 Micro:bit.

Open Thonny, connect to your Micro:bit and upload the three files. (Hint: View the files menu with View > Files)

Restart your Micro:bit (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.


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:

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.


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.