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 use the MPU 6050 Gyroscope / Accelerator sensor interferfaced to a Raspberry Pi, and reads the values using I2C.

Components:

For this tutorial I have used the following components:

  • Raspberry Pi
  • MPU 6050 sensor module
  • Jumper Cables

MPU-6050 Module

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:

MPU-6050 Module Pinout

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

MPU-6050 Module to Raspberry Pi

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

1
2
i2cbcm2708
i2cdev

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

MPU6050 Gyroscope Raspberry Pi

Read the MPU-6050 Module:

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

Python Script:

#!/usr/bin/python
import smbus
import math
 
# Register
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 def read_word_2c(reg): val = read_word(reg) if (val >= 0x8000):
        return -((65535 - val) + 1)
    else:
        return val
 
def dist(a,b):
    return math.sqrt((a*a)+(b*b))
 
def get_y_rotation(x,y,z):
    radians = math.atan2(x, dist(y,z))
    return -math.degrees(radians)
 
def get_x_rotation(x,y,z):
    radians = math.atan2(y, dist(x,z))
    return math.degrees(radians)
 
bus = smbus.SMBus(1) # bus = smbus.SMBus(0) fuer Revision 1
address = 0x68       # via i2cdetect
 
# Aktivieren, um das Modul ansprechen zu koennen
bus.write_byte_data(address, power_mgmt_1, 0)
 
print "Gyroscope Sensor"
print "--------"
 
gyroscope_xout = read_word_2c(0x43)
gyroscope_yout = read_word_2c(0x45)
gyroscope_zout = read_word_2c(0x47)
 
print "gyroscope_xout: ", ("%5d" % gyroscope_xout), " scaled: ", (gyroscope_xout / 131)
print "gyroscope_yout: ", ("%5d" % gyroscope_yout), " scaled: ", (gyroscope_yout / 131)
print "gyroscope_zout: ", ("%5d" % gyroscope_zout), " scaled: ", (gyroscope_zout / 131)
 
print
print "Accelerometer Sensor"
print "---------------------"
 
acceleration_xout = read_word_2c(0x3b)
acceleration_yout = read_word_2c(0x3d)
acceleration_zout = read_word_2c(0x3f)
 
acceleration_xout_scaled = acceleration_xout / 16384.0
acceleration_yout_scaled = acceleration_yout / 16384.0
acceleration_zout_scaled = acceleration_zout / 16384.0
 
print "acceleration_xout: ", ("%6d" % acceleration_xout), " scaled: ", acceleration_xout_scaled
print "acceleration_yout: ", ("%6d" % acceleration_yout), " scaled: ", acceleration_yout_scaled
print "acceleration_zout: ", ("%6d" % acceleration_zout), " scaled: ", acceleration_zout_scaled
 
print "X Rotation: " , get_x_rotation(acceleration_xout_scaled, acceleration_yout_scaled, acceleration_zout_scaled)
print "Y Rotation: " , get_y_rotation(acceleration_xout_scaled, acceleration_yout_scaled, acceleration_zout_scaled)

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:

Gyroscope
--------
gyroscope_xout:   -260  scaled:  -2
gyroscope_yout:   -154  scaled:  -2
gyroscope_zout:     78  scaled:  0

Accelerometer
---------------------
acceleration_xout:   -1048  scaled:  -0.06396484375
acceleration_yout:    -676  scaled:  -0.041259765625
acceleration_zout:   16644  scaled:  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.

Short URL: http://tinyurl.com/ybcwk7cd
   Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *

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

Do NOT follow this link or you will be banned from the site!