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.

Components

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):

20151125_205159

 

Setup

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

 

GY-521_accelerometer_Gyroscope_Steckplatine

 

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 gyro.py

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

sudo python gyro.py

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

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

Beschleunigungssensor
---------------------
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:

 

6 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 : sharma.swapn@gmail.com

    Reply
  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.

    Reply

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

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