# [Compass] Easy Magnetometer Calibration

[caption id="attachment_998" align="aligncenter" width="400" caption="Initial test. See how the center of the circle does -not- lie on 0,0 as it should"][/caption] [caption id="attachment_999" align="aligncenter" width="400" caption="Result after applying a X-20 and Y-80 in the code."][/caption] So, just check where the center of the circle is, and then check where it should be: then apply a little + or – here and there by eye; and you’re good to go. If it looks like an egg then you should apply some division or multiplication, but that’s even obvious to a 4-year old.

## Code for 2 Axis

Simple Matlab code:plot(data(:,1),data(:,2),'r.')

`axis equal`

ylabel('Magnetic signal Y [8-bit gauss]');

xlabel('Magnetic signal X [8-bit gauss]');

`grid on`

## Code for 3 Axis

Simple Matlab code:plot3(data(:,1),data(:,2),data(:,3),'r.')

`axis equal`

ylabel('Magnetic signal Y [8-bit gauss]');

xlabel('Magnetic signal X [8-bit gauss]');

`grid on`

Thanks!!! It really helped a lot! I am getting much better results even from the IMU embedded filter.

Hi there, sorry but i’m kind of new with matlab, so i’ve made an “m” file with you code, but when i’ve tried to run it, it says that there is no data.

So what should i do?, i mean, i have a hmc5883L magnetometre, i’ve made the code for the self test of the sensor and i’ve got the x, y and z values, then in a text file i’ve written with comma as you said, then exported those data, and last tried to run you code but it doesn’t work.

So please can you explain it more clearly all the steps?

Thank you.