I wanted to know the magnetic bearing for my airbot. So I bought a 3 axis magnetometer and got to work!
As I’ve previously mentioned, I purchased a SparkFun sensor pack. That also included the HMC5883L 3-axis magnetometer breakout from SparkFun.
Following the hookup guide (once I found it!) was ok – but yet again the raw numbers don’t really mean much.
Here’s what I had to do:-
- Configure the sensor
- Continuous measurement mode (set the 0x02 register to the value 0x00)
- Use 1.3 Ga as the accuracy rating (set the 0x01 register to the value 0x01 << 5 – has to be in the highest bits)
- Average the readings over a period of time (say 10 over a total of 0.1 seconds)
- Correct all readings for the actual position of my sensor
- Annoyingly, this breakout from SparkFun has a 90 degree different alignment to the accelerometer and the gyro sensors!
- So in my set up my x value is actually 0 – y, my y value is 0-x, and my z value is 0 – z. This is because I’m using a breadboard for testing.
- Scale the readings for my 1.3 Ga mode
- Multiple by 0.92 scale factor
- Calculate the magnetic bearing from the X and Y values (a bit of pythag / sohcahtoa)
- Convert from Radians to Degrees
All pretty straightforward. It’s really annoying though that the breakout is orientated differently from the other sensors.