HOW to Detect Magnetic Field WIth ESP32?
Description
In this kit, there is a Hall sensor which mainly adopts a A3144 linear Hall element. The element P1 is composed of a voltage regulator, a Hall voltage generator, a differential amplifier, a Schmitt trigger, a temperature compensation circuit and an open-collector output stage. In the experiment, we use the Hall sensor to detect the magnetic field and display the test results on the shell.
Working Principle
When the sensor detects no magnetic field or a north pole magnetic field, the signal terminal will be high level; when it senses a south pole magnetic field, the signal terminal will be low levels. The stronger the magnetic field strength is, induction distance is longer.
Required Components
Connection Diagram
Test Code
//*************************************************************************************
/*
* Description : Reading the value of hall magnetic sensor
* Auther : http://www.keyestudio.com
*/
int val = 0;
int hallPin = 15; //Hall sensor pin is connected to GPIO15
void setup() {
Serial.begin(9600);//Set baud rate to 9600
pinMode(hallPin, INPUT);//Set pin to input mode
}
void loop() {
val = digitalRead(hallPin);//Read the level value of hall sensor
Serial.print(val);//Print val
if (val == 0) {//There is a South Pole magnetic field
Serial.println(" The magnetic field at the South Pole!");
}
else {//If not
Serial.println(" Just be all normal!");
}
}
//*************************************************************************************
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on, open the serial monitor and set the baud rate to 9600. when the sensor detects no magnetic fields or the north pole magnetic field, the monitor l will show “1 Just be all normal!” and the LED on the sensor will be off; When it detects the south pole magnetic field,“0 The magnetic field at the South Pole!”and the LED on the sensor will be on.