A Hall effect sensor (or simply Hall sensor) is a type of sensor which detects the presence and strength of a magnetic field using the Hall effect, and is commonly used in applications such as proximity sensing, positioning, speed detection, and current sensing.

These sensors are so inexpensive that you can get a dozen for a dollar. But, when such an inexpensive sensor comes built-in with a feature-rich WiFi-enabled microcontroller, such as the ESP32, it may seem unnecessary at first. Nevertheless, who knows? You might come up with an idea to use it at some point in the future. A WiFi door status sensor, perhaps. See? You�re already getting some ideas.

So, let�s learn how to read the Hall sensor on the ESP32. But first, let�s go over how Hall-effect sensors work.

How Hall-effect Sensors Work?

A Hall-effect sensor uses a phenomenon called the Hall effect, which was discovered by Edwin Hall in 1879. The basic concept is simple:

Consider a conductive sheet shaped like a dollar bill. When a constant voltage source is connected across the left and right sides, it causes electrons to flow through the conductor in a straight line. Without any magnetic field present, if you were to measure the voltage across the top and bottom of the sheet, you�d find it to be almost zero.

hall eelement without magnetic field present

However, when a magnetic field is present with flux lines at right angles to the current, Lorentz force acts on the electrons. This force makes them deviate from their straight-line path, causing them to accumulate on one side of the conductor while being absent on the other. As a result, one side of the conductor becomes more electron-dense than the other. This leads to a potential difference (known as the Hall voltage) across the conductor. This phenomenon is referred to as the Hall effect.

hall effect explained

The stronger the magnetic field, the greater the deflection of electrons; the larger the current, the more electrons there are to deflect. In either case, the Hall voltage will be larger. In other words, the Hall voltage is directly proportional to both the electric current flowing through the conductor and the strength of the magnetic field.

Therefore, by measuring the Hall voltage for a known current, the strength of the magnetic field can be determined.

ESP32 Hall Effect Sensor

The ESP32 has a built-in hall effect sensor located beneath the metal lid of the ESP32-WROOM-32 module itself.

esp32 hall sensor

Being integrated into the ESP32 means that you can easily connect the sensor readings with WiFi or Bluetooth functionalities, making remote monitoring and control easier.

While the onboard Hall sensor might not replace dedicated external sensors for precise applications due to its positioning and sensitivity, it still offers a range of potential uses. This includes basic magnetic field detection, triggering specific functions when a magnet is nearby, or even building simple educational projects to understand the Hall effect.

Because the sensor is located beneath the metal lid, it is less sensitive to weak magnetic fields than standalone Hall sensors, so magnets of significant strength are usually required to obtain noticeable readings.

Reading the Hall Sensor

Reading the hall sensor on the ESP32 is straightforward. In the Arduino IDE, you use the hallRead() function. This function returns an integer value that represents the Hall voltage.

hallRead();

Example Code

Let�s read the hall sensor using an example from the library. Open your Arduino IDE, and navigate to File > Examples > ESP32, and open the HallSensor sketch.

This example simply reads the internal hall sensor on the ESP32 and shows the result on the serial monitor.

int val = 0;
void setup() {
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  val = hallRead();
  // print the results to the serial monitor:
  //Serial.print("sensor = ");
  Serial.println(val);//to graph 
}

Once you have uploaded the sketch, open the serial monitor at baud rate 9600 and press the EN button on the ESP32.

Now try to bring a magnet close to the ESP32 chip, you will see that the readings change depending on the distance and polarity of the magnet.

esp32 hall sensor output

Visualizing the signal in the Serial Plotter will help you understand that: when no magnetic field is detected, the output remains at approximately 100. If the south pole of a magnet is brought near, the output increases toward 200, and if the north pole of a magnet is brought close, the output decreases toward 0.

esp32 hall sensor serial plotter output

Login
ADS CODE