Danny van den Brande
Published © CC BY-SA

Arduino - Magnetic Reed Switch KY-021

Hello world! Yep yep, I made another simple project. This time for the KY-021 Mini reed switch. It will simply turn on and off a lamp.

BeginnerShowcase (no instructions)1 hour9,064
Arduino - Magnetic Reed Switch KY-021

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
mini Reed Switch
×1
Channel Relay
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Magnet
×1
Lamp + Power cable
×1

Story

Read more

Schematics

KY-021 Magnetic REED Switch schematic_bb.png

Code

KY-021_Mini_Reed_SWITCH.ino

Arduino
//Author: Danny van den Brande, arduinosensors.nl.
//Hello world! I made a simple example on the KY-021 mini reed switch.
//Do what ever you like with this code! This code will turn on a relay.
//In my video i used a lamp, but you can add any device to the relay if you want.
//reed switches can be used for a dozen of things but is mostly known for its use
//in burglar alarms, and work with a magnetic field.
int Relay = 9 ;
int MiniReed = 8; 

int val ;
void setup ()
{
  pinMode (Relay, OUTPUT);
  pinMode (MiniReed, INPUT);
}
void loop ()
{
  val = digitalRead (MiniReed) ;
  if (val == HIGH) 
  {
    digitalWrite (Relay, HIGH);
  }
  else
  {
    digitalWrite (Relay, LOW);
  }
}

Credits

Danny van den Brande

Danny van den Brande

36 projects • 108 followers
"Hello world."

Comments