0J4032.600Few days ago I was wondering about some challenging ideas. I’ve decided to write complete library for L3GD20 MEMS motion sensor. Moreover it would be my first Python script – that’s the challenge :) As in my previous articles, host device is Raspberry Pi Rev2. L3GD20 is a powerful chip. It can be installed as a separate element or as a part of bigger device like my MinIMU-9 v2 (http://www.pololu.com/catalog/product/1268) Original description can be found here: http://www.st.com/web/catalog/sense_power/FM89/SC1288/PF252443

But it’s key features are:

  • I2C or SPI.
  • Measurement scales 500/1000/2000 dps.
  • 3 gyro channels.
  • 16 bits value data output.
  • 8 bits temperature data output.
  • 3 power modes.
  • 5 operating modes.
  • Interrupt generation (highly configurable).
  • High pass and low pass filters.

I2C communication is very simple and there are many Python libraries that can help us. In this application, I will be using SMBus from http://www.acmesystems.it/i2c. There are many articles about how to connect MinIMU-9 (or other I2C device) toRaspberry Pi. I’ve found this one very helpful: http://marks-space.com/2013/04/29/guide-to-interfacing-a-gyro-and-accelerometer-with-a-raspberry-pi/ Article above and many others shows how to connect and read gyro values, but what if you want to configure other features like interrupts or filters ? That’s why I’ve decided to create full library. It allows you to control all L3GD20 device with human readable functions and enumerations instead of hex values hardcoded into main loop. Again, this is my first Python code, so, I hope you will like it :)


Cable connections are listed below.
MinIMU - Raspberry Pi Rev2
GND - Ground
VIN - Not connected
VDD - 3.3V Power

Raspberry Pi Configuration:

If you do not have your Raspberry Pi configured to use I2C communication bus, please follow the instructions below:

sudo apt-get install i2c-tools

Edit “/etc/modprobe.d/raspi-blacklist.conf” and comment line: “blacklist i2c-bcm2708”.

Edit “/etc/modules” and add two lines: “i2c-dev”, “i2c-bcm2708”.

While your Raspberry Pi will be rebooting, you have to download L3GD20 datasheet and navigate to page number 23. Because every I2C slave device has an address, you have to read it’s value from datasheet. It’s 1101010 or 1101011 (depending on SDO pin connection).

After reboot, please execute following test tool. One of addreses above should be on the list :)

sudo i2cdetect -y 1


The Library:

Whole library is accesible on my GitHub: https://github.com/mpolaczyk/Gyro-L3GD20-Python. Every register has it’s own Get and Set(if possible) method. Frequencies, modes and flags are represented by enumerations. This will cause that your code will be more friendly.

Example 1: Reading whole configuration:

Code can be found in Example_ReadConfig.py file.

from L3GD20 import L3GD20

# Communication object
s = L3GD20(busId = 1, slaveAddr = 0x6b, ifLog = False, ifWriteBlock=False)

# Preconfiguration

# Print current configuration
for a, b in s.ReturnConfiguration():
    print(str(a) + ': ' + str(b))

Effect – full configuration list:

confExample 2: Calibrating and reading gyro sensor data:


from L3GD20 import L3GD20
import time

# Communication object
s = L3GD20(busId = 1, slaveAddr = 0x6b, ifLog = False, ifWriteBlock=False)

# Configuration
s.Init() # Do measurements after Init!

# Calculate angle
dt = 0.02
x = 0
y = 0
z = 0
while 1==1:
    dxyz = s.Get_CalOut_Value()
    x += dxyz[0]*dt;
    y += dxyz[1]*dt;
    z += dxyz[2]*dt;
    print("{:7.2f} {:7.2f} {:7.2f}".format(x, y, z))



This shows that script contains of two parts:

  • Calibration – Gets max, min and mean from first 20 values. After calibration, readed values will be corrected (0 for values between min and max, valuemean for others).
  • Measurements loop – Gets angle values every ~20ms

Example 3: 3D Cube

This example is based on excellent article written by Leonel (http://codentronix.com/2011/04/21/rotating-3d-wireframe-cube-with-python). His trigonometric generator was replaced by a gyro, so cube can actually move as your sensor does. Example can be found on GitHub in file named: “Example_Plot3D.py” .

Enjoy! :)