Introduction: Motion Tracking Using MPU-6000 and Raspberry Pi

About: We are a group of makers. We work in IoT, IOS app, android app, embedded design, sensor design, raspberry pi, arduino, beaglebone, particle electron, particle photon, Bluetooth.

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:

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:

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:

The advantage of using raspberry pi is, that 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 Dcube Store

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:

You can copy the working code from here also:

<p>import smbus</p><p>import time</p><p># Get I2C busbus = smbus.SMBus(1)</p><p># MPU-6000 address, 0x68(104)</p><p># Select gyroscope configuration register, 0x1B(27)</p><p>#		0x18(24)	Full scale range = 2000 dps</p><p>bus.write_byte_data(0x68, 0x1B, 0x18)</p><p># MPU-6000 address, 0x68(104)</p><p># Select accelerometer configuration register, 0x1C(28)</p><p>#		0x18(24)	Full scale range = +/-16g</p><p>bus.write_byte_data(0x68, 0x1C, 0x18)</p><p># MPU-6000 address, 0x68(104)</p><p># Select power management register1, 0x6B(107)</p><p>#		0x01(01)	PLL with xGyro reference</p><p>bus.write_byte_data(0x68, 0x6B, 0x01)</p><p>time.sleep(0.8)</p><p># MPU-6000 address, 0x68(104)</p><p># Read data back from 0x3B(59), 6 bytes</p><p># Accelerometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB</p><p>data = bus.read_i2c_block_data(0x68, 0x3B, 6)</p><p># Convert the data</p><p>xAccl = data[0] * 256 + data[1]</p><p>if xAccl > 32767 :	</p><p>xAccl -= 65536</p><p>yAccl = data[2] * 256 + data[3]</p><p>if yAccl > 32767 :	</p><p>yAccl -= 65536
</p><p>zAccl = data[4] * 256 + data[5]</p><p>if zAccl > 32767 :	</p><p>zAccl -= 65536</p><p># MPU-6000 address, 0x68(104)</p><p># Read data back from 0x43(67), 6 bytes</p><p># Gyrometer X-Axis MSB, X-Axis LSB, Y-Axis MSB, Y-Axis LSB, Z-Axis MSB, Z-Axis LSB</p><p>data = bus.read_i2c_block_data(0x68, 0x43, 6)</p><p># Convert the data</p><p>xGyro = data[0] * 256 + data[1]</p><p>if xGyro > 32767 :	</p><p>xGyro -= 65536</p><p>yGyro = data[2] * 256 + data[3]</p><p>if yGyro > 32767 :	</p><p>yGyro -= 65536</p><p>zGyro = data[4] * 256 + data[5]</p><p>if zGyro > 32767 :	</p><p>zGyro -= 65536</p><p># Output data to screen</p><p>print "Acceleration in X-Axis : %d" %xAccl</p><p>print "Acceleration in Y-Axis : %d" %yAccl</p><p>print "Acceleration in Z-Axis : %d" %zAccl</p><p>print "X-Axis of Rotation : %d" %xGyro</p><p>print "Y-Axis of Rotation : %d" %yGyro</p><p>print "Z-Axis of Rotation : %d" %zGyro</p>

The code is executed using the following command:

$> python
gt; python

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

Step 4: 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.