ESP32 Internal Hall Sensor Example

ESP32 comes with in build hall sensor, this sensor can be used to detect presence of magnet, Like door sensor. In this tutorial we will see how to read its value and detect presence of magnet.

A Hall effect sensor is a transducer that varies its output voltage in response to a magnetic field. Hall effect sensors are used for proximity sensing, positioning, speed detection, and current sensing applications.[1]

ESP32 Internal Hall Effect Sensor

In a Hall effect sensor, a thin strip of metal has a current applied along it. In the presence of a magnetic field, the electrons in the metal strip are deflected toward one edge, producing a voltage gradient across the short side of the strip (perpendicular to the feed current). Hall effect sensors have an advantage over inductive sensors in that, while inductive sensors respond to a changing magnetic field which induces current in a coil of wire and produces voltage at its output, Hall effect sensors can detect static (non-changing) magnetic fields.

In its simplest form, the sensor operates as an analog transducer, directly returning a voltage. With a known magnetic field, its distance from the Hall plate can be determined. Using groups of sensors, the relative position of the magnet can be deduced.

Frequently, a Hall sensor is combined with threshold detection so that it acts as and is called a switch. Commonly seen in industrial applications such as the pictured pneumatic cylinder, they are also used in consumer equipment; for example some computer printers use them to detect missing paper and open covers. They can also be used in computer keyboards, an application that requires ultra-high reliability.

ESP32 Arduino IDE Code for Internal Hall Sensor

 *  Copyright (c) 2018,
 *  All rights reserved.
 * ESP32 Hall Sensor Example

 #define LED 2
//         Setup
void setup() {

//         Loop
void loop() { 
  int sensor = hallRead();  //Reads Hall sensor value
  Serial.print("Sensor Reading:");

  digitalWrite(LED,(sensor < 0) ? HIGH : LOW); //On board LED detects presence of magnet



After uploading code, open serial monitor from tools menu, and move magnet near to ESP32 and observe readings also on board blue LED detects magnet poles.


Leave a Reply