
Hello reader, I hope you all are doing great.
The ESP32 development board is featured with some inbuilt sensors like a capacitive touch sensor and hall effect sensor. In this tutorial, we are going to discuss one of those inbuilt sensors that is the Hall Effect sensor. Hall Effect sensor is used to detect the variation in the magnetic field in its surrounding.
What is Hall Effect?
A potential difference is produced when a current-carrying conductor is placed in the magnetic field and the direction of the magnetic field is perpendicular to the flow of current. This potential difference is also known as Hall voltage.
How does the Hall Effect sensor work?
- The Hall-effect sensor is a transducer that responds to changes in its output voltage when placed in a magnetic field.
- The Hall Effect occurs when the action of a magnetic field causes a current in a conductor to deviate from its path.
- The greater the magnetic field greater will be the sensor output or voltage deviation.
- This deviation causes the Hall Voltage to be generated with an appropriate format, and this can be harnessed by an external circuit, which is what Hall sensors do. Hall voltage, which is proportional to the intensity of the field it creates, can be measured by an external circuit or used for sensing.
- A Hall-effect switch turns ON and OFF in the presence and absence of a magnetic field respectively.
Applications of Hall Effect sensor
- In an Automotive system to sense speed, distance and position.
- Proximity sensing
- Current sensing
- Anti-lock braking system.
- Internal combustion engine to assist with ignition timing.
- To switch an electric circuit ON and OFF.
Hall Effect sensor in ESP32
In ESP32 the Hall effect sensor is located inside ESP WROOM 32chip and the sensor is a piece of wire with continuous current flowing through it.
Hall sensor is non-contact type so they do not have to be in contact with physical elements.

Fig. 2
Programming ESP32 Hall Effect Sensor using Arduino IDE
To understand the working of Hall sensor with ESP32, an example is available is Arduino IDE.
- You can find the code through File> Examples> ESP32 > Hall Sensor.
- An image has been attached below for reference:
If you want to know more about the basics of ESP32 and how to get started with Arduino IDE, then follow the link: Introduction to ESP32 Programming Series
Arduino IDE code
//Simple sketch to access the internal hall effect detector on the esp32. //values can be quite low. //Brian Degger / @sctv 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 value = "); Serial.println (val);//to graph delay(100); }
Code Description
The code is very simple where a function is called to read the hall sensor value and store it into a variable and then printed on the Serial monitor.
- The first step will be the declaration of an integer type variable to store the hall sensor value. The initial value assigned to the variable is zero.

Fig.
Setup()
- Inside the setup function, the only task is to initialize the serial monitor for serial communication at a 9600 baud rate.
Loop()
- Inside the loop function, you need to call a function ‘hallRead()’ to read the sensor value and store those readings into variable ‘val’.
- Print the sensor readings on the serial monitor or serial plotter using serial.println() function.
- A delay of 0.3 sec is added

Fig.
Testing
Components Required:
- ESP32 development board
- Magnet
- USB cable
- After successfully uploading the code into ESP32 open the serial plotter or serial monitor to see the results.
- Place a magnet near the ESP32 board. The sensor reading will increase or decrease depending upon the fact that which magnet pole is facing the Hall sensor.

Fig.
- We have attached an image below from the serial plotter for your reference:
- As we mentioned above, the sensor reading will either increase positively or negatively depending upon the magnet pole facing the hall sensor.
- In the image attached below, you can see positive, negative, and sometimes ‘0’ sensor output.
- The distance between the magnet and the Hall sensor decides the amount of potential difference generated.
- The greater the distance between the two, the smaller the hall voltage or potential difference will be.
- We have attached an image from the Arduino IDE serial monitor for your reference.

Fig. Serial monitor
This concludes the tutorial. I hope you found this helpful and I hope to see you soon with a new tutorial on ESP32.