Introduction: Motion Tracking Using MPU-6000 and Raspberry Pi

MPU-6000 is a 6-Axis Motion Tracking Sensor which has 3-Axis accelerometer and 3-Axis gyroscope embedded in it. This sensor is capable of efficient tracking of exact position and location of an object in the 3-dimensional plane. It can be employed in the systems which require position analysis to the highest precision.

In this tutorial the interfacing of the MPU-6000 sensor module with raspberry pi has been illustrated. To read the values of acceleration and rotational angle, we have used raspberry pi with an I2c adapter.This I2C adapter makes the connection to the sensor module easy and more reliable.

Step 1: Hardware Required:

Picture of Hardware Required:

The materials that we need for accomplishing our goal includes the following hardware components:

1. MPU-6000

2. Raspberry Pi

3. I2C Cable

4. I2C Shield for raspberry pi

5. Ethernet cable

Step 2: Hardware Hookup:

Picture of Hardware Hookup:

The hardware hookup section basically explains the wiring connections required between the sensor and the raspberry pi. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:

The MPU-6000 will work over I2C . Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.

Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic.

All you need is four wires! Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.

These connections are demonstrated in the pictures above.

Step 3: Code for Motion Tracking:

Picture of Code for Motion Tracking:

The advantage of using raspberry pi is, that is provides you the flexibility of the programming language in which you want to program the board in order to interface the sensor with it. Harnessing this advantage of this board, we are demonstrating here its programming in the python. Python is one of the easiest programming languages with easiest syntax. The python code for MPU-6000 can be downloaded from our github community that is Control Everything Community.

As well as for the ease of the users, we are explaining the code here also:

As the first step of coding you need to download the SMBus library in case of python, because this library supports the functions used in the code. So, to download the library you can visit the following link:

https://pypi.python.org/pypi/smbus-cffi/0.5.1

You can copy the working code from here also:

import smbus

import time

# Get I2C busbus = smbus.SMBus(1)

# MPU-6000 address, 0x68(104)

# Select gyroscope configuration register, 0x1B(27)

# 0x18(24) Full scale range = 2000 dps

bus.write_byte_data(0x68, 0x1B, 0x18)

# MPU-6000 address, 0x68(104)

# Select accelerometer configuration register, 0x1C(28)

# 0x18(24) Full scale range = +/-16g

bus.write_byte_data(0x68, 0x1C, 0x18)

# MPU-6000 address, 0x68(104)

# Select power management register1, 0x6B(107)

# 0x01(01) PLL with xGyro reference

bus.write_byte_data(0x68, 0x6B, 0x01)

time.sleep(0.8)

# MPU-6000 address, 0x68(104)

# Read data back from 0x3B(59), 6 bytes

# Accelerometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB

data = bus.read_i2c_block_data(0x68, 0x3B, 6)

# Convert the data

xAccl = data[0] * 256 + data[1]

if xAccl > 32767 :

xAccl -= 65536

yAccl = data[2] * 256 + data[3]

if yAccl > 32767 :

yAccl -= 65536

zAccl = data[4] * 256 + data[5]

if zAccl > 32767 :

zAccl -= 65536

# MPU-6000 address, 0x68(104)

# Read data back from 0x43(67), 6 bytes

# Gyrometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB

data = bus.read_i2c_block_data(0x68, 0x43, 6)

# Convert the data

xGyro = data[0] * 256 + data[1]

if xGyro > 32767 :

xGyro -= 65536

yGyro = data[2] * 256 + data[3]

if yGyro > 32767 :

yGyro -= 65536

zGyro = data[4] * 256 + data[5]

if zGyro > 32767 :

zGyro -= 65536

# Output data to screen

print "Acceleration in X-Axis : %d" %xAccl

print "Acceleration in Y-Axis : %d" %yAccl

print "Acceleration in Z-Axis : %d" %zAccl

print "X-Axis of Rotation : %d" %xGyro

print "Y-Axis of Rotation : %d" %yGyro

print "Z-Axis of Rotation : %d" %zGyro

The code is executed using the following command:

$> python MPU-6000.py

The output of the sensor is shown in the picture above for the reference of the user.

Step 4: Applications:

Picture of Applications:

MPU-6000 is a motion tracking sensor, which finds its application in the motion interface of smartphones and tablets. In smartphones these sensors can be employed in the applications such as gesture commands for applications and phone control, enhanced gaming, augmented reality, panoramic photo capture and viewing, and pedestrian and vehicle navigation. MotionTracking technology can convert handsets and tablets into powerful 3D intelligent devices that can be used in applications ranging from health and fitness monitoring to location-based services.

Comments

About This Instructable

1,048views

4favorites

More by Dcube Tech Ventures:Home Automation with Raspberry Pi using Relay boardMotion Control with Raspberry Pi and LIS3DHTR, 3-Axis Accelerometer, using Python3-Axis Accelerometer, ADXL345 with Raspberry Pi using Python
Add instructable to: