A Simple Arduino Magnetometer

Magnetometers are very useful devices if you want to measure the strength of magnetic fields. For instance, in geophysics, you can think of the Earth as a giant magnet with north and south poles, plus a magnetic field all around it. The strength of that magnetic field will vary with location and orientation, and to some extent will be warped by large changes in the materials forming our planet and its surface. However, as people have known for centuries, magnetic compasses can be strongly affected in areas with high iron contents, even causing inaccurate bearings by a few degrees. Therefore, we could use a cheap electronic compass module as a magnetometer, but with only small changes in heading due to nearby iron, detecting interesting anomolies can be difficult. So, here we use a Honeywell HMC6352 compass module which allows us to read the values of the two built in magnetometer channels that it uses to determine compass headings. This has the advantage of allowing us to get more data on the magnetgic fields at a higher resolution.

For the more adventurous people who might want to use a small magnetometer to measure, for instance, the iron content of a material, using this Honeywell device also allows for simple measurements of magnetic field on a single axis. Of course, we don't expect it to be highly accurate, as the HMC6352 is intended to be a compass module, but it does allow us to inexpensively learn more about magnetic fields and even do some simple geophysical surveying. However, it also has a number of other potentially fun uses in detecting nearby metal, such as cars moving into your driveway, or as a simple treasure detector. Whichever way you wish to use it, the circuit is shown in Figure 1 and is very simple. The compass module uses a form of serial communications known as i2c, which only requires two data lines (SCL, a data clock line, and SDA, a data transfer line) and a power supply to operate properly. The i2c commands are well covered in the HMC6352 datasheet, but are even easier thanks to the forums at diydrones where example code is available to read the compass heading and the two magnetometer channels.

The compass is shown connected to the 3.3V supply even though the HMC6352 has a wide operating voltage, and this is to prevent confusion if a different module is used that won't work with higher voltages (many sensors these days require a maximum of 3.3V, so please check the voltage requirements of any different compass module you may use). The purpose of the LED is to give a visual indication of when measuremens are being made. If you want to communicate with the Arduino using Bluetooth then it should be connected into the Arduino Tx and Rx pins as shown, but you can ignore that if you're getting data using a USB cable into the Arduino serial monitor. The circuit should be quite simple to construct on a breadboard, expansion shield or even, for long-term use, a piece of veroboard.

The circuit diagram.

Figure 1. The circuit diagram.

Once you have the circuit set up and connected to your computer, you can test it using the serial monitor in the Arduino development software (note the Arduino code is set to use 9600bps, whereas you may want to set a higher serial communication speed to suit your connection method). To output a comma separated line (heading, magnetometer 1, magnetometer 2) simply send '4' as a command, or for 200 lines to allow a sweep of approximately six seconds simply use the command '5'. As the data can be saved to a csv file (I used notepad for that), you can easily import it into Microsoft Excel. As an example, Figure 2 shows the two magnetometer channels when the compass module is kept at the same orientation and moved closer to, then further away from, a metal tin.

If you're using a Bluetooth module for communications, it's also easy to get the data on a Pocket PC using the program shown in Figure 3, which is programmed in NS Basic. In that Pocket PC example, the compass module was pointed vertically downwards and moved slowly over a small metal cylinder. In both of these examples it's relatively simple to work out where the compass module was close to the metal. The Pocket PC software displays the difference between each reading and the average for each channel, as that makes it easier to compare the magnetometer channels and change the scale of the vertical graph axis. However, the program adds the average back on when saving data, so you can still copy files to a desktop PC to import into a spreadsheet. Obviously both these examples keep the compass orientation constant to magnify any anomolies in the data, but the Arduino software also outputs the compass heading which you may be able to use to correct magnetic field values where you can't maintain a constant orientation.

A graph created in Microsoft Excel.

Figure 2. An Excel graph.

The NS Basic Pocket PC software.

Figure 3. Pocket PC software.

The Arduino code can be downloaded by clicking here, and you will find that it contains much more code than is necessary just for this project, as it is shared with code for accelerometers and other sensors that will be integrated together in a future project. If you wish to just use the compass module features it should be relatively straight forward to edit the code for your Arduino, although there is no need to do that if you just want to build this project. The NS Basic Pocket PC software can be downloaded by clicking here. As the Pocket PC software is GPL open source, but the NS Basic file can only be read if you have a paid for copy of their development environment, you can access a text copy by clicking here.