KY-025 Reed switch & KY-021 mini magnetic Reed switch

How to connect the reed switch module with Arduino, how the reed switch works & what working principle of the reed switch are some questions that may arise in your mind while using a reed switch with Arduino.

Hello guys! Today we are going to see about KY 021 Reed switch and KY 025 reed switch so let's begin.

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 NitrogenThe leads are protected against atmospheric corrosion as they are inside a glass tube.

reed switch sensor

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??

reed sensor

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.

normally open magnetic switch

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). 

magnetic reed sensor

Ky-021 mini reed switch

magnetic switch sensor

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

reed switch Arduino

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.

Code -
int reed_switch = 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);
    }
}

First, initialize pins of Reed switch & LED. We have declared a variable name sensor value to read values from the Reed switch. In void setup, we configure pins as INPUT or OUTPUT. We want to read values from the sensor pin so we will declare it as INPUT and LED as OUTPUT. We also configure the baud rate to 115200.

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). 

Arduino reed sensor


Code - 

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);
}


OUTPUT
reed switch sensor Arduino

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."

Close Menu