ESP32 built in hall effect sensor with Arduino IDE




All ESP32 chips have built-in hall effect sensor. Whatever type of ESP32 development board you use, it must have ESP32 chip with built-in hall effect sensor. Hall effect sensors are used to measure the magnetic field. Hall effect sensors detect the magnetic field of lines in their surroundings and produce a voltage at the output pins of the sensor. Similarly, ESP32 has built-in hall sensor inside its chip which can be used to measure the magnetic field. We can measure the output of this hall sensor at one of the GPIO pins of the ESP32 development board. Hall effect sensors are used to measure magnetic field strength and also used for current measurement. You can read about our previous projects which are built using different types of hall effect sensors.




Now, let’s move to the main topic of this tutorial. The first question is where the hall effect sensor located in ESP32 chip.  For all ESP32 chips, it is located under this metal cover of the ESP32 board. When you bring a magnet near to this metal cover, it detects the variations in the magnetic field and produces an output voltage according to the strength of the magnetic field.

hall sensor ESP32 position

ESP32 development board does not provide any pin for the output measurement of the hall sensor. Because we only want to read the value of voltage according to magnetic field strength in the surroundings of ESP32 board. Therefore output its built-in hall effect sensor saves in a register of ESP32 board, and we can easily read it with Arduino IDE built-in function.




Program for built-in hall effect sensor of ESP32

If you are just a beginner with ESP32 programming with Arduino IDE, you can check these getting started tutorials:

It is straightforward to measure the output of the hall effect sensor using this simple code and HallRead() function of the Arduino IDE.  HallRead() role reads the value of the output of the hall sensor and returns the result in a declared variable as we stated in the code below.

int hall_sensor_value = 0;
void setup()

{
Serial.begin(9600);  // It defines the baud rate of 9600 bits per second to serial monitor
}
void loop()

{
hall_sensor_value = hallRead();
Serial.print("Hall sensor value = ");
Serial.println(hall_sensor_value);
delay(500);
}

The code given above is very simple, and all code is the same as used in the last tutorial except the hallRead() function.  This function does not need any argument. It reads the value of hall sensor output which is stored in a register of ESP32.  We can simply save the output of hallRead() function in any variables. In this code, we have saved the output voltage of the hall sensor in a variable name” hall_sensor_value. After that, we are sending these values to the serial monitor. The output of hallRead() function can be either positive or negative depending on the direction magnetic field.

  • Now just copy the above code and paste it in Arduino IDE.
  • Compile this code and see if you got any errors. Remember when you copy code directly from the website, there are some changes of errors in code like semicolons, etc. So you can fix those errors easily.
  • After compiling code, upload the code to Arduino IDE by clicking on Arduino button.
  • Make sure you have selected a correct COM pin to which ESP32 board is connected.
  • After uploading code, open the serial monitor of Arduino IDE by going to the tools menu.
  • You will get output like this on the serial monitor.

hall effect sensor ESP32 values

As you can see in the above pictures, we are getting values according to the strength of the magnetic field and also negative values shows the direction of the field.  you may also like to check related tutorials on ESP32:



Leave a Comment