Hall Sensor Tutorial for Arduino and ESP8266

Hall Sensor Tutorial for Arduino and ESP8266

In this tutorial we cover the Hall Sensor, a sensor to measure magnetic fields.

First we describe what the Hall Effect is and how a hall effect can be measured from a sensor.

After we know the fundamentals, we cover two different sensors:

  1. Linear Hall Sensor
  2. Magnetic Hall Sensor
Linear Hall Sensor (KY-024)

What is the Hall Effect?

If electric current flows through a conductor, a magnetic field is employed perpendicular to the current direction. This magnetic field exerts a diagonal force on the moving charge carriers which tends to push them to one side of the conductor. Because there is a potential difference due to the pushed outwards charge carries a measurable voltage between the two sides of the conductor is produced, the so called Hall voltage. This effect is called the Hall Effect after E. H. Hall who discovered it in 1879. The effect is depending on the thickness of the conductor. Therefore the conductor should be very thin.

From the picture we see how a hall sensor is working. We need a power supply (5V and GND) and also a second circuit to measure the Hall voltage (input of the microcontroller).

Typical applications of Hall sensors are:

  • Brushless DC electric motors to detect the position of the permanent magnet.
  • Computer printers to detect missing paper and open covers.
  • General switch applications because of the following advantages:
    • Lower costs of production compared to a mechanical switch.
    • Higher reliability compared to a mechanical switch.
    • Higher operational frequencies than a mechanical switch.
    • No debouncing effect (see button and switch article)
    • Hall sensor as a switch can be completely build in a case and is therefore protected against dirt and water.

One disadvantage of a Hall Effect sensor is the much lower measuring accuracy compared to fluxgate magnetometers or magnetoresistance-based sensor. But these sensors are quite more expensive for our DIY projects.

I use the following parts for this tutorial

  • A microcontroller of your choice (Arduino, ESP8266, ESP32)
  • Linear Hall Sensor (KY-024) and Magnetic Hall Sensor (KY-003) that are part of the ELEGOO Sensor Pack

You find all links to the parts on the Components and Parts page

Linear Hall Sensor (KY-024)

The KY-024 Linear magnetic Hall sensor reacts in the presence of a magnetic field. It has a build in potentiometer on the module to adjust the sensitivity of the sensor and it provides both analog and digital outputs.

  • The digital output acts as a switch that will turn on/off when a magnet is near, similar to the KY-003.
  • The analog output can measure the polarity and relative strength of the magnetic field.

Linear Hall Sensor Analog Output

For the analog input we have to connect the signal pin from the Hall Effect sensor to the analog pin from the microcontroller. The microcontroller read the analog values. The maximum ADC reference voltage is defined to 5V = 1023 which gives us a good resolution because the maximum operation voltage of the sensor is 6.5V.

In the following example we measure the polarity and the strength of the magnet and display the results on the serial plotter of the Arduino IDE. If you do not find the serial plotter, here is a tutorial for the complete Arduino IDE where I show you how to display results with the serial plotter.

int analogPin = A0; // linear Hall analog interface
int analogVal;      // analog readings

void setup ()
{
  pinMode(analogPin, INPUT); 
  Serial.begin(9600);
}

void loop ()
{
  // Read the analog interface
  analogVal = analogRead(analogPin);
  Serial.println(analogVal); // print analog value
  delay(100);
}
Linear Hall Sensor Analog Output
Linear Hall Sensor - Analog Output

Linear Hall Sensor Digital Output

For the digital input connection we connect the signal pin with a digital input pin of the microcontroller. Because the signal is digital and only gives us a 0 or 1, we have to make sure that the sensitivity of the sensor is well calibrated with the potentiometer.

In this example we also measure the strength of the magnet, but we do not see the polarity. Moreover we do not know the relative strength of the magnet. We only know that our threshold, calibrated with the potentiomenter) was exceeded if we get the 1 as result of the measurement.

int digitalPin = 8; // linear Hall digital interface
int digitalVal;     // digital readings

void setup ()
{
  pinMode(digitalPin, INPUT); 
  Serial.begin(9600);
}

void loop ()
{
  // Read the digital interface
  digitalVal = digitalRead(digitalPin);
  Serial.println(digitalVal); // print digital value
  delay(100);
}
Linear Hall Sensor Digital Output
Linear Hall Sensor Digital Arduino

Magnetic Hall Sensor (KY-003)

The KY-003 magnetic Hall sensor is a digital sensor and is therefore only able to operate as switch. Maybe you know a reed switch? I also wrote an entire article about this switch, you can find here. The reed switch is also a magnetic switch. But how do you know if you better use a magnetic hall sensor as switch or a reed switch? In the following table I compared the two different magnetic switches with the advantages and disadvantages.

It is not possible to measure how strong the magnetic field is with the magnetic hall sensor. The operation voltage of the KY-003 is between 3.3V and 5V. This module is able to operate under high temperature conditions due to the maximal operating temperature of 85°C or 358 Kelvin.

In the next video you see that I can not turn off or on the sensor with the same polarity. Therefore I have to change the polarity to trigger the magnetic sensor. To visualize if the sensor or the switch is turned on or off, I used a LED.

int led = 9 ;       // LED
int hallsensor = 8; // magnetic Hall digital interface
int digitalVal;     // digital readings

void setup ()
{
  pinMode(led, OUTPUT); 
  pinMode(hallsensor, INPUT); 
  Serial.begin(9600);
}

void loop ()
{
  // Read the digital interface
  digitalVal = digitalRead(hallsensor);
  // when the Hall sensor detects a magnetic field, turn LED on
  if (digitalVal == LOW) 
  {
    digitalWrite (led, HIGH);
  }
  else
  {
    digitalWrite (led, LOW);
  }
  delay(100);
}
Magnetic Hall Sensor

Conclusion

Hall sensors are a nice way to realize projects, where the sensor itself is hidden. An example could be that you want to update a system or restart a system, where the whole system is build in a wall. In this case you could trigger the action with a simple magnet touching the wall.
Did you liked the article? If you have any questions, please use the comment section below to ask.

4 Responses

Leave A Comment