Home / Tutorials / Arduino Tutorial / Arduino Magnetic Sensor Using Hall Effect
hall effect sensor

Arduino Magnetic Sensor Using Hall Effect

An Arduino magnetic sensor has several uses: for reading the speed of a rotating object, for door alarms and for navigation. In this tutorial, we will build a simple magnet detector using a hall effect sensor.

Video Tutorial

What is Hall Effect?

Hall effect describes a phenomenon where a voltage builds across a current-carrying conductor due to a presence of a magnetic field.

hall effect theory illustration

In the illustration shown, an upwards magnetic field is applied to a conductor with flowing electrons (for simplicity, other types of charges may exist). This field introduces a force to the electrons that tends to pull them to the left. This makes the left part of the conductor more negative compared to the right, thus voltage is produced.

Hall Effect Sensor

There are manufactured integrated circuits that follow the hall effect principle. These sensors, which looks like transistors, produce an output voltage when a magnet is near them.

ss49E hall effect sensor

Its three pins are VCC, OUT and GND. The nature of what comes out from the OUT pin depends on the type of hall effect sensor: analog or digital.

Analog-type hall effect sensors produce a voltage which varies linearly with the strength of a magnetic field. Meanwhile, digital hall effect sensors contain a Schmitt trigger which produces a logic high voltage in the presence of a magnetic field and zero when there is none. Hall effect “switches” are typically digital types.

Correct polarity is always followed; the front face of the sensor detects the south pole of magnet. Conversely, the back face of the sensor detects the north pole of the magnet.

Hall effect sensors also have different configurations in how they detect magnets. Head-on magnetic sensors detect when the south pole of the magnet moves towards the front face of the sensor. Alternatively, sideways magnetic sensors detect when the south pole of the magnet moves across the front face of the sensor.

Sensor Module for Arduino Magnetic Sensor

Modules make interfacing sensors to microcontrollers easier. For this tutorial, I am using this hall effect sensor module:

arduino magnetic sensor

This sensor features the SS49E linear hall effect sensor with a LM393 op-amp IC used as a comparator for digital output. The module also includes a potentiometer for tuning the sensitivity of the digital output.

As seen, there are four output pins where G is ground, + is for a 5V supply, D0 is digital output and A0 is analog output. A red LED turns on when the module is powered. A green LED lights up when the D0 pin goes high. Moreover, the A0 pin connects directly to the OUT pin of the SS49E sensor.

Since the SS49E is a linear type, we can detect not only the presence of the magnet but also its strength. Its datasheet shows this:

SS49E hall effect sensor voltage vs field intensity

The graph shows that the sensor produces a voltage of 1 to 4 V corresponding to a magnetic field intensity of -100 to 100 milliTesla.

Testing the Sensor

As mentioned, the hall effect module provides both analog and digital outputs. If a magnet is detected, the digital output pin goes high and the green LED on the board lights up.

But before that, I wanted to test if the hall effect sensor really can measure magnetic field strength. Thus, I wired the A0 pin of the module to the A0 pin of the arduino:

arduino to hall effect sensor

Then I uploaded the built-in AnalogReadSerial sketch to the Arduino:

Here’s what the serial monitor shows when there is no magnet present:

arduino magnetic sensor serial output
This value corresponds to a voltage of around 2.6 volts. Now if we look at the graph again:

A voltage of 2.5-2.6 means around zero Teslas! With this, I created this formula:

What’s interesting, however, is the value on the serial monitor decreases when a magnet is introduced and the green LED turns on:

Then, I decided to use the other pole of the magnet and the value goes up:

Interestingly, the LED did not turn on this time. This means the LED turns on only when a magnet in one polarity is detected. Similarly, the module gives increasing or decreasing voltage depending on the polarity of the magnet.

Building a Usable Arduino Magnetic Sensor

Now that I know that the hall effect module works, it’s time to build my actual project. I added an LCD and a speaker to the previous Fritzing diagram:

arduino magnetic sensor wiring diagramWith this circuit, the magnetic field intensity is displayed on the screen and the speaker will beep when a magnetic is detected. I chose to display both microTeslas and milliTeslas for the device to detect multiple type of magnets.

Here’s now the full sketch:

You might need the LiquidCrystal I2C library for this sketch to run.

Here, the D0 pin of the module connects to digital pin 2 of the Arduino. Recall that an interrupt can be attached to this pin. So when the D0 pin goes high, the Arduino can react immediately. The interrupt service routine (named magnetDetected here) is just for turning on the speaker.

As before, the A0 pin of the module connects to the A0 pin of the Arduino. For a 10-bit ADC, analogRead returns a value as:

I reversed this formula in the sketch to get the actual voltage from the module (A0volts). This value is then used as Voutput in the formula I first presented above:

In the video, you will see the how different magnet types give different tesla values.

I hope you find this article on Arduino magnetic sensor useful. Feel free to drop comments, suggestions and reactions in the comments section below!

 

Check Also

arduino digital input

How pinMode, digitalWrite and digitalRead Work

I believe what makes Arduino popular is how it makes microcontrollers easier to use. I …

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.