0
0
AutocadHow-ToBeginner · 4 min read

How to Use Hall Effect Sensor with Arduino: Simple Guide

To use a Hall effect sensor with Arduino, connect its power and ground pins to Arduino's 5V and GND, and connect the sensor output to an Arduino digital input pin. Then, use digitalRead() in your Arduino code to detect the magnetic field presence from the sensor output.
📐

Syntax

The basic syntax to read a Hall effect sensor in Arduino is:

  • pinMode(sensorPin, INPUT); sets the sensor pin as input.
  • digitalRead(sensorPin); reads the sensor output as HIGH or LOW.

This lets you detect if a magnetic field is near the sensor.

arduino
const int sensorPin = 2;

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorValue = digitalRead(sensorPin);
  Serial.println(sensorValue);
  delay(500);
}
Output
1 1 0 0 1 ... (varies depending on magnetic field presence)
💻

Example

This example shows how to connect a Hall effect sensor to Arduino pin 2 and print "Magnet detected" when a magnet is near, or "No magnet" when not.

arduino
const int hallSensorPin = 2;

void setup() {
  pinMode(hallSensorPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  int sensorState = digitalRead(hallSensorPin);
  if (sensorState == LOW) {
    Serial.println("Magnet detected");
  } else {
    Serial.println("No magnet");
  }
  delay(500);
}
Output
No magnet No magnet Magnet detected Magnet detected No magnet ...
⚠️

Common Pitfalls

  • Incorrect wiring: Make sure the sensor's power pin goes to 5V and ground to GND; swapping these can damage the sensor.
  • Wrong pin mode: Always set the sensor pin as INPUT in setup().
  • Ignoring sensor output type: Some Hall sensors output LOW when magnet is near; check your sensor datasheet.
  • No pull-up resistor: If your sensor output is open-collector, use a pull-up resistor or enable Arduino's internal pull-up with pinMode(pin, INPUT_PULLUP);.
arduino
/* Wrong way: Missing pinMode setup */
const int sensorPin = 2;

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

void loop() {
  int val = digitalRead(sensorPin); // May read random values
  Serial.println(val);
  delay(500);
}

/* Right way: */

void setup() {
  pinMode(sensorPin, INPUT);
  Serial.begin(9600);
}
📊

Quick Reference

Here is a quick checklist for using a Hall effect sensor with Arduino:

  • Connect sensor VCC to 5V and GND to Arduino GND.
  • Connect sensor output to a digital input pin (e.g., pin 2).
  • Set pin mode to INPUT or INPUT_PULLUP if needed.
  • Use digitalRead() to detect magnet presence.
  • Check sensor datasheet for output logic (LOW or HIGH when magnet is near).

Key Takeaways

Connect Hall effect sensor power to 5V and ground to GND on Arduino.
Use digitalRead() on the sensor output pin set as INPUT to detect magnets.
Check sensor output logic to know if LOW or HIGH means magnet detected.
Use internal pull-up resistor if sensor output is open-collector type.
Always verify wiring and pin modes to avoid incorrect readings or damage.