MyHomeThings
Published © MIT

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.

BeginnerFull instructions provided1 hour7,720
Using ESP32 built-in hall effect sensor

Things used in this project

Hardware components

Espressif ESP32 Development Board
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Read hall sensor

Arduino
int hallValue;

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

void loop()
{
  hallValue = hallRead();

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

  delay(1000);
}

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

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

Send a push message when opening a door

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

Credits

MyHomeThings

MyHomeThings

11 projects • 4 followers

Comments