Project26.Reed Switch

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.

Specification

  • Operating Voltage 3.3V ~ 5V

  • Output Type Digital

  • Board Size 18.5mm x 15mm

Connect

_images/26_bb.png

Code

int Led = 13;      //define LED interface
int buttonpin = 3; //define magnetic ring sensor interface
int val;           //define digital variable val
void setup()
{
    pinMode(Led, OUTPUT);      //define LED as output interface
    pinMode(buttonpin, INPUT); //define magnetic ring sensor as output interface
}
void loop()
{
    val = digitalRead(buttonpin); // read and assign the value of digital interface 3 to val
    if (val == LOW)
    {
    digitalWrite(Led, HIGH);
    }
    else
    {
    digitalWrite(Led, LOW);
    }
}

Phenomenon

_images/26.jpg