Arduino Basics: KY-003 Hall Magnetic Sensor Module
Abstract
Learn how to use the KY-003 Hall Magnetic Sensor Module with Arduino. This module utilizes the Hall Effect to detect the presence of a magnetic field and provides a digital ON/OFF signal. This tutorial focuses on configuring Arduino digital input pins to detect a magnet’s proximity, offering a non-contact solution for counting, proximity sensing, and speed measurement.
1. Introduction
The KY-003 uses a Hall Effect sensor (a specific integrated circuit) that outputs a voltage proportional to the strength of the magnetic field passing through it. The module’s circuitry converts this analog voltage into a clean digital HIGH or LOW signal. Unlike the mechanical reed switch (KY-021), the Hall Effect sensor has no moving parts, making it much faster and immune to mechanical wear and bounce.
In this episode, you’ll learn:
- The fundamental principle of the Hall Effect in sensing.
- How to safely wire the module as a digital input.
- How to read the sensor signal using digitalRead().
- How to use the input to implement a simple counter for rotational speed or events.
This project introduces a robust, solid-state method for non-contact magnetic detection.
2. Prerequisites
Make sure you have:
- An Arduino Uno or compatible board.
- One KY-003 Hall Magnetic Sensor Module.
- One small magnet (e.g., a neodymium magnet).
- Jumper Wires.
- Arduino IDE
3. Wiring and Setup for Arduino
The KY-003 module is typically a digital output device.
Step 1 – Identify Pins
The module has three pins: Signal (S or OUT), VCC (+), and Ground (− or GND).
Step 2 – Connect the Module
Wire the module to the Arduino as follows. The specific Hall sensor used on the module is usually latched or bipolar and often outputs LOW when a specific magnetic pole (South) is detected.
- Connect the – / GND pin of the KY-003 to the GND pin on the Arduino.
- Connect the + / VCC pin of the KY-003 to the 5V pin on the Arduino.
Connect the S / Signal pin of the KY-003 to Arduino Digital Pin 3.
This image was created with Fritzing
Step 3 – Initialize Pin Mode
In the setup() function of your code, set the signal pin as a standard input:
pinMode(8, INPUT);
We will use the on-board LED (Pin 13) as an alert indicator, set as OUTPUT.
4. Writing Vibration Detection Code
Since Hall sensors are ideal for counting events (like wheel rotations), we will write code that detects a change in the magnetic state to count the events.
const int HALL_PIN = 2;
const int LED_PIN = 13;
// Variables for counting events
int sensorState = 0; // Current state of the sensor (LOW/HIGH)
int lastSensorState = 0; // Previous state of the sensor
long eventCount = 0; // Total events counted
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(HALL_PIN, INPUT);
Serial.begin(9600);
Serial.println("KY-003 Hall Sensor Counter Ready!");
}
void loop() {
// Read current sensor state
sensorState = digitalRead(HALL_PIN);
// Check for the rising edge (transition from LOW to HIGH)
if (sensorState == HIGH && lastSensorState == LOW) {
eventCount++;
digitalWrite(LED_PIN, HIGH); // Flash LED briefly
Serial.print("Magnet Detected! Count: ");
Serial.println(eventCount);
delay(50); // Small delay to visualize flash
digitalWrite(LED_PIN, LOW);
}
// Save the current state for the next loop iteration
lastSensorState = sensorState;
}
Code Explanation
- Edge Detection: Because we check if sensorState is HIGH AND lastSensorState was LOW, the code only increments the counter once per rising edge (when the magnet leaves the detection zone, assuming the default active-low operation, or vice-versa, depending on the sensor’s pole). This prevents multiple counts for a single pass of the magnet.
- Solid State: The Hall sensor requires no debouncing (unlike mechanical switches), allowing for accurate, high-speed counting.
5. Using the Sensor for Simple Proximity Alert
For a simpler proximity alert (ON when magnet is near, OFF when magnet is far), you would use the sustained polling method without state change detection.
/* Simple Proximity Alert (replace loop() content from Section 4) */
sensorState = digitalRead(HALL_PIN);
// Assuming Active LOW: HIGH when magnet is far, LOW when magnet is near.
if (sensorState == LOW)
{
digitalWrite(LED_PIN, HIGH);
Serial.println("Magnet is present.");
}
else
{
digitalWrite(LED_PIN, LOW);
}
delay(10);
6. Uploading and Running the Project
Step 1 – Build
Click the Verify button (checkmark icon) in the Arduino IDE to compile the sketch.
Step 2 – Upload
- Connect your Arduino board via USB.
- Select the correct board and COM port.
- Click the Upload (arrow icon) button.
Step 3 – Test
- Open the Serial Monitor (Tools > Serial Monitor).
- Pass a magnet rapidly past the face of the Hall sensor.
- The LED should flash briefly and the counter in the Serial Monitor should increment by 1 for each magnetic event, demonstrating clean, non-bouncing detection. You may need to flip the magnet to find the correct pole that triggers the sensor.
7. Hands-On Lab Recap
You’ve learned:
- The advantages of the Hall Effect sensor over mechanical switches.
- How to wire a solid-state digital magnetic sensor.
- How to use state change detection (lastSensorState) to accurately count events.
- A robust, high-speed method for non-contact counting and proximity sensing.
8. Common Issues & Fixes
| Issue | Cause | Solution |
|---|---|---|
| Sensor is stuck HIGH or LOW regardless of magnet. | Wrong magnetic pole orientation. | Flip the magnet over. Hall sensors are sensitive to North vs. South poles. |
| Counter increments multiple times per pass. | Using the simple proximity method instead of edge detection. | Ensure you implement the sensorState != lastSensorState logic (Section 4) to count only the transition, not the sustained presence. |
| Weak or no detection. | Magnet too weak or too far away. | Use a stronger magnet (neodymium is best) and ensure it passes within a few millimeters of the sensor face. |


