Esp32 Hall Effect Sensor

What is the Hall Effect Sensor?

It is a sensor that senses the magnetic field and gives output as a voltage.

Voltage output is directly proportional to the magnitude of the magnet.


Hall Effect Sensor Working Image
Hall Effect Sensor Working

How Hall Effect Sensor Works?

When a current-carrying conductor comes in contact with a perpendicular magnetic field, it produces a voltage that is perpendicular to both the current and the magnetic field. This voltage is known as Hall Voltage.

 A typical Hall effect sensor is made up of a thin strip of semiconductor material through which a current passes.

 So using the Hall effect sensor we can detect the strength and polarity of the magnetic field isn’t that cool! 

Using this concept we can create a lot of applications. 


Applications of Hall Effect Sensor

The Hall effect sensor can be utilized for a variety of applications that require detecting the presence or absence of a magnetic field.

The Hall effect sensor doesn’t require any contact with the magnet that is why there is no damage from wear and tear, which leads to the long life of the sensor.

For example: we can mount a magnet on a door or window and the Hall effect sensor on the frame. When the door or window is opened or closed, the change in magnetic field is detected. Using this we can make our own security door system.

Not only this we can use this concept to make more useful applications like speed sensing, current sensing, automotive applications, contactless switches, magnetic encoding, smart lighting, magnet polarity checkers, and many more. 


Overview of ESP32 Hall Effect Sensor

  • The built-in hall effect sensor on the ESP32 development board allows it to monitor changes in the magnetic field around it.
  • By a little tinkering and some readings we get to know that the hall effect sensor is placed below this metal lid at the point which is highlighted in the below figure.
ESP32 Hall Sensor Position
ESP32 Hall Sensor Positioning


Important function 

To use the hall sensor of ESP32 we only have to remember one function which is hallRead()This function simply returns the hall sensor measurements. The values are directly proportional to the strength of the magnetic field.

Code For ESP32 Hall Effect Sensor

int hall_sensor_value = 0;

void setup() {

void loop() {
  hall_sensor_value = hallRead(); /* read hall effect sensor value using hallRead() function */
  Serial.println(hall_sensor_value);  /* printing the hall sensor value on serial monitor */

Code Explanation

Here we have created a global variable named hall_sensor_value to store the hall sensor value.

int hall_sensor_value = 0;

Inside setup function

Using the Serial.begin() function we have to initialize the serial monitor at a baud rate of 9600.


Inside loop function

Using the hallRead() function we can read the value of the sensor, after that, we store the value in our global variable.

hall_sensor_value = hallRead();

Using Serial.println() we are printing the value of the hall sensor on the serial monitor


A delay of 1 second so that we can read.


Now, open the Arduino IDE, and let’s upload the code to our ESP32. After that open the serial monitor. We will get reading like the image shown below. These readings are taken in the absence of magnet.


Hall Effect Sensor Readings Without Magnet
Hall Effect Sensor Readings Without Magnet


For the testing, we don’t have an actual magnet with us. So, we decided to use the earphone magnet. However, you should test it with any permanent magnet i.e. found in the sound system, etc.


Earphone Magnet
Earphone Magnet


Now place a magnet close to the place where the hall sensor is located as shown in the above figure. We will get a high positive value if the pole of the magnet is North as shown below.

ESP32 Hall Effect Sensor Magnet Test Readings
ESP32 Hall Effect Sensor Magnet Test Readings


Now place a magnet close to the place where the hall sensor is located as shown in the above figure. We will get a Negative value if the pole of the magnet is South as shown below.

Hall Effect Sensor Reading North Pole Magnet
Hall Effect Sensor Negative Readings ( South Pole Magnet)


So, In this guide, we have learned about what is Hall sensor, How it works, what are its applications, and what type of reading we get based on the polarity and strength of the magnet. Do a little tinkering with this concept and use it in real-life applications. 

Components Used

WiFi Development Tools - 802.11 ESP32 General Development Kit, embeds ESP32-WROOM-32E, 4MB flash.