Skip to content
Michal Belica edited this page Aug 3, 2017 · 5 revisions

IMU sensors project wiki

This page describes how to use IMU sensors Python classes with Raspberry Pi.

Hardware

The project was developed with IMU Digital Combo Board -- a nice breakout board containing I2C connected ADXL345 digital accelerometer and ITG3200 digital gyroscope.

Wiring

To use the sensors with Raspberry Pi, just connect pins on the IMU board labeled SDA, SCL, 3.3V and GND to same labeled pins on the Raspberry Pi GPIO header.

Software

First you need to enable I2C kernel modules.

Install python-smbus package and optionally i2c-tools package which contains i2cdetect command line utility.

To find I2C addresses of the sensor devices, with the sensors connected, on the command line run

# 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: -- -- -- 53 -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

From the output you can see the addresses. In my case, 0x53 is the accelerometer and 0x68 the gyroscope.

Note: on 512MB Raspberry Pi Model B the I2C on the GPIO header is connected to bus 1, but on the earlier 256MB model, it is connected to bus 0, so you'll have to use i2cdetect -y 0

Example code

Read accelerometer data values:

import time
from adxl345 import SensorADXL345

sensor = SensorADXL345(1, 0x53) # replace with your bus number and address
sensor.default_init()
time.sleep(0.1)
ax, ay, az = sensor.read_data()
sensor.standby() # put the sensor into standby when you're done reading for a while
print ax, ay, az

Read gyroscope data values:

import time
from itg3200 import SensorITG3200

sensor = SensorITG3200(1, 0x68) # replace with your bus number and address
sensor.default_init()
time.sleep(0.1)
gx, gy, gz = sensor.read_data()
print gx, gy, gz
Clone this wiki locally