Tutorial: How to read data from IMU

In the previous post I described how to setup raspberry pi for connection with the IMU.

Now it is time to see how to read some data from the sensor.

First yuo need sw that manage the i2c interface.there are many examples.You can find one called adafruit_i2c.py on github.

Then it is necessary to have the code specific for the sensor,in my case a MPU6050.

I tryied for some days to build my own code, but I encountered problems related to unconsistent results: even if I did not move the sensor, the returned results were always different. I suspect it was a problem on how I formatted the values.

Finally, Thanks to the great job done by Hove in  his blog, I used his code and I’m now able to collect correct data from the sensor.

I did some minor modification and prepare this IMU_test files.

So I started some preliminary tests to verify which is the sensor behaviour.

I fixed the sensor on a bar ,horizontally, than turned the bar by a known angle ( 13 degrees,measured with my smartphone level) then move back to horizontal.


I recorded on a file the sensor data : acceleration along axis (from ACCelerometer) and rotational speed (from GYRO scope). On excel sheet  I calculated the angle around x  respect the ACC and respect the GYRO:

  • rx ACC=DEGREES(ATAN2(accZ+9.8;accY))
  • rx(i) GYRO=wx(i) *dt+wx(i-1)

Below you can see the graph.


I underline in the picture the 2 tipical problems on the IMU :

  • the Gyro drift (you can see an angle of 1  degree while it reality it was 0)
  • the Accelerometer sensibility to noise.

So next  development step is to filter/reduce this 2 problems by combining the 2 sensors results.

4 thoughts on “Tutorial: How to read data from IMU

  1. Hove

    Hi Oscar – that looks like a great set of readings – my accelerometer was so full of noise I abandoned using it for the moment. Does your code use any different settings to mine (i.e the accelerometer range, or DLPF settings which might explain the difference). I’m looking forward to see how you merge the two sets of values too – again I’ve not bothered yet since again because my accelerometer was just swamped by noise!

    1. solenerotech Post author

      Hi hove. No setting changes respect your code. Take in consideration that those values are stored on a test session: no motor was running.the only differece i can see maybe is related how you calculate the angles from accel. data.
      Im now looking to see that happen if i aply a low pass filter and that happen using the kalman filter also.

      1. Hove

        Interested to hear your Kalman filter results – I looked at it for a few minutes once, but it was too hard for me! I hadn’t for enough to understand what it really did. I’ve got a little more of an idea now, so very interested to know how it works for you.
        Good luck!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s