Hello world! yep yep, i made another simple example project.
This time for the KY-021 Mini reed switch. / Arduino Magnetic Reed Switch.
It will simply turn on and off a light with a relay by using a magnetic field.
Reed switches are used allot in burglar alarms.
Arduino Uno, I used the robotdyn Uno.. one with 7 analog pins! Very good stuff! buy it!
KY-021 Mini reed Switch
KY-019 1 Channel Relay.
A magnetic field, can be any magnet but best to use is a small one. Or even better a magnet from a alarm.
Set of jumper wires. MM/FF/MF
Lamp + power cable (optional) dont need this! The relay itself will show you if it works by turning on a led.
//Author: Danny van den Brande, arduinosensors.nl.
//Hello world! I made a simple example on the KY-021 mini reed switch / Arduino Magnetic 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);
digitalWrite (Relay, LOW);
What is a reed switch?
The reed switch is an electrical switch operated by an applied magnetic field. It was invented at Bell Telephone Laboratories in 1936 by W. B. Ellwood. It consists of a pair of contacts on ferrous metal reeds in a hermetically sealed glass envelope. The contacts may be normally open, closing when a magnetic field is present, or normally closed and opening when a magnetic field is applied. The switch may be actuated by a coil, making a reed relay, or by bringing a magnet near to the switch. Once the magnet is pulled away from the switch, the reed switch will go back to its original position.
An example of a reed switch’s application is to detect the opening of a door, when used as a proximity switch for a burglar alarm.