Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 06: Hall Sensor Module

In this lesson, you will learn how to use a Hall sensor with an ESP32 Development Board to detect the polarity of a magnetic field. We’ll cover reading analog signals from the sensor and interpreting them to differentiate between south and north poles. This project is ideal for beginners in electronics, providing practical experience with sensors and signal processing on the ESP32 platform.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

ESP32 & Development Board (ESP32 Board)

BUY

Hall Sensor Module

-

Breadboard

BUY

Wiring

../_images/Lesson_06_Hall_Sensor_Module_esp32_bb.png

Code

Code Analysis

  1. Setting up the Hall Sensor

    const int hallSensorPin = 25;  // Pin connected to the Hall sensor output
    void setup() {
      Serial.begin(9600);             // Initialize serial communication at 9600 bps
      pinMode(hallSensorPin, INPUT);  // Set hall sensor pin as input
    }
    

    The hall sensor’s output is connected to pin 25 on the ESP32 Development Board. The setup() function is used to initialize serial communication at 9600 bits per second (bps) for displaying data on the serial monitor. The pinMode() function is used to configure 25 as an input pin.

  2. Reading from the Hall Sensor and Determining Polarity

    The Hall sensor module is equipped with a 49E linear Hall effect sensor, which can measure the polarity of the magnetic field’s north and south poles as well as the relative strength of the magnetic field. If you place a magnet’s south pole near the side marked with 49E (the side with text engraved on it), the value read by the code will increase linearly in proportion to the applied magnetic field strength. Conversely, if you place a north pole near this side, the value read by the code will decrease linearly in proportion to that magnetic field strength. For more details, please refer to Hall Sensor Module.

    void loop() {
      int sensorValue = analogRead(hallSensorPin);  // Read analog value from Hall sensor
      Serial.print(sensorValue);                    // Output raw sensor value to Serial Monitor
      delay(200);                                   // Delay for 200 milliseconds
    
      // Determine magnetic pole based on sensor value
      if (sensorValue >= 2600) {
        Serial.print(" - South pole detected");  // South pole detected if value >= 2600
      } else if (sensorValue <= 1200) {
        Serial.print(" - North pole detected");  // North pole detected if value <= 1200
      }
    
      Serial.println();  // New line for next output
    }