Problem solved, right? We can find our location with the GPS, and magnetic north with the AK8973 chip. A quick table lookup, perhaps with some interpolation if we want to get fancy, and we have the direction of true north. Well, yes—but the problem is that the AK8973 chip won’t really give us magnetic north. It’s trapped inside a metal box, with currents flowing all around it and a speaker magnet nearby. All of those things affect the chip, altering the direction it reports as magnetic north. Fortunately, the currents, case and speaker magnets don’t move around, so we can calculate their effects on the field once and adjust for it. This is called the magnetic deviation, and it’s an even bigger problem in places like airplanes, which have even bigger metal boxes surrounding the compass. We’re going to finesse this problem for the metal detector, but it’s one you have to deal with for other magnetometer applications—more on that later.

Using the iPhone or iPad as a Metal Detector

Earlier we talked about the fact that currents can create magnetic fields, and magnetic fields can create currents. Metal also affects a magnetic field. All those mobile electrons bend the field a bit, changing its strength and direction. And that’s how we’re going to turn your iPhone into a metal detector.

First, let’s do a quick experiment to find out if all of this is possible. The Earth’s magnetic field strength varies from place to place. Here in Albuquerque, it’s about 50µT. Can a small piece of metal distort such a small magnetic field enough to be useful? I ran the Magnetometer sample from techBASIC 1.1 and passed my iPhone about 6 inches over my aluminum-topped keyboard to find out. Here’s what I saw:

OK, that’s promising. The big spike occurred just as I hit the edge of the keyboard. While the early experiment is promising, this is a case of too much information. I don’t really want the magnetic field in three directions; turning the iPhone as I move it could give me a false positive. Here’s what I get just from turning the iPhone.

At may not be obvious yet, but here we are seeing two effects on the magnetometer. The first, and biggest, comes from simply twisting the detector in the field. As you can see, for the most part, I was twisting the iPhone about the X axis, changing the field strength in the Y and Z directions, and these more or less counterbalance one another. That’s what we would expect. There is another effect that is hard to see here, but that we will return to in a moment. In any case, it’s clear that our magnetometer would be a lot more useful if we could see the overall field strength instead of the strength along each axis. We can find the overall field strength by resolving the three vectors into a single magnitude

            m = sqr(x*x + y*y + z*z)

where x, y and z are the magnetic field strengths along each axis, and m is the overall field strength.

Converting the Magnetometer Sample Into a Metal Detector

To create a new program based on this idea, I copied the Magnetometer sample. Here are the details, which you can skip if it’s obvious to you.

The next step is to modify the magnetometer sample to show the overall magnitude of the magnetic field instead of the components along each axis. There are different ways to do this. I deleted the two extra graphs, changed a couple of labels and comments, and plotted the magnitude of the total magnetic field instead of the magnitude in the X direction. Here are the changes, with new text shown in red and deleted text shown with a line through it.

! Shows a running plot of the magnetic field for the last 10

! seconds in 0.1 second intervals.


! Initialize the display with the field set to 0 along all axis.

DIM mx(100, 2), my(100, 2), mz(100, 2)

FOR t = 1 TO 100

  mx(t, 1) = (t - 100)/10.0

  my(t, 1) = (t - 100)/10.0

  mz(t, 1) = (t - 100)/10.0


! Initialize the plot and show it.

DIM p as Plot, px as PlotPoint, py as PlotPoint, pz as PlotPoint

p = Graphics.newPlot

p.setTitle("Magnetic Field in Micro Teslas")

p.setXAxisLabel("Time in Seconds")

p.setYAxisLabel("Field: X: Green, Y: Red, Z: Blue Strength")


p.setGridColor(0.8, 0.8, 0.8)

px = p.newPlot(mx)

px.setColor(0, 1, 0)

px.setPointColor(0, 1, 0)

py = p.newPlot(my)

py.setColor(1, 0, 0)

py.setPointColor(1, 0, 0)

pz = p.newPlot(mz)

pz.setColor(0, 0, 1)

pz.setPointColor(0, 0, 1)

! Set the plot range and domain. This must be done

! after adding the first PlotPoint, since that also

! sets the range and domain.

p.setView(-10, -10, 0, 10, 0)


! Loop continuously, collecting magnetometer data

! and updating the plot.


t0 = -1

index = 1


  ! Get a new reading, them make sure it is a change since the

  ! last one.

  m = sensors.mag

  IF m(4) > t0 THEN

    ! Move all previous points one sample back.

    FOR i = 1 TO 99

      mx(i, 2) = mx(i + 1, 2)

      my(i, 2) = my(i + 1, 2)

      mz(i, 2) = mz(i + 1, 2)



    ! Place the new point at the right of the plot.

    mx(100, 2) = SQR(m(1)*m(1) + m(2)*m(2) + m(3)*m(3))

    my(100, 2) = m(2)

    mz(100, 2) = m(3)


    ! Update the plots.





    ! Adjust the function range based on the maximum observed value.

    max = 0

    FOR i = 1 TO 100

      IF ABS(mx(i, 2)) > max THEN max = ABS(mx(i, 2))

      IF ABS(my(i, 2)) > max THEN max = ABS(my(i, 2))

      IF ABS(mz(i, 2)) > max THEN max = ABS(mz(i, 2))


    range = 10^(INT(LOG(max)/LOG(10)) + 1)

    p.setView(-10, -range, 0, range, 0)


    ! Repaint the plots.



    ! Reset the time to the new time.

    t0 = m(4)



With this program in place, we can try our metal detector again. First, try spinning it in away from metallic objects, so only the Earth’s magnetic field has an effect. Here’s what I saw:

Creating a Copy of a Program in Tech BASIC

Press and hold on the source window, letting up after the cursor-magnifier shows up. This gives the option to select all of the text; do that. Another option appears to copy the text. Tap that option, too.

Back on the Programs view, tap the New button to get a new program, enter Metal Detector for the name, and press on the screen again, letting up after the cursor-magnifier shows up. This time, the only option is Paste. Do the Paste to create a copy of the old Magnetometer sample.

My expectation was that the magnetic field would be constant, since the magnitude of the Earth’s magnetic field is not changing as I rotate the iPhone. That’s not what the data shows, though. Why? Well, consider that the magnetometer is, in fact, a metal detector and an electric field detector that is sensing changes in the Earth’s magnetic field as metal and current warps the field. As we mentioned earlier, iPhone itself has a lot of metal, probably has a magnet in the speaker, and definitely has magnetic currents. The magnetometer is detecting the iPhone! Fortunately, we can largely eliminate this effect by holding the iPhone in a constant orientation as we pass it over the suspected metal. As long as you don’t twist the iPhone as it is moved, the distortion of Earth’s magnetic field from the interaction of the iPhone does not change.

Using the Metal Detector

Passing our metal detector over my keyboard gives the following. The spike occurred just as the iPhone passes over the edge of the keyboard. Speed is important, too. Move the iPhone too fast or too slow, and the size of the spike is reduced.

If you are having difficulty seeing the change, especially if the plot looks like a straight line, remember that you can increase the vertical scale by placing two fingers on the screen and spreading them out in a vertical direction.

So there you have it: A Your iPhone is a metal detector! You can experiment with various metallic objects to get an idea how sensitive it is and how fast to move the iPhone for best results.

For More Details:

AK8973 – 3-axis Electronic Compass,, Asahi Kasei Microsystems

Hall Effect,, Wikipedia

Earth’s magnetic field,'s_magnetic_field, Wikipedia

Magnetic declination,, Wikipedia

Magnetic deviation,, Wikipedia

Metal Detector,, Wikipedia

techBASIC,, Byte Works, Inc.

techBASIC in the App Store,

Download the metal detector program here.

Copyright 2011, Byte Works, Inc. All Rights Reserved.