# How do I determine the distance from a magnet using Magnetometer?

I have a triple-axis magnetometer and a magnet, and I am trying to calculate the relative position of the magnet from the magnetometer.

The magnetometer outputs the strength of any fields detected in each of the (X,Y,Z) directions, and I understand that I'll have to compensate for the Earth's own field, as well as how to determine the relative location of the magnet, but what I do not understand is how to determine the distance that the magnet is from the magnetometer.

So my question is: assuming that I have a magnetometer pointed exactly straight towards a magnet, and the magnetometer is outputting the field strength, is there an equation that I can use to determine the distance that they are apart?

I have attempted doing some research into this, but all the results I am finding vary from one another incredibly. Any help/advice would be much appreciated.

Edit 1: I feel like I should note that I'm using a cylindrical magnet. Would the calculations be different if I were to use a rectangular one?

• This would be drastically dependent on the way the magnet is polarized. – Dzarda Aug 27 '14 at 11:46
• And the strength of other surrounding magnetic fields... – Matt Young Aug 27 '14 at 18:31

No, you can't derive the distance to a magnet from a single field strength measurement without knowing the strength of the magnet.

However, you can derive distance from two readings at different distances from the magnet. Magnetic field strength falls off with the square of the distance. By moving a small and known amount towards or away from the magnet, you can calculate the distance to the magnet by how much the field strength changed over the know distance between the two measurements.

For example, if you moved 1 m closer to a magnet and the field strength quadrupled, then the magnet must have been 2 m away from the original measuring point. If the field strength only doubled, then you moved 1/sqrt(2) closer to the magnet. The 1 m closer was therefore .29 of the distance to the magnet, which means the magnet was 3.4 m from the original measurement.

• Ok, so assuming I did know the strength of the magnet, and I also had two magnetometers, this would be possible using a squared relationship? Say I had a set-up along the lines of: Meter 1 ----- Magnet ----- Meter 2, where meter 1 and meter 2 were a known distance apart, as I increased the distance of the magnet from meter 1, the field strength read by meter 1 would decrease by something like 1/d^2, where as the strength read by meter 2 would increase by the same factor. Is that correct? Or have I misinterpreted? – Kadin Aug 28 '14 at 2:23
• @user: That sounds right. – Olin Lathrop Aug 28 '14 at 12:35
• @user2222956, the field from a dipole, (a simple magnet is a dipole.) will drop off as 1/d^3. Google dipole field pattern. – George Herold Aug 28 '14 at 13:38
• Ok, so is it 1/d^2, or 1/d^3, because now I'm unsure. I've done more research, and different sources give conflicting information, some saying it's d^2, the others saying d^3. What's the difference and which is more accurate? – Kadin Sep 4 '14 at 4:36

It all comes down to how strong the magnet is. If you don't know the strength of the magnetic field close up then use the magnetometer to get a "short (or zero) distance reading". From this you can compute distances against field strength measured.

The shape of the magnet can also influence how the field reduces as you back-away from it and the formulas can be difficult to decipher given the various shapes of magnet that you could have and the direction it is polarized.

This online calculator may be helpful

In reality, there is no equation that can calculate this. You have to measure it in situ. There will be offset from Earth's field and also offset from the sensor itself, which has to be corrected at every power-on.

For example, LIS3MDL has zero-gauss level of $\pm 1\, \mathrm{G}$, which is pretty attrocious. And that is only typical value. But once it is zeroed out, it drifts little over time (tested for several minutes).

Sensitivity may be also off by tens of percents.

That is why (among other reasons) compasses in mobile phones have to be calibrated by figure 8 before use.

Your magnet has a magnetic dipole moment mu (u). For a current loop the dipole moment is the (area) * (current)* (number of turns). (u=N*I*A) If you knew the dipole moment of your magnet, then "in theory" you can calculate the B field it produces at all points in space. For points very close to the magnet.. say some distance that is about the size of the magnet, this dipole approximation will break down. But other than that, you are all set, the exact shape of the magnet won't really matter, only it's magnetic moment. (A good freshman physics text will give you the field along the axis of the dipole.) For points along the axis the field will decrease as the third power of the distance. Now in practice you have to deal with other B fields, (like that of the Earth), and also any pieces of magnetic material that are in the area will distort the dipole field of your magnet.

Edit: I just wanted to add that the B field from a magnetic dipole looks just like the E field from an electric dipole. I don't think you should have any trouble doing the measurements. (Well find a non-magnetic table and hold all the orientations constant.)