Mini Reed Switch Module KY-021 on Arduino

What we want to achieve

Using an Arduino - we are using a Nano V3.0 compatible board - we want to turn on the on-board LED (LED_BUILTIN) on when no magnetic field is present, simulating the way security switches on doors and windows work:

Attached to the door or window frame is a Reed Switch, on the movable part - the door or window - is magnet.

In closed position it will be near enough to the reed to close the circuit.
When the door - or window - is opened, the reed will return to open position and the Arduino will trigger the LED - or any other operation - to alert of the door being opened.

Of course we can use only the Reed Switch, however the module makes it a bit easier.

Instead of triggering the LED_BUILTIN, you can trigger an external LED or other output devices, send messages, etc.


Triggering a LED with the reed switch can be accomplished without the Arduino and we might give a tip on this later.


  • Arduino (compatible) development board - we used the Arduino Nano compatible board (see here)
  • Our Mini Reed Module KY-021 (see here)
  • Arduino IDE (download here)


    • This tip is focused on using the Arduino IDE

    How to


       KY-021 Arduino
      Pin S D3
      Pin + VCC 3.3V - 5V
      Pin - GND


      Mini Reed Switch Module connected to Arduino


      // Reed Mini Module KY-021 for Arduino
      // 18 January 2020
      // Author:
      // 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

      Leave a Reply

      All blog comments are checked prior to publishing