Log #91 · Nov 2, 2026
KY-024 Hall Sensor Module: Using the 4-Pin Digital and Analog Outputs
How the 4-pin KY-024 Hall effect module works, what the A0, G, + and D0 pins do, how to set the threshold potentiometer, and Arduino code that reads both outputs and detects either magnet pole.
The KY-024 is the blue Hall effect module with four pins that comes in almost every 37-in-1 sensor kit. Unlike the bare three-leg sensor, it gives you two outputs at once: an analog voltage that follows the magnetic field and a digital on/off signal with an adjustable threshold. This guide explains what's on the board, how to wire it, how to tune it and how to use both outputs in your code.
If you want the physics of how a Hall sensor works, or you're using a bare A3144 or 49E chip, start with Hall effect sensors explained.
What's on the board
| Part | What it does |
|---|---|
| 49E linear Hall sensor (the small black part on the edge) | Outputs about half the supply voltage with no magnet; the voltage rises for one pole and falls for the other |
| LM393 comparator (8-pin chip) | Compares the sensor voltage with a threshold and switches D0 |
| Blue trimmer potentiometer | Sets that threshold |
| Power LED | On whenever the module has power |
| D0 LED | Shows the state of the digital output |
The four pins are usually labeled:
- A0: analog output, straight from the 49E sensor
- G: ground
- +: supply, 5 V (3.3 V also works with a 3.3 V board)
- D0: digital output from the comparator
Wiring
| KY-024 | Arduino Uno/Nano |
|---|---|
| A0 | A0 |
| G | GND |
| + | 5V |
| D0 | D2 |
No extra resistors are needed: the module already has a pull-up on the comparator output. Some boards print the pin order differently, so read the labels instead of trusting a picture.
Step 1: Read the analog output
Upload this and open Tools → Serial Plotter:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(20);
}
With no magnet nearby you should see a flat line around 500–530 (about 2.5 V, see analogRead explained). Bring one pole of a magnet toward the sensor's face and the line rises toward 900 or more; flip the magnet and it drops toward 100. This is the key difference from the digital-only KY-003 module: the KY-024 tells you which pole is near and roughly how close it is.
Step 2: Set the threshold for D0
The comparator switches D0 when the sensor voltage crosses the voltage set by the trimmer. To tune it:
- Keep magnets away from the module.
- Turn the trimmer slowly until the D0 LED changes state. Then turn it back a little, just until the LED returns to its previous state. The threshold now sits just beyond the idle voltage, on the "no magnet" side.
- Bring the magnet close: the LED should change state. If it doesn't, try the other pole.
Because the comparator only looks at one side of the threshold, D0 reacts to one pole only: the pole that pushes the voltage toward the threshold. The other pole pushes the voltage further away and never triggers it. If your project must react to either pole, use A0 (see Step 4).
Turning the trimmer further from the idle point makes D0 less sensitive, so the magnet has to come closer. That's handy for making a contactless switch that only triggers at a precise distance.
Step 3: Read the digital output
const int D0_PIN = 2;
void setup() {
pinMode(D0_PIN, INPUT);
Serial.begin(9600);
}
void loop() {
int state = digitalRead(D0_PIN);
Serial.println(state);
delay(100);
}
Watch which value appears when the magnet is near. On some KY-024 boards D0 goes LOW when triggered; on others it goes HIGH, depending on how the comparator inputs are wired. Check yours once and write it in a comment. The D0 LED changes whenever D0 changes, but on many boards it lights when D0 is LOW, so use the Serial Monitor to see the actual value.
Step 4: Use both outputs (and detect either pole)
This sketch uses A0 to identify the pole and estimate strength, and D0 for a fast trigger:
const int A_PIN = A0;
const int D_PIN = 2;
const int MARGIN = 60; // how far from idle counts as "magnet"
int idle = 512; // measured at startup
void setup() {
pinMode(D_PIN, INPUT);
Serial.begin(9600);
long sum = 0;
for (int i = 0; i < 50; i++) { // average the no-magnet reading
sum += analogRead(A_PIN);
delay(5);
}
idle = sum / 50;
Serial.print("Idle reading: ");
Serial.println(idle);
}
void loop() {
int raw = analogRead(A_PIN);
int field = raw - idle; // + one pole, - the other
Serial.print("A0: ");
Serial.print(raw);
Serial.print(" field: ");
Serial.print(field);
Serial.print(" D0: ");
Serial.print(digitalRead(D_PIN));
if (field > MARGIN) Serial.println(" -> pole A");
else if (field < -MARGIN) Serial.println(" -> pole B");
else Serial.println(" -> no magnet");
delay(100);
}
Keep magnets away while the board starts, because the first half-second is used to measure the idle value. Increase MARGIN if the reading flickers between states with no magnet nearby.
KY-024 vs other Hall modules
| Module | Pins | Sensor | Output |
|---|---|---|---|
| KY-003 | 3 | A3144 (digital switch) | Digital only, one pole |
| KY-035 | 3 | 49E (linear) | Analog only |
| KY-024 | 4 | 49E + LM393 comparator | Analog and adjustable digital |
Choose the KY-003 for simple counting (wheel turns, door closed), the KY-024 when you want to tune the trigger distance or know which pole is present.
Project ideas
- Contactless button behind a 3D-printed panel: press a magnet-tipped plunger toward the sensor.
- Door or drawer alarm: magnet on the door, module on the frame, buzzer when D0 changes.
- Position sensing: a magnet on a sliding part gives a voltage that changes with distance; calibrate a few points and interpolate.
- Rotation counter or speedometer: one magnet on a wheel, count D0 pulses with an interrupt, as shown in the RPM example of the Hall sensors guide.
Troubleshooting
- A0 stuck near 0 or 1023: check that + and G aren't swapped and that A0 really goes to an analog pin.
- D0 LED always on or always off: the trimmer is far from the idle point. Turn it slowly through its range (these trimmers often need many turns) until the LED changes.
- Only one pole triggers D0: that's normal, as explained in Step 2. Use A0 for both poles.
- Readings drift when the module is near motors: motors and speakers have their own magnets and currents. Keep the sensor a few centimeters away from them or recalibrate
idlein place.
— TheSolderBench · found a mistake? tell me