Phone Magnetometer

Measuring Disturbances in the Earth's Magnetic Field with a Smartphone

If you have a smartphone, it probably has a “compass” sensor built in which you may have used to help you find your way around using a mapping app. Like a traditional compass, the sensor detects the direction of the Earth's magnetic field and uses it to figure out which way you are holding your phone. I was interested to know more about what these sensors could do and in particular, how sensitive and accurate they are. For example, could they be used to detect natural disturbances in the Earth's field due to solar activity, so-called “space weather” events?

An App for that...

To find out, I wrote a simple Android app which reads data from the phone's magnetic sensor, known as a magnetometer. The app reads data as fast as the sensor can provide it (about 25 times per second with my phone) and averages these values into one second means which are recorded in a log file on the phone's SD card or equivalent storage space. The log file can be read on a PC by accessing the phone's disk through a USB connection.

The app doesn't run in the background, so you can't use your phone for anything else whilst it's running. This isn't as useless as it sounds because the phone needs to be left in the same place whilst the data are being recorded – if you move the phone, the direction of the magnetic field it sees will change. This might still sound a bit useless – after all, you normally want to carry your phone with you – but I'll come back to this topic later.

I ran the app on a “spare” Google/HTC Nexus One phone. I soon found that the magnetic field measurements from the magnetometer were very noisy. Even after averaging to one second intervals, the values fluctuated by around 200 nT from one sample to the next. A “nT” is a “nanotesla” and is the unit used for measuring the Earth's magnetic field strength. The Earth's field varies from about 30000 nT at the equator to 60000 nT at the poles, so this might seem a small fluctuation. Unfortunately, at mid-latitudes such as the U.K. where I live, a real change of 200 nT in the Earth's field would be considered quite a large disturbance. Fortunately, natural disturbances tend to have time scales of many minutes or hours, not seconds so it is possible to average the data even more to reduce the random fluctuations.

Land of the Midnight Sun

To see if it was possible to make out a real magnetic disturbance in the noisy data, I needed to leave the app running and hope for something to happen. Sadly, large magnetic disturbances don't come along every day in the U.K. Luckily, I had to make a work trip to Tromsø in the north of Norway. Tromsø lies in the “auroral zone”, a ring-like region around the Earth's north magnetic pole where the aurora borealis or “northern lights” are most frequently observed (there is an equivalent region in the southern hemisphere). Auroral activity is associated with large magnetic disturbances. I was going in July, so I would not see the aurora since it would be midnight sun conditions, but hopefully my phone app would record the magnetic disturbances!Shortly after arriving at the place I was staying, I set up my phone in a spare room where it would be undisturbed during my stay. The photograph shows the phone lying on a cloth on a desk and connected to mains power through its charger. The app would have to run continuously so the battery would not last very long. I used the cloth to stop the phone from slipping about on the hard table top.

The magnetometer in the phone measures the magnetic field in three directions at right-angles to each other. If you hold the phone in front of you in a normal “portrait” orientation, these directions are to the right across the screen (“X”), up across the screen (“Y”) and towards you, out of the screen (“Z”). I positioned the phone so that, approximately, “X” pointed to the East, “Y” to the north and “Z” vertically upwards. I then left it where it was for the rest of my stay.

Not far away, a much more accurate magnetometer was also monitoring the Earth's magnetic field. This instrument is run by the Tromsø Geophysical Observatory (TGO) and is part of a network of such instruments used for scientific research. After returning home, I contacted TGO and they kindly provided me with data from their magnetometer to compare with my phone app's recording.

The Proof of the Pudding

The figure to the right shows plots of how the X, Y and Z measurements of the Earth's magnetic field compared between the TGO (blue) and phone (red, labelled "MagTest") magnetometers. The lines mostly do not overlap because my positioning of the phone was not very accurate and the X, Y and Z directions are not quite the same for the two magnetometers. But the most dramatic difference is that whilst the TGO data are nearly straight lines with occasional wiggles marking disturbances, the phone data show large waves in the X and Y directions and random jumps in the Z direction. In this figure, the data from both TGO and the phone have been averaged to 5 minute time steps.The large waves in the X and Y directions have a period of about 1 day. I think they are most likely to be due to the temperature in the room (and of the phone) varying and affecting the sensor's performance. Nevertheless, in the Y direction, it is possible to see that there are short term wiggles in the phone trace which coincide with the wiggles in the TGO trace. In order to remove the effect of the temperature variations and compare the data from the two instruments better, I smoothed the measurements by averaging them over 3 hours and subtracted this smoothed data from the original data. The result then only shows variations with periods shorter than 3 hours (the filtering is not perfect, so some longer variations will remain). I treated the TGO data in the same way so that a direct comparison could be made with the phone data.

The result is shown on the left where I have also zoomed in to the interval where the largest disturbances occurred. It's now clear that the during the disturbances measured by the TGO magnetometer, the phone magnetometer shows some good agreement, especially in the Y direction. However, the phone data show quite large variations which are not present in the TGO data.So, the experiment showed that it is indeed possible to see natural magnetic disturbances in the data recorded from a smartphone magnetometer. On the other hand, the disturbances need to be quite large to be unambiguously distinguished from the random fluctuations in the measurements. Using a smartphone to detect space weather events looks likely to be of limited value.

Magnetometers for the Masses?

Having said that, it's worth bearing in mind that smartphones are very common now. Two identical smartphones recording in the same place are unlikely to produce the same random fluctuations in their measurements, but they should agree on the real disturbances. By looking for agreement (“correlation”) between measurements, it might be possible to weed out the real disturbances from the rubbish. This means that crowdsourcing measurements of the Earth's magnetic field isn't out of the question.

As I alluded to earlier, a problem in using a smartphone in this way is that it needs to be left alone in one place while it's measuring. But most of us do leave our phones alone at night whilst we're asleep and they are re-charging! There is a tendency for large magnetic disturbances to occur at night too. So at precisely the time when we might most want to be measuring, the phones are well placed to do so and being on charge means that having an app running all the time isn't a problem for battery life. Would it be necessary for everyone to align their phones carefully to make useful measurements? Probably not: with the assistance of the phone's accelerometer sensor, which senses the pull of gravity and thus which way is "down", it's possible to convert the phone's X, Y and Z directions to something more meaningful namely magnetic north, magnetic east and "up"; in fact this is exactly how the phone's “compass” works. To take account of the disturbances we're trying to measure, the magnetic north and east directions would have to be averaged over some longish time interval whilst the phone remained still.


I've shown that a smartphone's magnetometer can indeed detect natural disturbances in the Earth's magnetic field, but that the random fluctuations in the measurements are rather large compared to those disturbances – the natural events don't exactly “jump out” of the data. Collecting measurements from several phones might make it possible to determine which are the real disturbances.


The source code for the app is available to download below, released under the Apache License, Version 2.0. Note that this app isn't meant to be a finished product, just a tool to record the magnetometer measurements for the purpose of this study.

Copyright 2013, 2014 Andrew Senior.