The KY-003 Hall magnetic sensor module: a black three-pin board with the sensor bent out over one edge

KY-003 Hall magnetic sensor module: how it works, wiring and code

Updated

The KY-003 notices magnets. Bring one near the small black sensor on its edge and the signal pin flips; take it away and it flips back. Nothing touches, nothing moves, and nothing wears out.

It's the module behind most 'is the door shut' and 'how fast is this wheel turning' projects. It's also the first of three magnet sensors in the kit, and knowing which of the three to reach for saves a lot of head scratching.

How a Hall sensor works

Send a current along a thin strip of semiconductor and hold a magnet near it, and the field pushes the moving charges toward one side of the strip. That leaves a tiny voltage across it, named after Edwin Hall, who discovered the effect in 1879. The sensor on the KY-003 measures that voltage, and a circuit inside the same three-legged package turns it into a clean switch: field strong enough, output on; field gone, output off.

Because the decision is made inside the sensor, the board around it is simple: the sensor, a resistor, a small LED and three pins (S for signal, + in the middle, − for ground). There's no knob. The KY-003 decides for itself how strong a field counts, which in practice means bringing the magnet close. In the course walkthrough it's identified by the factory number HW-477 printed on the board, which helps when you're hunting for it in the case.

Wiring

Module pinHERO Board pinWhat it carries
S10The signal: LOW when a magnet is near
+ (middle)5VPower for the sensor
−GNDGround

Pin 10 matches the course sketch.

The code, and the one word that matters

The course sketch lights the board's L LED while a magnet is near. It's short, but one word in it does a lot of work.

const uint8_t KY_003_PIN = 10;  // the module's S pin

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(KY_003_PIN, INPUT_PULLUP);
}

void loop() {
  if (digitalRead(KY_003_PIN) == LOW) {  // LOW = magnet detected
    digitalWrite(LED_BUILTIN, HIGH);
  } else
    digitalWrite(LED_BUILTIN, LOW);
  delay(100);
}
KY-003 Hall magnetic sensor, from the SensorKit course (comments shortened)

That word is INPUT_PULLUP. It switches on a resistor inside the HERO Board's chip that gently holds the pin HIGH. The sensor pulls the line LOW when it sees a field and lets go when the field is gone, and the pull-up brings it back to HIGH. Without a pull-up somewhere, a released line floats and reads random noise. So the logic runs the opposite way to what you might expect: LOW means magnet, HIGH means no magnet.

You can watch it happen. Add Serial.begin(9600); to setup() and Serial.println(digitalRead(KY_003_PIN)); to the loop, then open the Serial Monitor at 9600 baud. On the bench in the walkthrough it printed 1 with nothing nearby and 0 the moment an ordinary fridge magnet came close. A bare hand changed nothing.

Getting a reliable trigger

Hall sensor, linear Hall or reed switch?

KY-003 HallKY-024 Linear HallKY-025 Reed switch
Senses withA Hall switchA linear Hall sensorTwo metal reeds in a glass tube
OutputsDigital onlyAnalog and digitalAnalog and digital
KnobNoYes, sets the digital trip pointYes, sets the digital trip point
Tells poles apartUsually answers one pole onlyYes: one pole raises the reading, the other lowers itNo: either pole closes it
Moving partsNoneNoneYes, the reeds flex

The three magnet modules in the 37-in-1 kit.

Pick the KY-003 when you only need yes or no, with nothing to tune and nothing to wear out. Pick the KY-024 linear Hall when distance or polarity matters: in its walkthrough the reading sat near 520, rose toward 700 for one direction of field and fell toward 300 for the other. Pick the KY-025 reed switch when you want a contact that any magnet closes, either way round.

What to build with it

Common problems

Watch it on the bench

The KY-003 walkthrough in the free SensorKit course wires it to a HERO Board and shows the 1 and 0 in the Serial Monitor, in under three minutes. The digital I/O article covers INPUT versus INPUT_PULLUP in more depth, and the 37-in-1 sensor kit guide lists every other module in the case.

37-in-1 Sensor Kit: All three magnet modules (KY-003, KY-024 and KY-025) and 34 more, with the free SensorKit course's filmed walkthroughs.

HERO Board: The Uno-compatible board the walkthroughs are filmed on, with its USB cable.

Questions

Is the KY-003 analog or digital?

Digital. It switches between HIGH and LOW. For a reading of how strong the field is, use the KY-024 linear Hall module, which has an analog output.

Why does the KY-003 read LOW when a magnet is near?

The sensor pulls its output to ground when it detects a field, and a pull-up resistor holds it HIGH the rest of the time. So LOW means magnet. That's why the course code uses INPUT_PULLUP and checks for LOW.

Does the KY-003 detect both poles of a magnet?

Hall switches like the one on this module usually respond to one pole. If nothing happens, turn the magnet over. If you need both poles, use the KY-024 or the KY-025.

What magnet do I need for the KY-003?

Any small permanent magnet. The course walkthrough uses an ordinary fridge magnet.

Keep going