Skip to main content

How to use a Hall Effect Sensor

What is a Hall Effect Sensor?

The hall effect sensor is a type of magnetic sensor which can be used for detecting the strength and direction of a magnetic field produced from a permanent magnet or an electromagnet with its output varying in proportion to the strength of the magnetic field being detected.

image-1706547936079.png

Wiring

  1. left to 5V (Power)
  2. middle to GND
  3. right to PIN2 via 10K resistor halleffectcircuit.png

Getting started

The following code uses digitalRead() to get a integer (1/0) representing the detection of magnet.

const int hallSensorPin = 2;  // Hall Effect sensor connected to digital pin 2
int hallSensorState;          // Variable to store the state of the sensor

void setup() {
  Serial.begin(9600);                // Start serial communication at 9600 baud
  pinMode(hallSensorPin, INPUT);     // Set the Hall Effect sensor pin as an INPUT
}

void loop() {
  hallSensorState = digitalRead(hallSensorPin); // Read the state of the sensor
  Serial.println(hallSensorState); 

  delay(100); 
}