Monday, November 26, 2012

Orientation sensing with the Raspberry Pi and MinIMU-9 v2

The Raspberry Pi is an ideal platform for experimenting with inertial measurement units (IMUs). These IMUs are usually I²C devices, so it is straightforward to access them using the Raspberry Pi's I²C bus. The integrated 3D graphics on the Raspberry Pi are useful for visualizing the results.

I have made some software that allows you to connect the MinIMU-9 v2 from Pololu Robotics & Electronics to your Raspberry Pi and calculate the orientation of the IMU. I also wrote a visualization program that shows the results in 3D. Check out the demo in this video:

The software is open source and I have written a tutorial for it. The only items needed besides your Raspberry Pi setup are:

Soldering is required: the male header pins that come with the MinIMU-9 v2 need to be soldered in.

The MinIMU-9 v2 can be conveniently powered from the Raspberry Pi's 3V3 power line. The photos below show how to connect a MinIMU-9 v2 to a Raspberry Pi:


The software consists mainly of two programs, both written in C++. The minimu9-ahrs program reads data from the MinIMU-9's sensors via I²C, calculates the orientation of the IMU, and outputs data to the standard output. Internally, it represents the orientation with a quaternion, but several different output options are available: direction cosine matrix, quaternion, euler angles, and raw sensor data. The ahrs-visualizer program reads direction cosine matrix data from its standard input and displays a 3D representation of the MinIMU-9 v2 using OpenGL. I also used libi2c, libpng, the Boost program_options library, and Eigen. There is a Python script that helps calibrate the magnetometer. For easy installation of the software, I have created Debian packages.

For more information, see the tutorial! I would love to see what people build with this, so please post comments below!


  1. Dear Mr.David, I did all the steps accordingly but I am still encountering a problem when I tried to install the package referring to 'dpkg -i minimu9-ahrs_VERSION_armel.deb' and I cannot find out what the problem is. Do you know where did I do wrong?

  2. Shahirah: Please create a new github issue here and give me all of the details about what you are doing what you are seeing:

  3. Dear Mr. I want to try this code with a microcontroller PIC, but I can´t written correctly can you help to write this code in C. for CCS ??? thank you.

    1. Sorry, this code is not well-suited for a PIC because it uses the Eigen library and lots of fancy features of C++. You would be better off looking at this code, which is similar:

  4. This comment has been removed by the author.

  5. Just wanted to say thanks for this, it's been very helpful :)

  6. Noobie here. Think I've got everything installed but when running minimu9-ahrs it fails to open ic2 device. I have the MinIMU-9 v2. and the latest pi. i2cdetect -y 1 shows the 3 addresses 19 1e and 6b for the MinIMU-9 v2.. also having trouble installing ahrs-visualizer. Suspect I'm having trouble with the basics. Just got started with the PI and all this about a week ago. Any help would be appreciated.

    1. You probably have a newer Raspberry Pi that exposes the pins for /dev/i2c-1 instead of /dev/i2c-0. You will have to provide the right options to my programs to use the correct I2C bus.

    2. I kinda figured my device wasn't the 0 but was 1 so I tried to tell it otherwise but this is probably where I have a big hole in my understanding and I'm probably entering the wrong thing. I put sudo minimu9-ahrs -b, /dev/12c-1 after the -b, I've tried many thinks like --12c-1 --/dev/i2c-1 /dev/i2c-1 i2c-1 but I really don't understand the syntax and/or may be using the wrong command all together. I get errors like no such option, no such file or directory etc. Thanks for your come back. This old guy appreciates it!

    3. My memory is foggy too. Did you try "--help"? What did it say?

  7. This comment has been removed by the author.

  8. I've been looking at "man minimu9-ahrs" for help. Just now looked at minimu9-ahrs --help and get similar stuff. I've tried the following but even after looking at the help I'm not really sure of the syntax. like single - or double dash or commas -b or arg just not really sure of what I should use. for example if I type the following:
    "sudo minimu9-ahrs -b, --/dev/i2c-1" gives me
    "Error: unknown option /dev/i2c-1" If I try
    "sudo minimu9-ahrs -b, /dev/i2c-1" gives me
    "Error: Failed to open I2C device.: No such file or directory No such file or directory (system:2)" and the help is similar to the man stuff
    "sudo minimu9-ahrs --help" gives me
    "Allowed options:
    -h [ --help ] produce help message
    -v [ --version ] print version number
    -b [ --i2c-bus ] arg (=/dev/i2c-0) i2c-bus the IMU is connected to
    --mode arg (=normal) specifies what algorithm to use.
    normal: Fuse compass and gyro.
    gyro-only: Use only gyro (drifts).
    compass-only: Use only compass (noisy).
    raw: Just print raw values from sensors.
    --output arg (=matrix) specifies how to output the orientation.
    matrix: Direction Cosine Matrix.
    quaternion: Quaternion.
    euler: Euler angles (yaw, pitch, roll).

    For more information, run: man minimu9-ahrs"
    So am I typing stuff in wrong or just don't have a clue?

    1. Except for the --help, the stuff you tried simply doesn't look valid. Why did you put a comma after b? What about "-b /dev/i2c-1" ?

    2. This comment has been removed by the author.

    3. This comment has been removed by the author.

  9. This comment has been removed by the author.

  10. This comment has been removed by the author.

  11. This comment has been removed by the author.

  12. Hello David,
    I am using a Raspberry Pi and the MinIMU-9 v2 module for controlling an autonomously flying model airplane.
    I have compiled the program from source code and everything seems to work fine but there is one problem I have: The MinIMU module is mounted into the airplane rotated 180° about the X-axis (X=X, Y=-Y, Z=-Z). What can I do to get the right output? Change the source code? Where?

    I would appreciate your help.
    Best regards
    Wolfgang from Germany

  13. Hi Dave, I'm pretty new to Raspberry Pi. I'm a software engineer by profession and have tinkered with electronics from time to time as well. I'm looking at reverse engineering the Technomancer Hoodie that ThinkGeek sells into a custom Halloween costume with a bit more control and functionality. I haven't sourced or picked up any components yet since I'm still in the research phase. This project looks like a great place to start. I was wondering though if it's possible to connect TWO IMUs to the Raspberry Pi? It looks like GPIO pins 0 and 1 are the only pins suited but I could be wrong.

    The idea is to attach one IMU to each wrist with a wrist band and track the movements of each arm.

    1. The Raspberry Pi actually has two I2C busses (/dev/i2c-0 and /dev/i2c-1) but I think one of them is on a different connector or maybe no connector at all. And one annoying thing is that they switched the I2C interfaces between revisions. If you can figure out a physical solution to getting access to the other I2C bus, then you should be able to use two instances of my software running on two different busses.

      Since you and I are a software guys though, maybe it would better to find some library for the Raspberry Pi that does software I2C on two of the other GPIOs. When you are the master in an I2C bus, timing is not very important and you could implement a bit-banging I2C thing from user space in any language. Someone must have done this already.

    2. A few Google searches led me to this:

  14. I am using two minimu-9 v2 sensor.I use 4052 multiplexer but it didnt work.I didnt solve problem.I controlled two servos with two pot that using multiplexer.Can you any sugesstion?

  15. Hi Mr.David,
    I am an experienced Italian helicopters, Raspberry use to interface the weather station and webcam present in an oil rig.
    I would like to add the ability to display also the pitch and roll and heading of the platform and then access the data via the web in my site .... is it possible using its software generating a web page?
    Thank you for your attention ...

    Christian D.