Calibration
Why’s that the case? Turns out the magnetometer needs to be calibrated to return the correct answer.
The calibration involves quite a bit of math (matrices) so we won’t cover it here but thisApplication Note describes the procedure if you are interested. Instead, what we’ll do in thissection is visualize how off we are.
You should get an output in the console that looks like this:
You can pipe that to a file using:
Then import that TSV file into a spreadsheet program (or use the Python script shown below) and plotthe first two columns as a scatter plot.
Take home message: Don’t just trust the reading of a sensor. Verify it’s outputting sensible values.If it’s not, then calibrate it.