KY-021 Mini Magnetic Reed Switch Module

View on Amazon
Overview
About KY-021 Mini Magnetic Reed Switch Module
The KY-021 Mini Magnetic Reed Switch Module is designed to detect the presence of a magnetic field. It contains a reed switch that closes its contacts when exposed to a magnetic field, allowing current to flow through. This module operates at voltages between 3.3V and 5V, making it compatible with various microcontrollers like Arduino and ESP32. Itβs commonly used in applications such as door and window sensors, position detection, and other projects requiring magnetic field detection.
Get Your KY-021
π‘ Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
KY-021 Specifications
Complete technical specification details for KY-021 Mini Magnetic Reed Switch Module
π Technical Parameters
KY-021 Pinout
The **KY-021** is a 3-pin mini magnetic reed switch sensor module:
Visual Pinout Diagram

Pin Types
Quick Tips
**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
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 Pin (-) | Power | Ground connection | |
2 Pin (middle) | Power | Power supply | 3.3V to 5V |
3 Pin (S) | Communication | Digital signal output | Changes state when magnet detected |
Wiring KY-021 to ESP32
To interface the **KY-021** with an **ESP32** for magnetic field detection:
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| KY-021 Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 Pin (-) Required | GND | Ground | |
2 Pin (middle) Required | 3.3V | Power supply | |
3 Pin (S) Required | 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 Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
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.
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.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
KY-021 Programming Examples
Ready-to-use code examples for different platforms and frameworks
int reed_magnet = 10; // Declaration of the sensor input pin
int value; // Temporary variable
void setup() {
pinMode(reed_magnet, INPUT); // Initialization sensor pin
Serial.begin(9600); // Initialization of the serial monitor
Serial.println("KY-021 Magnetic field detection");
}
void loop() {
// The current signal at the sensor is read out
value = digitalRead(reed_magnet);
// If a signal could be detected, this is displayed on the serial monitor.
if (value == LOW) {
Serial.println("Magnetic field detected");
delay(100); // 100 ms break
}
}This Arduino code sets up the KY-021 reed switch on digital pin 10. It reads the digital value corresponding to the presence of a magnetic field and prints a message to the serial monitor when a magnetic field is detected.
#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.
binary_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.
platformio.ini
[env:esp32]
platform = espressif32
board = esp32dev
framework = arduinomain.cpp
#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.
import 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.
Wrapping Up KY-021
The ESP32 KY-021 Mini Magnetic Reed Switch Module is a powerful KY-0xx module sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the KY-021 into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the KY-021? Check out these similar sensors that might fit your project needs.

KY-034 Automatic Flashing Color LED Module
The KY-034 is an automatic flashing color LED module that emits a sequence of seven colors when powered. It's suitable for decorative and...

KY-009 RGB Full Color LED SMD Module
The KY-009 is an RGB LED module that allows for the creation of various colors by adjusting the brightness of its red, green, and blue LEDs...

KY-035 Analog Hall Magnetic Sensor Module
The KY-035 is an analog Hall magnetic sensor module based on the AH49E linear Hall-effect sensor. It provides an analog output voltage...





