KY-021 Mini Magnetic Reed Switch Module
The KY-021 is a mini magnetic reed switch module that detects the presence of a magnetic field. It provides a digital signal output, making it suitable for applications like security systems, position sensors, and magnetic field detection projects.

On this page
KY-021 pinout
The KY-021 is a 3-pin mini magnetic reed switch sensor module:
| Pin | Type | Description | Notes |
|---|---|---|---|
| Pin (-) | Power | Ground connection | |
| Pin (middle) | Power | Power supply | 3.3V to 5V |
| Pin (S) | Communication | Digital signal output | Changes state when magnet detected |
Interface: Digital output (magnetic field detection)
Sensor: Reed switch (magnetically-activated switch)
Operation: Reed contacts close when magnet is nearby
Power: 3.3V to 5V operation
Detection: Detects presence of magnetic field
Applications: Door/window sensors, position detection, security systems, proximity sensing
Wiring the KY-021 to ESP32
To interface the KY-021 with an ESP32 for magnetic field detection:
| KY-021 pin | ESP32 pin | Purpose |
|---|---|---|
| Pin (-) | GND | Ground |
| Pin (middle) | 3.3V | Power supply |
| Pin (S) | GPIO4 | Digital input (any GPIO) |
GPIO Selection: Any digital GPIO pin works, GPIO4 is just an example
Voltage: Use 3.3V for ESP32 compatibility
Magnet: Use a permanent magnet (neodymium works well)
Distance: Detection range typically 10-20mm depending on magnet strength
Door Sensor: Popular for door/window open/closed detection
KY-021 code examples
KY-021 Arduino example
Copyint reed_magnet = 4; // KY-021 signal pin (GPIO4, matches the wiring above)
int value;
void setup() {
pinMode(reed_magnet, INPUT_PULLUP); // Reed contact closes to GND near a magnet
Serial.begin(115200);
Serial.println("KY-021 Magnetic field detection");
}
void loop() {
value = digitalRead(reed_magnet);
if (value == LOW) {
Serial.println("Magnetic field detected");
delay(100); // 100 ms break
}
}The sensor's signal pin connects to GPIO4 (matching the wiring above) and is read as a digital input with the internal pull-up enabled. On the ESP32, pull-ups are enabled with INPUT_PULLUP - the old AVR trick of writing HIGH to an input pin does nothing here. The reed contact closes to GND near a magnet, which reads LOW.
KY-021 ESP-IDF example
Copy#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#define REED_SWITCH_PIN GPIO_NUM_4
void app_main(void) {
gpio_config_t io_conf = {
.pin_bit_mask = (1ULL << REED_SWITCH_PIN),
.mode = GPIO_MODE_INPUT,
.pull_up_en = GPIO_PULLUP_ENABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE
};
gpio_config(&io_conf);
printf("KY-021 Magnetic Field Detection Test\n");
while (1) {
int level = gpio_get_level(REED_SWITCH_PIN);
if (level == 0) {
printf("Magnetic field detected\n");
vTaskDelay(pdMS_TO_TICKS(100)); // 100 ms delay
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}This ESP-IDF code configures GPIO4 as an input with an internal pull-up resistor for the KY-021 reed switch. It continuously checks the pin state and prints a message when a magnetic field is detected.
KY-021 ESPHome example
Copybinary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
name: "KY-021 Magnetic Reed Switch"
filters:
- delayed_on: 50ms
- delayed_off: 50ms
on_press:
- then:
- lambda: |-
ESP_LOGD("sensor", "Magnetic field detected!");This ESPHome configuration sets up the KY-021 reed switch as a binary sensor on GPIO4. The internal pull-up resistor is enabled, and debounce filters are applied to prevent false triggers. When the sensor detects a magnetic field, a log message is generated.
KY-021 PlatformIO example
Copy[env:esp32]
platform = espressif32
board = esp32dev
framework = arduino#include <Arduino.h>
#define REED_SWITCH_PIN 4
void setup() {
pinMode(REED_SWITCH_PIN, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("KY-021 Magnetic Field Detection Test");
}
void loop() {
if (digitalRead(REED_SWITCH_PIN) == LOW) {
Serial.println("Magnetic field detected");
delay(100);
}
delay(100);
}This PlatformIO code configures GPIO4 as an input with a pull-up resistor for the KY-021 reed switch. It detects magnetic fields and prints a message when a field is detected.
KY-021 MicroPython example
Copyimport machine
import time
REED_SWITCH_PIN = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
while True:
if REED_SWITCH_PIN.value() == 0:
print("Magnetic field detected")
time.sleep(0.1)
time.sleep(0.1)This MicroPython script configures GPIO4 as an input with an internal pull-up resistor for the KY-021 reed switch. It continuously checks the sensor state and prints a message when a magnetic field is detected.
KY-021 specifications
About the KY-021
The KY-021 is about as bare as a reed switch gets: two ferromagnetic contact tongues sealed in glass that snap together when a magnet gets close, broken out to a 3-pin header with nothing else on the board - no comparator, no potentiometer, no signal conditioning. Joy-IT’s own documentation describes exactly that mechanism, and the module reads on a GPIO the same way any other mechanical switch does: closed to GND near a magnet, open otherwise.
That bare-bones design is also what separates it from the similarly-named KY-025, which wraps a larger reed switch in the same kind of LM393-comparator-plus-potentiometer board used across several other KY modules and adds a genuinely adjustable trigger threshold and a second output pin. The KY-021 has no threshold to tune - it is either open or closed - so wire it straight to a GPIO with INPUT_PULLUP, debounce the transition in software, and expect nothing more than presence or absence of a nearby magnet.
KY-021 troubleshooting
No Response from Sensor
›
Issue: The sensor does not output any signal when a magnet is nearby.
Solutions:
- Verify that all connections are secure and correctly placed.
- Ensure the module is receiving the appropriate voltage (3.3V to 5V).
- Check if the microcontroller's digital input pin is correctly configured.
- Test the reed switch with a multimeter to confirm it closes when exposed to a magnetic field.
False Triggering
›
Issue: The sensor outputs a signal without a magnet nearby.
Solutions:
- Ensure stable mounting to prevent unintended movements.
- Check for loose connections or interference from nearby electronic components.
- Implement software debouncing to filter out spurious signals.
- Verify that there are no stray magnetic fields in the environment causing false triggers.
Where to buy the KY-021

Resources
Similar sensors





