Arduino Nano Hall Magnetic Sensor
The Hall Magnetic Sensor project showcases how to use an Arduino Nano to detect a magnetic field with a Hall effect sensor. This project will light up an LED when a magnetic field is detected. It is useful for applications in security systems, contactless switches, and magnetic field detection.
Components Needed
- Arduino Nano
- Hall Magnetic Sensor (e.g. KY‑003 type)
- LED (optional if not using built‑in LED)
- 220Ω Resistor (if using an external LED)
- Breadboard and Jumper Wires
Circuit Setup
1. Connect Hall Magnetic Sensor to Arduino Nano:
Connect the Vcc pin of the Hall sensor to the 5V pin on the Arduino Nano.
Connect the GND pin of the Hall sensor to the GND pin on the Arduino Nano.
Connect the output pin of the Hall sensor to digital pin 2 on the Arduino Nano.
2. Connect LED to Arduino Nano (Optional):
If using an external LED, connect the anode (long leg) of the LED to digital pin 13 on the Arduino Nano through a 220Ω resistor.
Connect the cathode (short leg) of the LED to the GND pin on the Arduino Nano.
How the Hall Sensor Works
Most Hall modules give a digital output: HIGH when no magnet is near, and LOW when a magnetic field is detected (or vice versa, depending on the module). The Arduino reads this pin and triggers an action such as turning on an LED or printing a message.
// Hall sensor on pin 2, LED on pin 13 (Nano's built‑in LED)
const int hallSensorPin = 2;
const int ledPin = 13;
void setup() {
pinMode(hallSensorPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("=== HALL SENSOR MODULE READY ===");
Serial.println("Status: 0=No magnet, 1=Magnet detected");
}
void loop() {
int sensorValue = digitalRead(hallSensorPin);
// Many Hall modules become LOW when magnet is near
if (sensorValue == LOW) {
digitalWrite(ledPin, HIGH);
Serial.println("1"); // Magnet detected
} else {
digitalWrite(ledPin, LOW);
Serial.println("0"); // No magnet
}
delay(100); // Smooth reading
}
Instructions
1. Circuit Setup:
Wire the Hall magnetic sensor and the LED to the Arduino Nano as described in the circuit setup section.
2. Code Upload:
Connect the Arduino Nano to your computer using a USB cable.
Open the Arduino IDE and paste the provided Hall sensor code.
Select the appropriate board (Arduino Nano) and port from the Tools menu.
Upload the code to the Arduino Nano.
3. Testing:
Once the code is uploaded, bring a small magnet near the Hall sensor.
Observe the LED lighting up and the sensor values printed on the Serial Monitor.
Applications
Security Systems: Detect if a door or window is opened or closed (using a magnet on the moving part and sensor on the frame).
Contactless Switches: Use magnetic fields to trigger switches without physical contact, ideal for sealed or dusty environments.
Magnetic Field Detection: Measure the presence and approximate strength of magnetic fields in science or hobby projects.
Troubleshooting Common Issues
- LED never turns on? Check if the Hall sensor output is LOW or HIGH when a magnet is near; swap the `if` condition in code if needed.
- Sensor not responding? Verify Vcc, GND, and signal pin wiring; some modules have a small LED that shows active state.
- Erratic readings? Increase the delay from 100 ms to 200–300 ms, or add a small pull‑up/pull‑down resistor if your module supports it.
Best Practices and Notes
- Use the built‑in LED on pin 13 if you don’t want extra wiring, or an external LED with a 220Ω resistor for higher brightness.
- Always double‑check the Hall sensor module’s datasheet; some detect magnet when output goes LOW, others HIGH.
- Keep the magnet at a small distance from the sensor; very close contact can saturate the sensor and cause unstable readings.
- Combine this circuit with a relay or MOSFET if you want to control high‑power devices (motors, solenoids) using the Hall trigger.
Variants and Extensions
You can extend this project by replacing the LED with a buzzer to create a magnetic alarm, or by using the Hall sensor as a speed sensor to count motor rotations per second. For more precise control, you can also read the Hall sensor using an interrupt pin for faster response.