The KY-021 Magnetic Reed Switch module is a switch that is normally open and gets closed when exposed to a magnetic field, sending a digital signal. Commonly used in mechanical systems as proximity sensors.
Compatible with Arduino, Raspberry Pi, ESP32 and other microcontrollers. For analog output see the KY-025 reed switch.


Ky-021 Specifications
This module consist of a small reed switch actuated by a magnetic field, a 10kΩ resistor, and 3 male header pins.
Operating Voltage | 3.3V ~ 5V |
Output Type | Digital |
Board Size | 18.5mm x 15mm [0.728in x 0.591in] |
Connection Diagram
Connect the module’s Power line (middle) and ground (-) to +5 and GND respectively. Connect signal (S) to pin 2 on the Arduino.
KY-021 | Arduino |
---|---|
S | Pin 2 |
middle | +5V |
– | GND |

KY-021 Arduino Code
The following sketch will turn on the pin 13 LED on the Arduino when the module detects a magnetic field. Place a magnet near the module to activate the reed switch.
int led = 13; // LED pin
int reelSwitch = 2; // magnetic senso rpin
int switchState; // variable to store reel switch value
void setup()
{
pinMode (led, OUTPUT);
pinMode (reelSwitch, INPUT);
}
void loop()
{
switchState = digitalRead(reelSwitch); // read the value of digital interface 2 and assign it to switchState
if (switchState == HIGH) // when the magnetic sensor detect a signal, LED is flashing
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
}
nee
hi, how to contact you
hi, she don’t love u :’v
–
AMA SIM VC QUE E ILUDIDA GAROTA