Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

MinIMU-9 AHRS code


In your specific example, the Arduino compiler will throw an error when numbers other than zero or one are used with the “0b” prefix, since 2 is not a valid binary digit:

gyro.writeReg(L3G_CTRL_REG1, 0b00000112);  // this will not compile

Any number starting with “0b” or “B” is interpreted as a binary number by the Arduino compiler, and anything starting with “0x” is hexadecimal. You can read more about the way integer constants are interpreted here.


So I have tried out the latest version of v1.2.2 which supports LSM303 library 2.0.0, however, I have drift in Yaw. (It goes correctly to the right compass direction and gradually goes beyond it further and further).
With the previous version v1.2.1 which supports LSM303 library 1.4.4, there is no problem at all. (I did all the caliberation as guided).

So is anyone experiencing the same problem ? Since the MinIMU-9 AHRS code is pretty much the same, I suspect the major rewrite of LSM303 2.0.0 is not stable, isn’t it ?

I’d appreciate some comments or insights from Kevin or any other Pololu engineer working on this… Thanks. Anh.



Are you using the same calibration constants you were using with the older AHRS code and LSM303 library, or did you try running the calibration again with the new library? Which version of the MinIMU-9 and what type of Arduino are you using?

- Kevin


Hi Kevin,

Thanks for your reply during holiday season. Everytime I switch to a different version of LSM303 / AHRS, I re-calibrate the device. I’m using MinIMU-9-v2 and Arduino UNO R3.
I have double-checked this issue and still happening…


Ok, I might have to test it myself when I get a chance (likely later this week). It’s possible I broke something while changing the library for the 2.0.0 release.

- Kevin


I tried running a calibration with the latest LSM303 library (2.0.0) and using the resulting values in the latest AHRS program (1.2.2) and didn’t notice any problems. The compass calibration code and the way the compass data is used in the AHRS didn’t really change from the previous version, so I’m not sure why you’re seeing a difference in behavior.

Do you get significantly different calibration values when you calibrate with the old and new LSM303 libraries? Do you see differences in the yaw behavior if you use the same calibration values on the old and new versions of the AHRS program? How much does the yaw drift? (Is it just a small amount, like 5-10 degrees, or does it drift by a lot more?)

Did you ever change anything in the code (either old or new versions) besides the compass calibration values?

- Kevin



I am using the latest AHRS program and calibrated the compass. When I rotate the device on the spot, the returned yaw angle is working just fine with minor drifting. However, when i start to move the device in a translational manner, the yaw angle starts to change alot even if the device is facing the same direction. Is there something that I did it wrongly? Thank you.



I am sorry you are having trouble with that sensor. Can you tell me more about how are you moving it? How quickly are you moving it, and are you performing steady movements or accelerating and shaking the IMU a lot? After you stop moving the sensor, does the behavior of the yaw angle return to normal, or does it stay different? If you keep the sensor in place, but rotate it about the other two axes (pitch and roll), does the calculated orientation behave as expected?

By the way, which version of the IMU are you using? Do you have it mounted to anything? If so, how is it mounted? Is there anything nearby (such as magnetic or metal objects or wires carrying large currents) that could be affecting the magnetometer readings?




Thanks for the reply. I am using Arduino due. I move the sensor pretty slow, along a straight line. In the same time I move my handphone along the same straight line and check the reading on the compass. The sensor has drifted for around 30 degree but my handphone still remains the same. When I stop moving the sensor, the yaw angle does not return to normal. If I keep the sensor in place, and rotate it about the other two axes, the z-orientation is still changing while the readings of x,y orientation are normal(but when I do a 360 rotation on x,y axis the reading only change by 80-90). I am using the minimu9-v2 and it is mounted on a breadboard with its electronic parts facing upwards (I have change the code in the AHRS accordingly). There is nothing nearby other than arduino and wires. (And the handphone is not directly placed beside the sensor).


It seems weird that the calculated yaw is changing because that indicates that the magnetic reading is changing, independent of the calibration. There are a few things you can do that might help me determine what is going on:

Can you take your phone and place it in the exact spot you used for the IMU and move it along the exact path and see if the compass reading changes? (If it does, it might indicate that something is interfering with the magnetic reading like metal in the table or the surface the devices are resting on.)

Can you try running the LSM303 “heading” example (which only uses the magnetometer and accelerometer, not the gyro) and doing the same test and see if the reported heading changes?

Can you also post a picture that shows your entire setup?




I just do a thorough test with my phone’s compass, apparently the magnetic field in the room has an average of 40 micro T. And the reading does changing when I move transnational, the angle changes. I tired the heading example as well and the heading does change when I do the same movement. The attachment is the picture of my setup.

Thank you very much for your time :slight_smile:


A post was split to a new topic: MinIMU-9 v5 AHRS problems