Using ESP32 built-in hall effect sensor

All ESP32 cards have a built-in Hall Effect sensor. The Hall Effect sensor can detect changes in the magnetic field in its environment. The larger the magnetic field, the higher the output voltage of the Hall Effect sensor.

The Arduino has a function hallRead() that returns the voltage difference generated by the magnetic field as an analog value. The stronger the external magnetic field, the higher the analog value returned by the function.

Open the Arduino IDE, select the appropriate ESP32 board and port, then upload the code below.

int hallValue;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  hallValue = hallRead();

  Serial.print("ESP32 Hall effect sensor value: ");
  Serial.println(hallValue);

  delay(1000);
}

When the upload is complete, open the serial monitor.

Approximate a magnet to the ESP32 hall sensor and see how the values returned by the hallRead function change.

If you flip the magnet, the sign of the values changes. Depending on the polarity of the magnet, the value returned by the Hall effect sensor will be positive or negative.

The nearer the magnet is to the sensor, the higher the absolute values.

Create an opening sensor with ESP32’s built-in hall sensor

The following example shows how to make a door/window opening sensor using ESP32 and a magnet.

int hallValue;
const int thresholdValue = 40;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  hallValue = hallRead();

  Serial.print("ESP32 Hall effect sensor value: ");
  Serial.print(hallValue);

  if(abs(hallValue) < thresholdValue)
  {
    Serial.println(" - The door is open!");
  }
  else
  {
    Serial.println(" - The door is closed!");
  }
  Serial.println();
  delay(1000);
}

In the example above, a threshold value is defined and stored in the thresholdValue constant.

Compare this with the HallValue value and if it is below the threshold, the magnet has probably been removed from the vicinity of the ESP32’s built-in Hall Effect sensor. A notification corresponding to this value is displayed on the serial monitor.

SONOFF SNZB-04 ZigBee Wireless Door Window Sensor

SONOFF ZigBee Door Window Sensor

SONOFF SNZB-04 ZigBee Wireless Door Window Sensor, Burglar Alarm for Home Security, Compatible with Alexa/Google Home

advertising – amazon.com

Send a push message when opening a door

The following code can be used to send a push message from the ESP32 to the specified device (phone, tablet, computer) when a door is opened.

You can read about using ESP32 and Pushsafer here.

#include <WiFi.h>
#include <WiFiClient.h>
#include <Pushsafer.h>

#define PushsaferKey "Your_private_key"         // http://pushsafer.com

int hallValue;
int thresholdValue = 40;
bool pushsaferFlag = false;

const char ssid[] = "SSID";                     // Your SSID
const char password[] = "Password";             // Your password

WiFiClient client;
Pushsafer pushsafer(PushsaferKey, client);

void setup()
{
  WiFi.mode(WIFI_STA);
  delay(100);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
}

void loop()
{
  hallValue = hallRead();
  if(abs(hallValue) < thresholdValue)
  {
    if(!pushsaferFlag)
    {
      struct PushSaferInput input;
      input.message = "The door is open!";
      input.title = "Attention!";
      input.sound = "6";
      input.vibration = "1";
      input.icon = "1";
      input.iconcolor = "#00FF00";
      input.priority = "1";
      input.device = "xxxxx";                 // Your Device ID:  http://pushsafer.com
      pushsafer.sendEvent(input);
      pushsaferFlag = true;
    }
  }
  else
  {
    pushsaferFlag = false;
  }
  delay(300);
}