KY-021 Mini Magnetic Reed Switch Module

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 magnetic reed switch Fritzing part image
Arduino KY-021 Mini Magnetic Reed Switch Module

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 Voltage3.3V ~ 5V
Output TypeDigital
Board Size18.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-021Arduino
SPin 2
middle+5V
GND
KY-021 Arduino Connection diagram

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);
  }
}

Downloads

Leave a Comment


5 thoughts on “KY-021 Mini Magnetic Reed Switch Module”