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
|Pin +||VCC 3.3V - 5V|
// Reed Mini Module KY-021 for Arduino
// 18 January 2020
// Author: email@example.com
// Reed Mini Module KY-021
// KY-021 -> Arduino
// S -> D3
// + (middle) -> VCC 3.3V or 5V
// - (GND) -> GND
// A LED will be off and turn on LED_BUILTIN when magentic field is introduced, simulating an alert light when a door is opened.
// 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
pinMode (reedModule, INPUT);
pinMode (LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, digitalRead(reedModule)); // when the magnet is removed , turn LED on