LSM303DLM - cannot get accurate readings

Hello,

I am trying to use a LSM303DLM digital compass with my robot to get its direction.

Some background

  • I am connecting it to an Arduino Mega. I am using the code provided on github (github.com/pololu/LSM303).
  • I have the compass mounted on a breadboard, which is placed on the robot close to the Arduino.
  • I have the LSM303DLM oriented on the breadboard like so: imgur.com/tvInAJb
  • A picture of the robot is here: imgur.com/2fzOn95
  • To calibrate the sensor, I run the “Calibrate” Arduino code. While it is running, I keep the robot stationary and then rotate the compass in a bunch of orientations while it is “inside” the robot. After this I place the values in the “Heading” Arduino code.

Problem

  • When I run the “Heading” Arduino code, I get confusing results. When moving the robot in a straight line, the heading value increases. When I rotate the robot in place, the angle’s aren’t correct either. For example, if I rotate the robot 180 degrees then the compass changes 160 degrees, or 200 degrees.
  • I have tried placing my Nexus 4 phone on the robot while running its compass app, and the app seems to give reasonable results.

Any pointers or help would be greatly appreciated.

If I understand your post correctly, then a better way to calibrate the compass would be to rotate the entire robot, with the compass attached to it in a fixed (and hopefully final) position. In this way, magnetic or iron objects in the robot have a fixed relationship to the compass.

Did you check whether the compass readings change when the motors are running, at various speeds and in different directions?