Main Site Documentation

Compass module


I need a good compass for my RWAR project (see the RC car thread…).

Anybody have any suggestions? For right now I was thinking I might just get the Parallax HMC6352:

How sensitive will the compass be to the RC car’s motor?

EDIT: It’s probably worth noting that I need something pretty accurate. By how much, I don’t really know, but as long as the compass it pretty close it should be ok. I can always run a software filter on it.


Hi Chris ,

Have in mind that this sensor is not tilt compensated. So when you robot is only on a flat surface it is no problem but when you drive over a hill for example it could be a problem

Sparkfun has also the tilt-compensate version but it is much more expensive.


Wow thats more expensive than the cellular parts. How does tilt affect the compass?


I used this one which is the same chip -

Worked fine, but mind you mostly on a flat desk… not in a remote control car :slight_smile: If you tilt it (I can’t remember what the threshold was), it doesn’t work overly well so there’s a reason they make the tilt compensated ones… but at 4x the cost, ouch :frowning:

I had ported the code over from some other Basic derivative at the time to MikroeBasic and it wasn’t bad at all to read the sensor. I’m hoping to use one of these in a future project (too many lined up)… so if you post it, it would be appreciated!



Thanks for the input, guys.

I know that tilt compensation sensor is expensive (only $20 less than the @ #$#$^ car!!), but I don’t think I have any other choice.

Any other ideas?


What about getting a cheap one (no tilt)… and using a GPS to compensate? So if beyond a certain threshold, and it’s moving, use the GPS to find direction. Another option (though not sure how practical) would be to mount the module on a pivot like device (remember the old ship compasses?) so it’s constantly level regardless of the position of the vehicle.



Yeah, I thought about putting the compass on a pivoting mount running from the IMU feed, but that’s just one more failure point. I’d rather pay an extreme amount of money for a really good compass than try to bandaid a cheap one.

I suppose I could use GPS to compensate, but again, I’d like the compass to be able to find it’s own.


Honestly that’s probably not a bad idea… as it’s probably not worth the trouble. One less thing to break and less coding to worry about it :slight_smile: Plus if it’s stopped, on an incline, and using the GPS compensation method, it won’t be much use.



Nobody ever said this stuff would be cheap ::slight_smile:

Does anybody have any input on how the big motor on the RC car might affect the compass?


your IMU will have degrees as a calculation that is possible. Since the gyro in it will be able to handle tilt. When compass is in tilt use gyro as your calculation as you are already paying for it. If the gyro in side is a heading hold version then even better.


That’s another good point. What about using the tilt feed from the IMU to stabilize the compass? Is this even possible?


So what i mean is you will know your degree of tilt. Once off base you can use the heading of the gyro to determine your heading from last point that compass was non tilt. The degrees of tilt will effect the reading like 2 degrees per degree of tilt. So checking the gyro as a backup will allow you to know your true degrees of turn or if you are still on course once you detect tilt with it. Just sync up you compass and gyro on init which should happen on level land.


There is also a video from an engineer about the compass modules

At timecode 1:05 he shows the influence of tilts

So take a look :slight_smile:


@ bstag, Thanks. That’s very close to what I was thinking about doing. I might just do that instead.
@ cypher, Thank for the link. I have Sparkfun in my subscriptions, but for some reason that video didn’t show up when it came out. I’ll give it a though watch tomorrow!


What does everyone think of this: ?

I know, I know, that $150 compass is the one I should really be buying, but jeez, that is a lot of money to spend on a single sensor. Nevermind the fact that I still have to buy an IMU.


3 axis mag - To use so you have tilt compensation.

Example algorithm

def wrap(angle):
if angle > pi:
angle -= (2pi)
if angle < -pi:
angle += (2
if angle < 0:
angle += 2*pi
return angle

def magnetometer_readings_to_tilt_compensated_heading(bx, by, bz, phi, theta):
""" Takes in raw magnetometer values, pitch and roll and turns it into a tilt-compensated heading value ranging from -pi to pi (everything in this function should be in radians). “”"
variation = 4.528986*(pi/180) # magnetic variation for Corpus Christi, should match your bx/by/bz and where they were measured from
Xh = bx * cos(theta) + by * sin(phi) * sin(theta) + bz * cos(phi) * sin(theta)
Yh = by * cos(phi) - bz * sin(phi)
return wrap((atan2(-Yh, Xh) + variation))