Measuring Rotation and acceleration with the Raspberry Pi

Acceleration and rotation sensors are most known from smartphones. The rotation of the device can be detected and can be addressed directly.

With the Raspberry Pi and a Gyroscope / Accelerator sensor it is possible to measure the rotation as well as the acceleration of the 3 axes.
In this tutorial I show how to use the MPU 6050 and how to read the values using I2C, which is demonstrated it in a video.


For this tutorial I have used the following components:

The module has 2 pin headers, one of those is bent. I’ve soldered the bent pins to the sensor board, which saves space (see picture):




The practical thing about I2C is that very few pins are used. Of the eight pins of the sensor, we only need to connect the upper 4:

Raspberry Pi MPU 6050
Pin 1 (3.3V) VCC
Pin 3 (SDA) SDA
Pin 5 (SCL) SCL
Pin 6 (GND) GND




Activate I²C on the Raspberry Pi

First we enable SPI and I2C. If you have already done this in a previous tutorial, you can skip this step.

sudo raspi-config

Here we find the menu for activating services. Under “8. Advanced Options” there is the entry “A7 I2C”, which we activate (This can differ in other Raspbian versions). For older Raspbian versions, the entries in the /etc/modprobe.d/raspi-blacklist.conf file must be excerpted (with #).

We then edit the modules file:

sudo nano /etc/modules

If the following lines are not already included, add them and restart the Pi (sudo reboot):

Now we can quickly install the necessary tools:

sudo apt-get install i2c-tools python-smbus

Let’s start a small test. The parameter -y 1stands for revision 2. If you have a completely old Pi (before 2013), you would have to specify a 0 instead:

sudo i2cdetect -y 1

If the gyroscope is properly connected, you will see this output (if you have other I2C modules connected, their hex addresses should be displayed):

pi@raspberrypi ~ $ sudo i2cdetect -y 1
 0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

To address the device with address 68 (Attention: Hexadecimal) under the register, enter the following:

sudo i2cget -y 1 0x68 0x75


Read the MPU-6050 Modul

The most convenient way to read the acceleration sensor is probably Python. Therefore, we create a file and paste the following code.

sudo nano

Save it with CTRL + O and exit the editor with CTRL + X. You can then run the script.

sudo python

Now you’ll see an output that contains all the captured data:

gyroskop_xout:   -260  skaliert:  -2
gyroskop_yout:   -154  skaliert:  -2
gyroskop_zout:     78  skaliert:  0

beschleunigung_xout:   -1048  skaliert:  -0.06396484375
beschleunigung_yout:    -676  skaliert:  -0.041259765625
beschleunigung_zout:   16644  skaliert:  1.01586914062
X Rotation:  -2.32121150537
Y Rotation:  3.59994842011

If you want to know more about accelerometers and gyroscopes, you should read this article.

Finally, a small sample video demonstrating the use of the sensor live:


15 Responses

  1. Hi, I was going through this article to make a POC for my self and it was really helpful. I am very impressed by the video demonstration of the sensor and want to do similar thing where a image is mimicking the sensor movement on screen as shown in video.

    Can you please provide me the code for that part as well ? I will really appreciate it. You can share the code at :

  2. hai there, i am just trying this module and your tutorial is very helpful thank you. but can i ask? how u show the data on 3D? what software that you used. i this its more good if i can see data on 3D like your video.

  3. hi there,,,
    i a trying to run this code bus smbus is not working , error says that ” no module named as smbus”… can you help me out in this?

  4. hi it works but I cant understand these lines of code
    power_mgmt_1 = 0x6b
    power_mgmt_2 = 0x6c

    def read_byte(reg):
    return bus.read_byte_data(address, reg)

    def read_word(reg):
    h = bus.read_byte_data(address, reg)
    l = bus.read_byte_data(address, reg+1)
    value = (h << 8) + l
    return value

  5. Can any explain where all the integer in the code came from?
    for instance:
    1. 8000 & 65535 in the “read_word_2c(reg)” function
    2. 131 in “gyroskop_xout:”
    3. 16384 in “beschleunigung_xout_skaliert”

    This might be intuitive for someone with a strong background but I don’t have any clue on how to dig deeper for the meaning of those numbers. Anyone mind explaining would be very much appriciated

    • 1. 0x8000 is a hexadecimal literal meaning 1000 0000 0000 0000 in binary, and 65535 is 2 to the 16th power minus one, making it the largest unsigned integer than can be encoded in 16 bits. This is being done because the bus data is in two’s complement (hence the “2c” in “read_word_2c”)
      2 & 3. I believe these are conversions from the 16 bit output to readable output

  6. Hello,

    I’m trying to connect two MPU9250 in my rapsberry pi. So i’m trying to change the address of one MPU. I had connected the AD0 pin to 3.3V but it’s still in the 0x68 direction. Should i do something more??

  7. Hello,
    my return values are all zero, checked twice connection, prerequisites and code.

    Any Idea

  8. I have a problem with MPU6050 gyro sensors, I am making school project with Vehicle Accident GPS tracking. When the normal condition, eventhing is ok. Gyro is working his axis,but when over limit gyro’s x,y,z values, LCD and Serial monitor is stop their process. how can i do that?


  9. hi, i am working with accelerometer output of anroide mobile like that

    x = 1.0
    y = 1.30
    z = 9.4

    using this data how can i predict that

    1. mobile is in vertical position or not
    2. mobile is falling down get carry out by human


Leave a Comment

Your email address will not be published.

Subscribe to Raspberry Pi Tutorials and don't miss any new Tutorial!