Python code Accelerometer gyrscope

Python Code for BerryIMU – Accelerometer, Gyroscope, Magnetometer & Pressure Sensor

We have updated our git repository with python code for the BerryIMU.

This is specific for the BerryIMU, however the math and code can be applied to any digital IMU, just some minor modifications need to be made. E.g  Pololu MinIMU, Adafruit IMU and Sparkfun IMUs

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone


We have left the code as simple as it can be to make it easier to understand.

The code currently performs angle measurements using the gyroscope and accelerometer , which are fused using a complementary filter. The heading is also calculated using the magnetometer, without tilt compensation.
BerryIMU Raspberry Pi Gyroscope Accelerometer
To view pressure;

pi@raspberrypi ~ $ sudo python

To view pressure;

pi@raspberrypi ~ $ sudo python

Detailed Guides and Tutorials

In this order;
Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi
Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi – Kalman Filter
Create a Digital Compass with the Raspberry Pi – Part 1 – “The Basics”
Create a Digital Compass with the Raspberry Pi – Part 2 – “Tilt Compensation”
Create a Digital Compass with the Raspberry Pi – Part 3 – “Calibration”


These cover both BerryIMU and displaying graphics with SDL;
How to Create an Inclinometer using a Raspberry Pi and an IMU

11 thoughts on “Python Code for BerryIMU – Accelerometer, Gyroscope, Magnetometer & Pressure Sensor”

    1. Hi Micheal
      In its current state, the code runs fine and gives accurate results.
      However, as you start to add more code the loop will take longer to run… resulting in the gyro drifting.

      Easy fix is to have this value;
      LP = 0.041 # Loop period = 41ms. This needs to match the time it takes each loop to run
      match the current the actual loop time. this is printed to the console when the code is running.

      There is also a sleep value you can remove… or play with. It is only there to try and make the output easier to read and to try and get a constant loop time.

      With my C implementation, I am able to have this all worked out automatically. I have yet to do it with Python. Python isn’t one of my strong points 🙁

  1. Not sure if anyone else had this problem but I had to run this to get the python code to work with a vanilla install of raspbian:

    sudo apt-get install python-smbus

  2. Is anyone else having issues with the gyro’s figures constantly accumulating with the code provided? Nothing else is, just the Gyro’s figures… Thanks!

    1. This is normal. This drift is a result of noise in the readings in the gyro. All gyros exhibit this behavior. Even gyros found in smartphones.
      All sensors are noisy. E.g. gyro, magnetometer, pressure, temperature, etc..
      A Gyro is noisy and will drift over long periods of time, but is very,very accurate for short periods.
      An accelerometer is also noisy, but has very good angle measurement for extended periods of time.

      This is why a complementary or Karman filter is used, to fuse both values to get a very accurate result.

      In the above code, the figures with the accurate values are CFangleX(Complimentary Filter AngleX) CFangleY(Complimentary Filter AngleY) and heading.

      This post covers it in more detail;

      Regarding the gryo, It is less of an issue when using C, as python is an interpreted scripting language, it is hard to run the loops at high speed and at a constant time period. Each loop time in my python code completes at a different time. If someone knows away around this, I would be happy to test it out.

  3. Little question, is it working for example if the BerryIMU is positionned vertically i.e in a plane containing z axis ? If not, what needs to be modified ?


Leave a Reply

Your email address will not be published. Required fields are marked *