Hall Sensor Tutorial for Arduino, ESP8266 and ESP32

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. KY-024 Linear Hall Sensor
  2. KY-003 Magnetic Hall Sensor
Linear Hall Sensor (KY-024)

Table of Contents

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.

Magnetic Hall Sensor vs Reed Switch

Besides the magnetic hall sensor, there is also the reed switch, which serves as a magnetic switch. Depending on your project and the requirements, you prefer to use one of both magnetic switches.

The main difference between the magnetic hall sensor and the reed switch is the direction of the magnetic field. For a magnetic hall sensor, the magnet has to be perpendicular to magnetic hall sensor, but for the reed switch in parallel.

In the following table you find the comparison between the two magnetic switches with the advantages and disadvantages.

 Magnetic Hall Sensor (KY-024, KY-003)Reed Switch (KY-025, KY-021)
Switch Function• Transducer that varies the output voltage depending on the presents of a magnetic field.• Pair of ferrous metal contacts. If contracts are open, there is no electrical contact. The contacts are closed by a magnet near the switch and opened by removing the magnet.
Magnet – Sensor Orientation• Magnet has to be perpendicular to magnetic hall sensor• Magnet has to be parallel to reed switch
Advantages• No moving parts involved
• No debouncing effect
• Cheaper than magnetic hall sensor
Disadvantages• More expensive than reed switch• Has moving parts that are not able to operate over frequencies greater 10 kHz
• Switch has debouncing effect like all switches

IF you want to learn more about the reed switch, I wrote a complete tutorial for this as well.

Comparison between KY-024 and KY-003 Hall Sensor Module

If you want to use a hall effect sensor, you have to choose from different ones. In this article we cover the KY-024 and the KY-003 hall sensor modules. The following table shows the differences between each sensor module so that we are able to compare them.

 KY-024KY-003
Hall-Effect SensorSS49E3144EUA-S
Operating voltage2.7V...6.5V4.5V...24V
Board dimensions1.5 cm x 3.6 cm
0.6 in x 1.4 in
18.5 mm x 15 mm
0.728 in x 0.591 in
OutputsAnalog + DigitalDigital
Build in resistor100kΩ potentiometer-
RepositionNo/weak magnetic fieldOpposite polarity of magnetic field

Both hall effect sensor modules are quite different because they use different hall-effect sensors.

The KY-024 module is based on the SS49E hall-effect sensor that allows an operation voltage between 2.7V and 6.5V. This voltage range is perfect for our Arduino microcontroller that have an operation voltage of 5V as well as the ESP8266 and ESP32 microcontroller, with an operation voltage of 3.3V. The KY-024 module is larger in size compared to the KY-003 module because it measures the analog signal and creates a digital signal as well. Therefore the module needs more components like the LM393 dual comparator and a potentiometer with 100kΩ.

The KY-003 hall sensor module is based on the 3144EUA-S hall-effect sensor and has a higher operation voltage between 4.5V and 24V. Therefore the KY-003 module can be connected to an Arduino, but not to an ESP8266 or ESP32 microcontroller board because the operation voltage of 3.3V is too low. The KY-003 is smaller in size because there is only a digital output signal from the sensor module.

One advantage of the KY-003 hall sensor module is that this module is able to operate under high temperature conditions due to the maximal operating temperature of 85°C or 358 Kelvin.

The biggest difference between the KY-024 and KY-003 hall sensor module is the reposition of the hall-effect sensor. The KY-024 switches back to its initial state, when there is no magnetic field present or the magnetic field is weaker. But the 3144EUA-S hall-effect sensor in the KY-003 module holds its state even if there is no longer a magnetic field. The reposition of the KY-003 module is done with the opposite polarity of the magnetic field. This behavior is also shown in the different examples of this tutorial.

Pinout of the KY-024 and KY-003 Hall Sensor Module

The following picture shows the pinout of the KY-024 and KY-003 hall sensor module. You see also the electronic components on the PCB modules. In the next chapter we will concentrate on the schematic with the electronic components, but in this chapter we only discuss the different pinouts.

KY-024 and KY-003 Hall Sensor Pinout

The comparison between the KY-024 and KY-003 modules showed that the output between both hall sensor modules differ. From the picture above, we see that this difference leads to a different pinout. The KY-024 has, in addition to the two pins for the power supply, one pin for the digital and one pin for the analog output.

The KY-003 on the other hand has only the pins for the power supply and one output pin for the digital output.

Schematic and Functionality of the KY-024 Hall Sensor Module

The KY-024 Linear magnetic Hall sensor reacts in the presence of a magnetic field. To understand how and why the sensor module reacts, we have to take a look at the schematic and the functionality of the KY-024 hall sensor module.

Schematic of the KY-024 Hall Sensor Module

The following picture shows the schematic of the KY-024 hall sensor module with all electronic components that are on the circuit board.

KY-024 Hall Sensor Schematic

On the left side we see a part of the circuit with LED L1 and resistor R1 (1kΩ) in series. If the KY-024 module has a valid power supply, LED L1 turns on and resistor R1 protects the LED L1 for too high voltages.

The second strain in the circuit is a series connection between the potentiometer (100kΩ), resistor R4 and the hall-effect sensor SS49E that needs a connection to the power supply. This series connection builds a voltage divider that is connected to the reference voltage connection (2) of the LM393 comparator. The analog output of the KY-024 hall sensor module is also measured on this reference voltage connection (2).

The input voltage of both comparators (3) (5) is the output voltage of the voltage divider that is created by connecting resistor R5 (50Ω) and resistor R3 (50kΩ) in series.

The output of the first comparator (1) is in the same time the digital output of the KY-024 module and also the reference voltage of the second comparator (6). But the output of the first comparator (1) is also connected to the supply rail with resistor R2 (10kΩ), because the LM393 has an open-collector output and therefore must be connected to the supply voltage via a pull-up resistor.

The output of the second comparator (4) is connected via LED L2 and resistor R6 (1kΩ) to the supply voltage.

Functionality of the KY-024 Hall Sensor Module

If there is a valid power supply for the KY-024, between 2.7V and 6.5V, the LED 1 turns on and indicates that the hall sensor module is operating.

The voltage divider between R5 and R3 creates a stable voltage of V = VCC * R3 / (R3+R5) = VCC * 50/100 = 0.5*VCC. that is the input voltage of the first (3) and second (5) comparator.

To understand the functionality of the KY-024 hall sensor module we have to consider the direction of the magnetic field through the hall-effect sensor. Our objective is to turn on LED2 when the magnetic field goes from the bottom to the top of the KY-024 hall sensor module.

The following video shows a magnet with a north pole and a south pole lying flat on a sheet of paper. The painted magnetic field goes outside the magnet from the north to the south pole. In the video you can see that when the magnetic field goes from top to bottom through the PCB, LED2 does not light up. However, when the magnetic field goes through the PCB from bottom to top, LED2 lights up.

To turn on LED2, when the magnetic field goes from the bottom to the top of the KY-024 hall sensor module, we have to make sure that LED2 is off, when there is no magnetic field present. If your LED2 it turned on, but in your project, the magnetic field should not turn LED2 on, turn the screw of the potentiomter counterclockwise to decrease the sensitivity. Now your KY-024 hall sensor module is setup and we can dive into the functionality with the help of the schematic.

If the magnetic field goes from the bottom to the top of the sensor module, the SS49E hall-effect sensor decreases its output voltage, regarding to the SS49E datasheet. Therefore the output of the voltage divider between the potentiometer and resistance R4 that is also the analog output of the KY-024 hall sensor module and the reference voltage of the first comparator (2) decreases.

The input voltage of the LM393-1 comparator (3) is on a constant voltage of 0.5*VCC due to the voltage divider of a resistance in the same size. When the reference voltage (2) drops under the input voltage of 0.5*VCC, the output of the first comparator (1) is equal to the supply voltage VCC, resulting in a digital HIGH output.

This digital signal is now inverted in the second comparator, because the reference voltage (6) of VCC is greater than the input voltage (5) of 0.5*VCC. Therefore the output of the second comparator (4) is 0V. Because there is a potential difference between the supply rail and the output of the second comparator, LED2 turns on.

The following table gives you an overview of all components and parts that I used for this tutorial. I get commissions for purchases made through links in this table.

ComponentAmazon LinkAliExpress Link
Arduino Nano AmazonAliExpress
Arduino Pro Mini AmazonAliExpress
Arduino Uno AmazonAliExpress
Arduino Mega AmazonAliExpress
ESP32 ESP-WROOM-32AmazonAliExpress
ESP8266 NodeMCU AmazonAliExpress
ESP8266 WeMos D1 Mini AmazonAliExpress
Linear Hall (KY-024) and Magnetic Hall (KY-003) Sensor in Sensor Kit AmazonAliExpress

Read the Analog and Digital Values of the KY-024 Hall Sensor

In the following example we want to read the analog and digital value of the KY-024 hall sensor module. With the analog connection we are able to get an indication of the strength and the direction of the magnetic field.

The KY-024 has a build in potentiometer to set the threshold for the digital value to change between 0 and 1 based on the analog value. The digital value is

  • 0 for the threshold is not exceeded (build in LED is off), because the magnetic field is too weak of the direction of the magnetic field is wrong.
  • 1 for the threshold is exceeded (build in LED is on)

Wiring between KY-024 Hall Sensor Module and Microcontroller

The following pictures show the wiring between the KY-024 linear hall sensor and the Arduino, ESP8266 or ESP32 microcontroller. For the power supply you can either use the 5V output like I did for the Arduino boards or use the 3.3V output pins, because the EPS8266 and ESP32 only have an operating voltage of 3.3V.

Linear Hall Sensor KY-024 Arduino Nano
Linear Hall Sensor KY-024 Arduino Nano

For more information about the Arduino Nano, visit the Arduino Nano Tutorial.

Linear Hall Sensor KY-024 Arduino Pro Mini
Linear Hall Sensor KY-024 Arduino Pro Mini
Linear Hall Sensor KY-024 Arduino Uno
Linear Hall Sensor KY-024 Arduino Uno

For more information about the Arduino Uno, visit the Arduino Uno Tutorial.

Linear Hall Sensor KY-024 Arduino Mega
Linear Hall Sensor KY-024 Arduino Mega

For more information about the Arduino Mega, visit the Arduino Mega Tutorial.

Linear Hall Sensor KY-024 ESP32 NodeMCU
Linear Hall Sensor KY-024 ESP32 NodeMCU
Linear Hall Sensor KY-024 ESP8266 NodeMCU
Linear Hall Sensor KY-024 ESP8266 NodeMCU
Linear Hall Sensor KY-024 ESP8266 WeMos D1 Mini
Linear Hall Sensor KY-024 ESP8266 WeMos D1 Mini

Code to read Analog and Digital Values of the KY-024 Module

After the wiring we can create two Arduino script that reads the analog and digital sensor value of the hall sensor and print the values to the serial monitor of the Arduino IDE.

You can also change the script to pint only the analog values of the KY-024 hall sensor module to show the strength and polarity of the magnetic field in the serial plotter of the Arduino IDE.

int analogPin = A0; // for Arduino microcontroller
//int analogPin = A0; // for ESP8266 microcontroller
//int analogPin = A4; // for ESP32 microcontroller

int digitalPin = 7; // for Arduino microcontroller
//int digitalPin = D7; // for ESP8266 microcontroller
//int digitalPin = 0; // for ESP32 microcontroller

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

void loop() {
  int analogVal = analogRead(analogPin);
  int digitalVal = digitalRead(digitalPin);
  Serial.print(analogVal);
  Serial.print("\t");
  Serial.println(digitalVal);
  delay(100);
}

Because this script can be used for Arduino, EPS8266 and ESP32 microcontroller boards, you only need one of the first three lines for the definition of the analog pin. The same applies to the definition of the digital pin.  You can delete or comment the two lines that you do not need.

After we defined the pin that is the analog and digital input for the microcontroller, we have to define the digital pin as input pin in the setup function. Also we set the baud rate of the serial communication to 9600, that has to be the same in the Arduino IDE to plot the sensor values.

In the loop function, we read the analog values with the analogRead function and the digital sensor value of the KY-024 hall sensor module with the digitalRead function and save both sensor values as a variable. Then we print the analog and digital value to the serial monitor, separated by a tabulator, and use a short delay of 0.1 seconds before we start the loop function again to read the next analog and digital sensor value.

The following video shows the strength of the magnetic field and also the polarity with the spikes up and down.

Results reading Analog and Digital KY-024 Module Values

The following videos and picture show the results measuring the analog and digital values of the KY-024 hall sensor module.

In the first video you see the serial plotter of the Arduino IDE that shows only the analog sensor value.

If there is no magnetic field present, the analog value is stable around 520. If there is a magnetic field goes from the bottom to the top of the sensor module, the analog value decreases. But if we switch the KY-024 module, the analog value increases above the value of 520.

The second video with the screenshot of the serial monitor shows the influence of the magnetic field on the digital sensor value.

Linear Hall Sensor Digital Arduino

When there is no magnetic field present, the digital output of the KY-024 sensor module is 0. In the video, this is shown by the LED2 that is off. If we set the magnet close to the hall sensor, so that the resistance of the hall sensor decreases, the digital output switches from 0 to 1 and LED2 turns on.

Read the Digital Value of the KY-003 Hall Sensor

In the following example we want to switch the magnetic hall sensor on and off. But you can not turn off or on the sensor with the same polarity. Therefore you have to change the polarity to trigger the magnetic sensor, which you also see in the video at the end of this article. To visualize if the sensor or the switch is turned on or off, I use a LED and the digital signal of the KY-003.

The KY-003 module has no direct schematic because the module only extends the output of the 3144EUA-S hall-effect switch.

Wiring between KY-003 Hall Sensor Module and Microcontroller

The following pictures show the wiring between the KY-003 magnetic hall sensor and the Arduino microcontroller. We are not using any ESP8266 or ESP32 boards, because the minimum operation voltage of the KY-003 module is 4.5V and the operation voltage of the ESP microcontroller is 3.3V.

Magnetic Hall Sensor KY-003 Arduino Nano
Magnetic Hall Sensor KY-003 Arduino Nano

For more information about the Arduino Nano, visit the Arduino Nano Tutorial.

Magnetic Hall Sensor KY-003 Arduino Pro Mini
Magnetic Hall Sensor KY-003 Arduino Pro Mini
Magnetic Hall Sensor KY-003 Arduino Uno
Magnetic Hall Sensor KY-003 Arduino Uno

For more information about the Arduino Uno, visit the Arduino Uno Tutorial.

Magnetic Hall Sensor KY-003 Arduino Mega
Magnetic Hall Sensor KY-003 Arduino Mega

For more information about the Arduino Mega, visit the Arduino Mega Tutorial.

Code to read the Digital Values of the KY-003 Module

For the program code we need to make a couple of changes because we want to turn the LED on and off based on the current status of the magnet hall sensor.

// for Arduino microcontroller
int led = 9;
int hallsensor = 8;

void setup() {
  pinMode(led, OUTPUT); 
  pinMode(hallsensor, INPUT); 
}

void loop() {
  int digitalVal = digitalRead(hallsensor);
  
  if (digitalVal == LOW) {
    digitalWrite (led, HIGH);
  }
  else {
    digitalWrite (led, LOW);
  }
  delay(100);
}

At the beginning of the script we have to define on which digital I/O pins are the LED and the hall sensor connected. In the setup function, we define the LED pin as output and the pin of the hall sensor as input. The loop function start with reading the digital value from the KY-003 hall sensor. If the digital state is 0 (equal to LOW) we turn on the LED because there is a magnetic field recognized. Otherwise we turn the LED off. At the end of the script we include a short delay of 0.1 seconds before we start the loop function again.

Results reading the Digital Values of the KY-003 Module

The following video shows that the LED is turned on, when the magnet comes near the hall sensor. But if the magnet is not present, the status of the hall sensor does not change. The digital value is still 0 and therefore the LED is on. Only when the polarity of the magnet is changed, we can reset the magnetic hall sensor. The digital value returns to 1 and the LED goes off.

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.

6 thoughts on “Hall Sensor Tutorial for Arduino, ESP8266 and ESP32”

  1. Hi! I was wondering what exactly the unit is that’s recorded by the Linear Hall Sensor – the one that equates 5V to 1023?

    Reply
    • Yes, the onboard Analog to Digital converter converts the voltage to a digital value between 0 and 1023 where 5V is equivalent to 1023.

      Reply
      • Just to clarify, you can operate the depicted linear Hall sensor module with 3.3 or 5 Volts. But DO NOT feed a 5V-powered Hall sensor output into a 3.3V logic. You would need a logic level converter in between the two.

        Reply

Leave a Comment

Get your free Microcontroller Datasheet eBook

Get your free Microcontroller Datasheet eBook now, by joining the DIYI0T newsletter

- Ranking of 14 Arduino, ESP8266 and ESP32 boards

- Comparison of the Power Consumption of all Boards

- Detailed Datasheet Information and Pinouts