ESP32 Arduino: Internal Hall sensor measurements

The objective of this post is to explain how to get measurements from the ESP32 internal Hall sensor. The tests of this tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


The objective of this post is to explain how to get measurements from the ESP32 internal Hall sensor, using the ESP32 Arduino core.

An Hall sensor is a device that, when under the effect of a magnetic field, outputs a proportional voltage [1]. This is one of the many sensores included in the ESP32 microcontroller [2].

We will first obtain the values when no magnetic field is nearby and then test the results when we approximate a magnet to the ESP32. The code shown here is based on this example from the Arduino core, which I encourage you to try.

The tests of this tutorial were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.


The code

For this ESP32 tutorial, we will not need any library includes. Also, the setup function will be very simple and basically consists on opening a serial communication, so we can then send the Hall sensor measurements to a computer.

void setup() {

The rest of the code will be done in the main loop function. We will want to do a periodic measurement of the sensor values. But first, we declare an integer variable, to hold the result of each measurement.

int measurement = 0;

Then, to obtain the actual measurement, we simply call the hallRead function, which will return the value from the Hall sensor. Naturally, we will store the value in our previously declared variable.

 measurement = hallRead();

After having the value, we will print it to the serial port, so we can then check the value using the Arduino IDE serial monitor.

Serial.print("Hall sensor measurement: ");

Finally, we do a delay before getting to the next cycle of our measurement loop. To do so, we simply call Arduino’s delay function, passing as input the number of milliseconds to wait. In this example we will delay for 1 second, but you can play with other values.


The full source code for this ESP32 tutorial can be seen bellow.

void setup() {

void loop() {

    int measurement = 0;

    measurement = hallRead();

    Serial.print("Hall sensor measurement: ");



Running the code

To test the code, just compile it and upload it to the board using the Arduino IDE. Then, open the serial monitor and set the baud rate to the same value defined in our setup function.

Without any magnetic field near the ESP32, you should get some measurements near 0, as can be seen bellow in figure 1.

ESP32 Internal Hall sensor stable

Figure 1 – Hall sensor measurements without magnetic field near by.

Then grab a magnet and put it near the ESP32 chip. Depending on the magnetic poll that is near, the values of the Hall sensor measurements should either increase or decrease, as shown in figure 2.

ESP32 Internal Hall sensor under magnetic field

Figure 2 – Value from the ESP32 Hall sensor increased.




This entry was posted in Misc. Bookmark the permalink.

2 Responses to ESP32 Arduino: Internal Hall sensor measurements

  1. Paul says:

    Very good. It works as stated. I wonder why they included the hall sensor in the IC. Any application I’ve seen the hall is remote. Cool experiment though.

    Liked by 1 person

    • antepher says:

      Hi! Thanks 🙂

      I think that given the price of these kind of devices and the cost of integrating them in chips, we will be seeing more and more microcontrollers shipping with additional hardware such as this. But it’s just a guess.

      Regarding the applications, maybe it is related to allow the applications to be aware of the surrounding conditions. The ESP32 has both a hall sensor and a temperature sensor. Magnetic fields and temperature are two factors that may impact the operation of some electronic components, and thus being aware of them may be useful to understand some failing conditions of devices, or trigger some warnings. But it’s again another guess.

      Would like to ear more from others about their opinion on that, it is indeed a good question 🙂

      Best regards,
      Nuno Santos


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s