IoT/ESP32 B.Tech/Diploma Beginner

ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython

Learn how to use the ESP32 built-in Hall Effect Sensor with Arduino IDE and MicroPython to detect changes in the magnetic field in its surroundings. Try our sketch example.

  • Suitable forB.Tech/Diploma IoT/ESP32 students
  • Build time1-2 weeks
  • LevelBeginner
  • Available atAjvika Labs, Kakinada

About this project

The ESP32 development board features a built-in hall effect sensor that detects changes in the magnetic field in its surroundings. This tutorial shows how to use the ESP32 hall effect sensor with Arduino IDE and MicroPython.
The
hallRead() function has been discontinued
. Reading the hall sensor is no longer supported on the ESP32 version 3.X. You can still use this function, but you need to downgrade your ESP32 board add-on to version 2.
The ESP32 Hall Effect Sensor
The
ESP32 board
features a built-in hall effect sensor located behind the metal lid of the ESP32 chip as shown in the following figure.
A hall effect sensor can detect variations in the magnetic field in its surroundings. The greater the magnetic field, the greater the sensor’s output voltage.
The hall effect sensor can be combined with a threshold detection to act as a switch, for example. Additionally, hall effect sensors are mainly used to:
Detect proximity;
Calculate positioning;
Count the number of revolutions of a wheel;
Detect a door closing;
And much more.
Read Hall Effect Sensor – Arduino IDE
Reading the hall effect sensor measurements with the ESP32 using the Arduino IDE is as simple as using the
hallRead()
function.
Use the following 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);
}

// put your main code here, to run repeatedly
void loop() {
// read hall effect sensor value
val = hallRead();
// print the results to the serial monitor
Serial.println(val);
delay(1000);
}
This example simply reads the hall sensor measurements and displays them on the Serial monitor.
val = hallRead();
Serial.println(val);
Add a delay of one second in the loop, so that you can actually read the values.
delay(1000);
Upload the code to your ESP32 board:
Demonstration
Once the upload is finished, open the Serial Monitor at a baud rate of 9600. Approximate a magnet to the ESP32 hall sensor and see the values increasing…
Or decreasing depending on the magnet pole that is facing the sensor:
The closer the magnet is to the sensor, the greater the absolute values are.
Read Hall Effect Sensor – MicroPython
To read the ESP32 hall effect sensor using MicroPython, you just need to use the following snippet of code:
import esp32
esp32.hall_sensor()
You need to import the
esp32
module. Then, use the
hall_sensor()
method.
If you want to print the readings on the shell, you just need to use the
print()
function:
print(esp32.hall_sensor())
If you’re just getting started with MicroPython, you can read the following tutorial:
Getting Started with MicroPython on ESP32
Wrapping Up
Throughout this tutorial you’ve learned that:
The ESP32 features a built-in hall effect sensor
The hall effect sensor can detect magnetic field changes in its surroundings
The measurements from the sensor can increase or become negative depending on the magnet pole facing the sensor.
We hope you’ve found this tutorial useful. For more projects with the ESP32 you can check our project’s compilation:
20+ ESP32 Projects and Tutorials
This tutorial is a preview of the “
Learn ESP32 with Arduino IDE
” course. If you like this project, make sure you take a look at the
ESP32 course page
where we cover this and a lot more topics with the ESP32.
Updated August 29, 2019

Technologies used

ESP32ESP8266ArduinoRaspberry PiMQTTBluetoothBLEWS2812BPythonUARTMicroPythonArduino IDE

Hardware components

  • ESP32
  • ESP8266
  • Arduino
  • Raspberry Pi

Software & tools

  • Arduino IDE
  • Thonny
  • Node-RED
  • MQTT
  • MicroPython

What you get

  • Working project / hardware kit, tested before delivery
  • Complete source code with explanation
  • Project demo and guidance for your viva
  • Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed. Payment is in two parts: • 50% advance to start the project • Remaining 50% before delivery Note: • Documentation and PPT are not included by default • If you need documentation/PPT, it can be arranged for extra time and cost We'll confirm the exact timeline and cost with you after reviewing your requirements.