KY-021 MINI MAGNETIC REED SWITCH MODULE

Click Here to View Step by Step

KY-021 MINI MAGNETIC REED SWITCH MODULE


Short description :

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.

Working with  (Compatible)

Arduino, ESP32, Nodemcu, ESP8266, Raspberry Pi, and ......

KY-021 MINI MAGNETIC REED SWITCH MODULE

KY-021 MINI MAGNETIC REED SWITCH MODULE

ARDUINO IDE CODE 

Example description :

the module detects a magnetic field. Place a magnet near the module to activate the reed switch on/off led

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