This tutorial will demonstrate how a magnetic reed switch works and how to use it in a very simple project using an Arduino Mega 2560.
How often do we walk into a store and hear a bell ringing when we open the door? In the old days, shopkeepers’ bells (Figure 1) were very common in any local store which, inevitably, are being replaced by new digital technologies. One way to detect if a door is opened is to use a magnetic reed switch. In this tutorial, the goal is to simply detect the entrance of a customer with a KY-025 reed switch, which will warn the shopkeeper using two LEDs and a buzzer. This sensor can also be used in other applications, such as in alarm systems.
Parts Required
- Arduino Mega 2560;
- USB 2.0 Cable Type A/B;
- KY-025 or KY-021 Mini Magnetic Reed Switch Module;
- 1 Green LED;
- 1 Red LED;
- 2 220Ω Resistors;
- 1 Active Buzzer;
- Male to male jumper wires;
- Male to Female jumper wires;
- 1 Breadboard;
Note: Since only 4 digital pins are required for you to follow this tutorial, an Arduino Nano, ESP32 NodeMCU board or any similar board can be used instead. Just be sure to change the pins in the code if necessary.
The Magnetic Reed Switch
As seen in Figure 2, the KY-025 reed switch module used in this tutorial has 4 pins:
- A0 – analogue pin;
- G – ground pin;
- + – voltage pin (5 Volts);
- D0 – digital pin.
The working principle of this sensor is very simple. If a magnetic field (B) is detected in the vicinity of the reed switch, the two metal plates that compose it (normally separated but very close to each other) will touch, closing the electric circuit.
The voltage level from the reed switch is amplified by an amplifier and sent to the analogue output of the module. Then, an LM393 comparator is used to convert the analogue signal to a digital one. The sensitivity of this conversion can be controlled by adjusting the potentiometer [1].
Circuit
We will start by connecting the cathode of each LED to ground, followed by the 220Ω resistors to the anode of the corresponding LEDs. Then, the remaining terminals of the resistors are connected to the digital pins of Arduino Mega (pin 2 for the red LED and pin 4 for the green LED).
The active buzzer follows. Connect its positive terminal to Arduino Mega’s digital pin 6 and the negative terminal to ground.
As for the KY-025 reed switch module, start by connecting the “G” pin to ground, followed by the “+” pin to Vcc (5 Volts) and finally the “D0” pin to pin 7 of the Arduino Mega.
In the end, your circuit should be set as shown in Figure 4.
Code
Just like a storekeeper’s bell, the bell rings when a client opens the door. In this example, both the green LED and the buzzer are activated whenever the door (represented as a magnet in the video) is opened, meaning that the magnet is far from the reed switch and so its metal plates are separated. The “Opened” variable ensures that the buzz is only activated once while a client is entering the store. Once the door is shut again, meaning that the magnet is near the reed switch, the red LED is activated and the “Opened” variable is reset.
int closeLED = 2; int openLED = 4; int buzzer = 6; int reedSwitch = 7; bool Opened; void setup() { pinMode(closeLED, OUTPUT); pinMode(openLED, OUTPUT); pinMode(buzzer, OUTPUT); pinMode(reedSwitch, INPUT); Opened = 0; } void loop(){ if (digitalRead(reedSwitch)==HIGH){ digitalWrite(openLED, LOW); digitalWrite(closeLED, HIGH); digitalWrite(buzzer, LOW); Opened = 0; } else { digitalWrite(openLED, HIGH); digitalWrite(closeLED, LOW); if (Opened == 0){ digitalWrite(buzzer, HIGH); delay(100); digitalWrite(buzzer, LOW); delay(100); digitalWrite(buzzer, HIGH); delay(500); digitalWrite(buzzer, LOW); Opened = 1; } } }
That’s it! If you enjoyed this tutorial, you can visit our YouTube channel and watch this and many other tutorials. Thanks for following us and be sure to rate, comment and share our content.
References
[1] KY-025 Reed module datasheet
Towards the Future !!!
😉