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

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.

ESP32 Built-In 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.

ESP32 Boards Add-on install ESP32 board version 2

Watch the Video Tutorial

You can watch the video tutorial or keep reading this page for the written instructions to learn about the ESP32 hall effect sensor. This video covers how to use the hall effect sensor with Arduino IDE. Scroll down to learn how to use it with MicroPython firmware.

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.

The ESP32 Hall Effect Sensor

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.

ESP32 Hall Effect Sensor Effect

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);
}

View raw code

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:

Compile and Upload Code to ESP32 using Arduino IDE

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…

ESP32 Hall Effect Sensor Readings Arduino IDE serial monitor

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.

ESP32 Hall Effect Sensor Readings Arduino IDE serial monitor

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:

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



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

13 thoughts on “ESP32 Built-In Hall Effect Sensor with Arduino IDE and MicroPython”

  1. Good to know. Thanks.
    Though practical applications might be a bit hampered by the sensor being built in, I can definitely use this as the magnet can come close to the ESP.
    Great

    Reply
  2. Dear all, first of all thank you for the perfect information about the ESP32 MCU.
    Now I want to measure pulse count measurements on one of the digital input pins of the ESP32. Which book of you treat this pulse count or frequency measurement in detail?

    Thanks a lot.

    Reply
  3. Hi Sara,
    I’ve installed uPyCraft and micropython onto my ESP32, and “blink” and other examples work, but I get the message

    exec(open(‘blink.py’).read(),globals())
    Traceback (most recent call last):
    File “”, line 1, in
    File “”, line 9, in
    ImportError: no module named ‘esp32’

    when I try to follow this example. Any idea?

    Reply
    • Hi.
      Can you provide more details? What do you mean when you say it works, but you got an error? Usually, when you get an error with micropython, the code will not run.
      Regards,
      Sara

      Reply
      • The simple examples (blink, etc) will run, so I know micropython is working.
        But the addition/importing of “esp32” throws the error

        ImportError: no module named ‘esp32’

        Reply
        • Hi.
          I just tested this, and it is working as expected.
          What does your files look like? What ESP32 model do you have?
          Regards,
          Sara

          Reply
          • Ok, I got it to work. I installed Thonny and used the very latest version of the ESP32 micrpython firmware, and bingo.

            Using uPyCraft 1.1 and the automatic firmware installation settings on that DID NOT work – it is clearly installing an older version of the ESP32 micropython which doesn’t have the esp32 module to import.

  4. Hi,
    I would like to know if there is any parameter which could be used for the esp32.hall_sensor() function.
    Thank you.

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.