SOTM
Published © CC BY-NC

Mini Reed Switch Module KY-021 on Arduino

Ever wondered on how security switches to detect doors and windows opening or closing work? Here is a simulation.

BeginnerProtip30 minutes1,938
Mini Reed Switch Module KY-021 on Arduino

Things used in this project

Hardware components

Arduino Nano V3.0 Compatible board
×1
Solderless Breadboard
×1
Jumper Wire Kit
×1
Mini Reed Switch Module KY-021
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Schematic for Mini Reed Switch Module KY-021 on Arduino

Code

Arduino code for Mini Reed Switch Module KY-021

Arduino
//
// Reed Mini Module KY-021 for Arduino
// 18 January 2020 
// Author: dante@serveronthemove.com.au
//
// Hardware:
//   Arduino
//   Reed Mini Module KY-021
//
//   Wiring:
//     KY-021      -> Arduino
//      S          -> D3
//      + (middle) -> VCC 3.3V or 5V
//      - (GND)     -> GND
//
// Purpose:
//    A LED will be off and turn on LED_BUILTIN when magentic field is introduced, simulating an alert light when a door is opened.
//
// Note:
//    If you find the LED behaviour reversed - start turned on, and turn off when magnetic field is introduced -
//      you can reverse + and -


int reedModule = 3;    // S Pin of Reed Module KY-021 on Arduino D3

void setup() 
{
  pinMode (reedModule,  INPUT);
  pinMode (LED_BUILTIN, OUTPUT);
}

void loop()
{
  digitalWrite(LED_BUILTIN, digitalRead(reedModule)); // when the magnet is removed , turn LED on
}

Credits

SOTM

SOTM

7 projects • 4 followers

Comments