KY-021 Mini Magnetic Reed Switch Module

Description

Arduino magnetic reed switch module Keyes KY-021. A reed switch is a magnetic sensor that is normally open and gets closed when exposed to a magnetic field.

Specifications

The KY-021 Mini Magnetic Reed Switch Module consists of a 10kΩ resistor and a small reed switch actuated by a magnetic field, commonly used in mechanical systems as proximity sensors. Compatible with popular electronic platforms like Arduino, Teensy, Raspberry Pi and ESP8266.

Operating Voltage3.3V to 5v
Output TypeDigital

KY-021 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
S2
middle+5V
-GND
KY-021 Arduino Connection diagramclick to enlarge

KY-021 Arduino Example 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 KY-021 to activate the reel 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);
  }
}

Leave a Reply

Be the First to Comment!

avatar