# Portable Magnetometer

6,793

66

10

## Introduction: Portable Magnetometer

A magnetometer, sometimes also called Gaussmeter, measures the strength of the magnetic field. It is an essential tool to test the strength of permanent magnets and electromagnets and to understand the field shape of nontrivial magnet configurations. If it is sensitive enough it can also detect if iron objects got magnetized. Time-varying fields from motors and transformers can be detected if the probe is fast enough.

Mobile phones usually contain a 3-axis magnetometer but they have been optimized for the weak earth magnetic field of ~1 Gauss = 0.1 mT and saturate at fields of a few mT. The location of the sensor on the phone is not obvious, and it is not possible to place the sensor inside narrow apertures such as the bore of an electromagnet. Moreover, you might not want to bring your smartphone close to strong magnets.

Here I describe how to make a simple portable magnetometer with common components: a linear hall sensor, an Arduino, a display and a push-button. The total cost is less than 5EUR, and the sensitivity of ~0.01mT on a range of -100 to +100mT is better than what you might naively expect. To get accurate absolute readings, you’ll need to calibrate it: I describe how to do that with a home-made long solenoid.

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: The Hall Probe

The Hall-effect is a common way to measure magnetic fields. When electrons flow through a conductor in a magnetic field they get deflected sideways and thus create a potential difference on the sides of the conductor. With the right choice of semiconductor material and geometry, a measurable signal is produced that can be amplified and to provide a measure of one component of the magnetic field.

I use the SS49E because it’s cheap and widely available. A few things to note from its datasheet :

• Supply voltage: 2.7-6.5 V, so perfectly compatible with the 5V from the Arduino.
• Null-output: 2.25-2.75V, so approximately halfway between 0 and 5V.
• Sensitivity: 1.0-1.75mV/Gauss, so it will require calibration to get precise results.
• Output voltage 1.0V-4.0V (if operated at 5V): well covered by the Arduino ADC.
• Range: +-650G minimum, +-1000G typical.
• Response time 3mus, so it can sample at a few tens of kHz.
• Supply current: 6-10mA, low enough to be battery-operated.
• Temperature error: ~0.1% per degree C. Seems little but a 0.1% offset drift gives a 3mT error.

The sensor is compact, ~4x3x2mm, and measures the component of the magnetic field that is perpendicular to its front face. It will output a positive for fields that point from the back side to the front side, for example when the front is brought to a magnetic south pole. The sensor has 3 leads, +5V, 0V and output left to right, when seen from the front.

## Step 2: ​Required Material

• SS49E linear Hall sensor. These cost ~1EUR for a set of 10 online.
• Arduino Uno with prototype board for prototype or Arduino Nano (without headers!) for portable version
• SSD1306 0.96” monochrome OLED display with I2C interface
• A momentary push-button

To construct the probe:

• An old ballpen or other sturdy hollow tube
• 3 thin stranded wires somewhat longer than the tube
• 12cm of thin (1.5mm) shrink tube

To make it portable:

• A large tic-tac box (18x46x83mm) or similar
• A 9V-battery clip
• An on/off switch

## Step 3: ​First Version: Using an Arduino Prototype Board

Always prototype first to check that all the components work and that the software is functional! Follow the picture and to connect the Hall probe, the display and the null-button: The Hall probe needs to be connected to +5V, GND, A1 (left to right). The display needs to be connected to GND, +5V, A5, A4 (left to right). The button needs to make a connection from ground to A0 when pressed.

The code was written and uploaded using the Arduino IDE version 1.8.10. It requires to install the Adafruit_SSD1306 and Adafruit_GFX libraries Upload the code in the attached sketch.

The display should show a DC value and an AC value.

## Step 4: ​Some Comments About the Code

Feel free to skip this section if you’re not interested in the inner workings of the code.

The key feature of the code is that the magnetic field is measured 2000 times in a row. This takes about 0.2-0.3 seconds. By keeping track of the sum and of the squared sum of the measurements, it is possible to calculate both the mean and the standard deviation, which are reported as DC and AC. By averaging a large number of measurements, the precision increases, theoretically by sqrt(2000)~45. So with a 10-bit ADC, we can reach the precision of a 15-bit ADC! It makes a big difference: 1 ADC count is 5mV, which is ~0.3mT. Thanks to the averaging, we improve the precision of from 0.3mT to 0.01mT.

As a bonus, we also get the standard deviation, so fluctuating fields are identified as such. A field fluctuating at 50Hz does ~10 full cycles during the measurement time, so its AC value can be well measured.

After compiling the code I get the following feedback: Sketch uses 16852 bytes (54%) of program storage space. Maximum is 30720 bytes. Global variables use 352 bytes (17%) of dynamic memory, leaving 1696 bytes for local variables. Maximum is 2048 bytes.

Most of the space is taken up by the Adafruit libraries, but there is plenty of space for further functionality

## Step 5: ​Preparing the Probe

The probe is best mounted at the tip of a narrow tube: this way it can be easily placed and kept in position even inside narrow apertures. Any hollow tube of a nonmagnetic material will do. I used an old ballpen that gave a perfect fit.

Prepare 3 thin flexible wires that are longer than the tube. I used 3cm of ribbon cable. There is no logic in the colors (orange for +5V, red for 0V, grey for signal) but with just 3 wires I can remember.

To use the probe on the prototype, solder some pieces of stripped solid-core hookup wire o the end and protect them with shrink tube. Later this can be cut off so that the probe wires can be soldered directly to the Arduino.

## Step 6: Building a Portable Instrument

A 9V battery, the OLED screen and an Arduino Nano fit comfortably inside a (large) Tic-Tac box. It has the advantage of being transparent, to the screen is well readable even inside. All fixed components (the probe, the on/off switch and the push-button) are attached to the top, so that the whole assembly can be taken out of the box for changing battery or updating the code.

I was never a fan of 9V batteries: they are expensive and have little capacity. But my local supermarket suddenly sold the rechargeable NiMH version for 1 EUR each, and I found that they can be easily charged by keeping them on 11V through a 100Ohm resistor overnight. I ordered clips cheaply but they never arrived, so I took apart an old 9V battery to turn the top into a clip. The good thing about the 9V battery is that it’s compact and the Arduino runs well on it by connecting it to Vin. On +5V there will be a regulated 5V available for the OLED and for the Hall probe.

The Hall probe, the OLED screen and the push button are connected in the same way as for the prototype. The only addition is an on/off button between the 9V battery and the Arduino.

## Step 7: ​Calibration

The calibration constant in the code corresponds to the number given in the datasheet (1.4mV/Gauss) , but the datasheet allows for a large range (1.0-1.75mV/Gauss). To get accurate results, we’ll need to calibrate the probe!

The most straightforward way to produce a magnetic field of a well-determined strength is to use a solenoid: the field strength of a long solenoid is: B=mu0*n*I. The vacuum permeability is a constant of nature: mu0=1.2566x10^-6 T/m/A. The field is homogenous and depends only on the density of windings n, and the current I, both of which can be measured with good accuracy (~1%). The quoted formula is derived for infinitely long solenoid, but is a very good approximation for the field in the center as long as the ratio of length to diameter, L/D>10.

To make a suitable solenoid, take a hollow cylindrical tube with L/D > 10 and apply regular windings with enameled wire. I used a PVC tube with and outer diameter of 23mm and wound 566 windings, than spanned 20.2 cm, resulting in n=28/cm=2800/m. The wire length is 42m and the resistance 10.0 Ohm.

Supply power to the coil and measure the current flow with a multimeter. Use either a variable voltage supply or a variable load resistor to keep the current under control. Measure the magnetic field for a few current settings and compare it to the readings.

Before calibration, I measured 6.04 mT/A while the theory predicts 3.50 mT/A. So I multiplied tthe calibration constant in line 18 of the code by 0.58. The magnetometer is now calibrated!

Finalist in the
Magnets Challenge

2 614
91 17K
30 7.6K

## 10 Discussions

Very nice and interesting project. If I want to read Earth's magnetic fields caused by Sun flares or solar winds, is this project sensitive enough to read the KP readings? (Kp-index. The Kp-index describes the disturbance of the Earth's magnetic fieldcaused by the solar wind.)

I'm afraid not. This probe is for strong magnetic fields, in the 0.1-100mT range. The earth magnetic field is of order 0.1mT, so you'd want a probe with a _much_ higher sensitivity. I have no experience with those, but I'm pretty sure they exist!

Love it! I have been looking for something like this for ages. I'll post a photo when I have built it. Thanks again!

Great! Let me know if something is not clear!

Is it possible that this could be used as a PinPointer device for Metal Detecting?
Just curious as it has many windings and Hall Sensors.

IF NOT with this design, would a modified design do the job?

Haha, I had to lookup what a pinpointer is!! I found an instructable here: https://www.instructables.com/id/Pin-Pointer-Metal-Detector-Arduino/.

Bad news: this magnetometer can't be used as a pinpointer: it is meant to measure existing magnetic fields, while a metal detector creates magnetic fields (at high frequencies) and looks for the reaction of metal objects to that field.

Metal detectors usually use coils to detect magnetic fields. At high frequencies, that's much more sensitive than Hall sensors, which instead are better at low frequencies, and can measure static fields, which is not possible with a coil.

Well, that's why I asked. I didn't know if it would pick up signals like the real pin pointers do.
Thanks for the link to pin pointers though I'm going there noe an dcheck it out.

The author, https://www.instructables.com/member/TechKiwiGadgets/instructables/
has three instructables of metal detectors, I think they all work on the same principle. Beware I once ordered "LM339 Quad Voltage Comparator" from Aliexpress and received fakes!

I also once made an instructable of a metal detector https://www.instructables.com/id/Simple-Arduino-Metal-Detector/ that is incredibly easy to make (just an Arduino, a coil, a resistor, a capacitor and a diode), and maybe could be turned into a pinpointer by making a small coil.

Very good design! I was wondering if there is a way to program in
arduino ide a design that waits for certain frequencies to be registered
that trigger words to be displayed on a very tiny seven segment lcd.
For example the mic picks up 13.6 hz. How can I develop a program that
assigns certain words when certain frequencies are read? thanks!

hi, thanks. You mention a 'mic'. Do you mean microphone, to pick up audio? That's a different subject! Anyway, to detect a certain frequency from an analog signal, the Fourier transform is the standard tool. I've never coded that but I'm pretty sure there are example codes and maybe even libraries around for the FFT (Fast Fourier Transform). Good luck!