Raspberry Pi MPU-6000 6-Axis Motion Tracking Sensor Python Tutorial
by Dcube Tech Ventures in Circuits > Electronics
18 Views, 0 Favorites, 0 Comments
Raspberry Pi MPU-6000 6-Axis Motion Tracking Sensor Python Tutorial
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. Here is the demonstration with a python code using Raspberry Pi.
What You Need..!!
Connections:
Take a I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.
Then connect the one end of I2C cable to MPU-6000 sensor and the other end to the I2C shield.
Also connect the Ethernet cable to the pi or you can use a WiFi module.
Connections are shown in the picture above.
Code:
The python code for MPU-6000 can be downloaded from our GitHub repository-Dcube Store.
Here is the link for the same :
https://github.com/DcubeTechVentures/MPU-6000
We have used SMBus library for python code, the steps to install SMBus on raspberry pi is described here:
https://pypi.python.org/pypi/smbus-cffi/0.5.1
You can also copy the code from here, it is given as follows:
# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# MPU-6000
# This code is designed to work with the MPU-6000_I2CS I2C Mini Module available in Dcube Store.
import smbus
import time
# Get I2C bus
bus = 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
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.