What is Reed Switch??
Reed switch consists of two Ferro magnetic blades separated by close proximity. Usually, contacts are typically nickel-iron alloy as they don't stay magnetized for a long time. These contacts are coated with Rhodium as it increases the durability of a switch. These leads are placed inside a glass tube which is filled with inert gas typically Nitrogen. The leads are protected against atmospheric corrosion as they are inside a glass tube.
When the magnet is brought closer to this lead then an electro-mechanical force field is generated and become polarized thus leading contacts to get attracted to each other. The two lead contact touches each other and allows electricity to flow through them. When the magnet is removed the reed switch returns to its normal state (separate from each other) & no current flow through it. Reed switch acts as an open circuit thus no current flows through it. Reed switch comes with two types of contact i.e. (NO) normally open and (NC) normally closed. In this tutorial, we are using a normally open type of reed switch.
How does Reed Switch Works??
For example, we take a circuit in which the reed switch is connected in series with the LED. Here Reed switch acts as a switch, normally when there is no magnet near the reed switch it will act as a normally open. Thus there will be no flow of current through it & LED will be OFF.
But when we introduce a magnet close to the reed switch then two contacts will touch each other and make the electrical contact which makes the connection close. Thus current will pass through it and the LED will turn ON i.e. glow. These reed switches are available at a very low cost. This type of reed switch is not designed to carry a high amount of voltage or power. The reed switch we are using carries a maximum of 1.2A of current.
Application -
You will find the application of Reed switches in
- Burglar alarms
- Security systems
- The door & window have magnetic bases to identify when it is closed or open.
- Machine doors (machine only runs when doors are closed).
Ky-021 mini reed switch
In the ky-021 reed switch, we only have a digital pin that gives output in 1 & 0 when there is a magnetic field present or not. Ky-021 has no built-in LED so we will use an external LED to determine the magnetic field.
Ky-025 reed switch
Ky-021 consists of both analog and digital output along with a built-in LED & built-in potentiometer to set the threshold. it also has a comparator LM393. By rotating the potentiometer, we can increase sensitivity or threshold value. If the value from the sensor is less than the threshold value then it will read 0 (LOW). If the value from the sensor is more than the threshold then it will read as 1(HIGH).
Difference between KY-025 and KY-021 Reed Switch Module
Both
the KY-025 and the KY-021 are used to measure the magnetic field. But KY-021
has only a digital output, so we cannot measure how strong or cannot measure the magnitude of the magnetic field present near the sensor. On another hand, we
have KY-025 which has analog output to
measure the strength of the magnet.
Parameters | KY-021 | KY-025 |
---|---|---|
Operating voltage | 3.3V to 5V | 3.3V to 5.5V |
Outputs | only Digital | Both Analog & Digital |
Magnetic field strength | NO | detects |
Build in resistor | 10kΩ | 100kΩ potentiometer |
Parts Required
- KY-025 & KY-021 (magnetic Reed switch module)
- Arduino
- Jumper wires
- LED
- Breadboard
Circuit Diagram
Connect the ground pin of the ky-021 sensor to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to 9. Also, connect the positive lead of LED to Arduino pin 11.
int Led_pin = 2;
int sensor_value;
void setup() {
Serial.begin(115200);
pinMode(Led_pin,OUTPUT);
pinMode(reed_switch,INPUT);
}
void loop() {
sensor_value = digitalRead(reed_switch);
Serial.println(sensor_value);
if(sensor_value==HIGH){
digitalWrite(Led_pin,HIGH);
}
else{
digitalWrite(Led_pin,LOW);
}
}
In the void loop, we continuously read values from the Reed switch and store values in the variable name sensor values. if we place the magnet close to the reed switch then the leads get connected and make electrical contact thus output we get is HIGH. here we used LED which gets turned ON when output is HIGH & turns OFF when output is LOW (open circuit).
int digital_reed_pin = 11;
int analog_reed_pin = A0;
int Led_pin = 2;
int digital_value;
int analog_value;
void setup()
{
Serial.begin(115200);
pinMode(Led_pin,OUTPUT);
pinMode(digital_reed_pin,INPUT);
pinMode(analog_reed_pin,INPUT);
}
void loop()
{
digital_value = digitalRead(digital_reed_pin);
analog_value = analogRead(analog_reed_pin);
Serial.print("Digital_Output :");
Serial.println(digital_value);
Serial.print("Analog_Output :");
Serial.println(analog_value);
if(digital_value == HIGH){
digitalWrite(Led_pin,HIGH);
}
else{
digitalWrite(Led_pin,LOW);
}
delay(300);
}
Now we used the serial monitor to observe the output. To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M). Here you can see in the Serial monitor when there is no magnet near the reed switch then the digital output will be 0 and the analog output will be 1023. But when we place a magnet near the reed switch then the digital output will be 1 and the analog output will be 6. We can adjust the sensitivity by rotating the potentiometer in the clockwise or anti-clockwise direction.
Conclusion -
Today we learn about what is a reed switch, how to use a reed switch with Arduino, the types of reed switches, the application of the reed switch & working principle of the reed switch.
"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."