Reed Switch Tutorial for Arduino, ESP8266 and ESP32

In this article we use the KY-025 and KY-021 reed switch modules with different Arduino, ESP8266 and ESP32 boards to turn on a LED based on a magnet.

I show you how to connect both reed switches with the different microcontroller and how to program the script to read the analog and digital sensor values.

KY-021

Table of Contents

How does a Reed Switch work?

A reed switch is an electrical switches that opens and closes its contacts based on a magnetic field. If a magnetic field is present and strong enough, the connection inside the reed switch closes. Otherwise the switch remains open. The source of that magnetic field can be a magnet or a strong electric current.

The following picture shows the contacts of a reed switch to understand its functionality in detail.

Reed Switch Reeds

The picture shows that the reed switch consists of two magnetizable metal contacts, called reeds. These contacts are relatively thin and wide to make them flexible. Between both contacts there is a small gap when the reed switch is normally open without a magnetic field.

A magnetic field from an electromagnet or permanent magnet cause the reeds to attract to each other and close the circuit. In the next chapter you learn that the direction of that magnetic field has an influence on the reed switch.

When the magnetic field is removed, the contacts return to their initial position and therefore open the contacts of the switch.

A very common application of reed switches is to detect if a door or window is open or closed. Therefore you have one side of the sensor with the reed switch and an other side with the magnet.

Impact of the Direction of the Magnetic Field on the Reed Switch

Because the magnetic field is directed and therefore has a direction, the position and angle between the reed switch and the external magnet is important. There are two opposite scenarios which are also seen the in two following pictures. On the left side the magnet is parallel to the reed switch and in the right picture, the magnet is perpendicular to the magnetic switch.

Magnet is parallel to reed switch

This is the perfect setup between reed switch and magnet because the build up magnetic field is parabolic. You find this setup also in door or window reed switches.

Magnet is perpendicular to reed switch

In this setup the magnetic field has a dead zone in the middle of the reed switch. Therefor do not use the magnet and reed switch in a perpendicular setup.

The following video shows the influence of the direction of the magnetic field on the reed switch.

YouTube

By loading the video, you agree to YouTube's privacy policy.
Learn more

Load video

Reed Switch vs Magnetic Hall Sensor

The reed switch is not the only electrical device with can be used as a magnetic switch. There is also the magnetic hall sensor. For some use cases there is no difference if you use a reed switch or a magnetic hall sensor. But a big difference is how the direction of the magnetic field has to be in order to use the magnetic switch.

In the following table you find the comparison between the two magnetic switches with the advantages and disadvantage and also how the magnet – sensor orientation has to be.

 Magnetic Hall Sensor (KY-024, KY-003)Reed Switch (KY-025, KY-021)
Switch Function• Transducer that varies the output voltage depending on the presents of a magnetic field.• Pair of ferrous metal contacts. If contracts are open, there is no electrical contact. The contacts are closed by a magnet near the switch and opened by removing the magnet.
Magnet – Sensor Orientation• Magnet has to be perpendicular to magnetic hall sensor• Magnet has to be parallel to reed switch
Advantages• No moving parts involved
• No debouncing effect
• Cheaper than magnetic hall sensor
Disadvantages• More expensive than reed switch• Has moving parts that are not able to operate over frequencies greater 10 kHz
• Switch has debouncing effect like all switches

If you are interested in the (magnetic) hall sensors, I wrote a complete tutorial for this as well.

Comparison between KY-025 and KY-021 Reed Switch Module

There are two reed switch modules on the market. The KY-025 and the KY-021. The following table compares the technical data sheet between these two reed modules.

 KY-025KY-021
Operating voltage3.3V...5.5V3.3V...5.0V
Board dimensions1.5 cm x 3.6 cm

0.6 in x 1.4 in
1.5 cm x 1.9 cm

0.6 in x 0.75 in
OutputsAnalog + DigitalDigital
Build in resistor100kΩ potentiometer10kΩ

In the following sections, we want to measure the magnetic field with both, the KY-025 and the KY-021. Therefore we take a look in the wiring between the reed switch and the microcontroller. After the wiring is done we create a sketch to measure the magnetic field. Because the KY-021 has only a digital output, we can not measure how strong the magnetic field is, only if a predefined threshold through the 10kΩ resistor is exceeded.

With the KY-025 we gain much better flexibility, because we are able to measure the strength of the magnetic field with the analog output and are able to define the threshold with the potentiometer for our self.

Pinout of the KY-025 and KY-021 Reed Switch Module

The following picture shows the pinout of the KY-025 and KY-021 reed switch module. You see also the electronic components on the PCB modules. In the next chapter we will concentrate on the schematic with the electronic components, but in this chapter we only discuss the different pinouts.

KY-025 and KY-021 Reed Switch Pinout

The comparison between the KY-025 and KY-021 modules showed that the output between both reed switch modules differ. From the picture above, we see that this difference leads to a different pinout. The KY-025 has, in addition to the two pins for the power supply, one pin for the digital and one pin for the analog output.

The KY-021 on the other hand has only the pins for the power supply and one output pin for the digital output.

Microcontroller Datasheet eBook

The 35 pages Microcontroller Datasheet Playbook contains the most useful information of 14 Arduino, ESP8266 and ESP32 microcontroller boards.

Schematic and Functionality of the KY-025 Reed Switch

After the comparison between the KY-025 and KY-21 module, I want do dive deeper into the schematic of the KY-25 module and its functionality. This part is optional and only for you, if you want to gain deeper knowledge of the KY-025 module.

Schematic of the KY-025 Reed Switch

The following picture shows the schematic of the KY-025 reed switch module with all its electrical components. I will explain every part of the circuit in detail so that you understand the functionality.

KY-025 Reed Switch Schematic

On the far left side you see the LED L1 and the resistor R1 (1kΩ) that close a part of the circuit from the supply voltage to ground. The LED L1 indicates that the reed switch module is active and the resistor R1 prevents the LED from too high voltages.

The next part of the circuit is the potentiometer (100kΩ) in series to resistor R4 (150Ω) and the reed switch itself. This series connection acts like a voltage divider because between the potentiometer and R4, there is a connection to the reference voltage of the LM393 dual comparator (2). The potential on this voltage divider is also the analog output of the KY-025 module.

The second voltage divider is built with resistor R5 (70kΩ) and resistor R3 (100kΩ). The voltage divider is the input voltage of both comparators (3) (5).

The output of the first comparator (1) is connected to the digital output of the reed switch module and to resistor R2 (10kΩ) that disconnects the digital output from the operation voltage. Otherwise the digital output would be equal to the operation voltage independent of the status of the flame sensor. The reference voltage of the second comparator (6) is also the output of the first comparator (1).

The output of the second LM393 comparator (4) closes the circuit of the second LED2 and resistor R6 (1kΩ).

Functionality of the KY-025 Reed Switch

If there is an operation voltage between 3.3V and 5.5V, connected to the KY-025 reed switch module, LED1 turns on and the voltage divider between R5 and R3 creates a stable voltage of V = VCC * R3 / (R3+R5) = VCC * 100/170 = 0.6*VCC.
This voltage is the input voltage of the first (3) and second (5) comparator.

Now we have to consider the two main cases for operation:

  1. There is no magnetic field present and the reed switch is open
    If the switch is open, the analog output is nearly equal to the supply voltage that results in an analog value of 1023. Because the reference voltage VCC of the fist comparator (2) is greater than the input voltage (3) 0.6*VCC, the output of the comparator (1) is equal to 0V. The second comparator switches this input, because now the reference voltage (6) is 0V and the input voltage (5) is also 0.6*VCC. Therefore the output of the second comparator (4) is VCC. Because there is no potential difference between LED L2 and resistor R6, LED L2 is off.
  2. There is a magnetic field that closes the contacts of the reed switch
    If the contacts of the reed switch are closed, the analog output and therefore the reference voltage of the LM-393 (2) are pulled to GND. Because the input voltage of the comparator (3) is greater than the reference voltage, the output (1) is VCC. Now the digital output of the KY-025 reed switch module is HIGH and the second comparator switches the digital signal again, so that the output of the second comparator (4) equals to 0V. Now there is a potential difference across the LED2 and resistor R6 and LED2 turns on.

Because the reed switch is not an ideal switch, you have the potentiometer to tweak the voltage divider to a ratio that fits to your project or use case. The following video shows how I change the resistance of the potentiometer for the voltage divider while the magnet stays in the same position. Dependent on the current resistance of the potentiometer, the digital value of the KY-025 reed switch module changes between HIGH and LOW.

YouTube

By loading the video, you agree to YouTube's privacy policy.
Learn more

Load video

Schematic and Functionality of the KY-021 Reed Switch

After we learned how the KY-025 works, we dive also into the schematic and functionality of the KY-021 reed switch module. Again this section is special knowledge about the KY-021 module and therefore optional for the whole tutorial.

Schematic of the KY-021 Reed Switch

The schematic of the KY-021 reed switch module is very easy and shown in the following picture.

KY-021 Reed Switch Schematic

The digital output of the KY-021 reed switch has only a digital output that is labeled as “S” on the module and is the output of the voltage divider build by a resistor R1 (10kΩ) and the reed switch.

Functionality of the KY-021 Reed Switch

If you have read the chapter about the schematic and the functionality of the KY-025 reed switch, then you already know the functionality of the KY-21 reed switch module.

We have to consider the two cases main cases:

  • There is no magnetic field present and the reed switch is open
    If the switch is open, the digital output is equal to the supply voltage that results in a HIGH signal on the Arduino, ESP8266 or ESP32 microcontroller.
  • There is a magnetic field that closes the contacts of the reed switch
    If the contacts of the reed switch are closed, the digital output is pulled to GND that results in a digital LOW signal on the microcontroller.

The following table gives you an overview of all components and parts that I used for this tutorial. I get commissions for purchases made through links in this table.

ComponentAmazon LinkAliExpress Link
Arduino Nano AmazonAliExpress
Arduino Pro Mini AmazonAliExpress
Arduino Uno AmazonAliExpress
Arduino Mega AmazonAliExpress
ESP32 ESP-WROOM-32AmazonAliExpress
ESP8266 NodeMCU AmazonAliExpress
ESP8266 WeMos D1 Mini AmazonAliExpress
Reed (KY-025) and Mini Reed (KY-021) Switch in Sensor Kit AmazonAliExpress

Read the Analog and Digital Values of the KY-025 Reed Switch

In the following example we want to read the analog and digital value of the KY-025 reed switch.

With the analog connection we are able to get the status of the magnetic switch but can not measure exactly how strong the magnetic field is because the switch is very fast. We want to display the switch cycles to the serial output of the Arduino IDE to see that the analog values do not have any information of the strength of the magnetic field and therefore provide not more information than the digital value.

The KY-025 has a build in potentiometer to set the threshold for the digital value to change between 0 and 1 based on the analog value. The digital value is

  • 0 for the threshold is not exceeded (build in LED is off), because the reed switch is open
  • 1 for the threshold is exceeded (build in LED is on), because the reed switch is closed

The setup of the threshold is very hard because the sampling rate of the serial communication is not very high. Therefore we can use an oscilloscope with a high sampling rate connected to the analog pin to see the current voltage of the analog pin that is changing when the potentiometer is changed.

To stimulate the reed switch I use a magnet with 2 sides (north and south).

Wiring between KY-025 Reed Switch and Microcontroller

The following pictures show the wiring between the KY-025 reed switch and different Arduino, ESP8266 or ESP32 microcontroller boards. For the power supply you can either use the 5V output like I did for the Arduino boards or use the 3.3V output pins, because the EPS8266 and ESP32 only have an operating voltage of 3.3V.

KY-025 Arduino Nano
KY-025 Arduino Nano

For more information about the Arduino Nano, visit the Arduino Nano Tutorial.

KY-025 Arduino Pro Mini
KY-025 Arduino Pro Mini
KY-025 Arduino Uno
KY-025 Arduino Uno

For more information about the Arduino Uno, visit the Arduino Uno Tutorial.

KY-025 Arduino Mega
KY-025 Arduino Mega

For more information about the Arduino Mega, visit the Arduino Mega Tutorial.

KY-025 ESP32 NodeMCU
KY-025 ESP32 NodeMCU
KY-025 ESP8266 NodeMCU
KY-025 ESP8266 NodeMCU
KY-025 ESP8266 WeMos D1 Mini
KY-025 ESP8266 WeMos D1 Mini

Code to read Analog and Digital Values of the KY-025 Module

After we wire the KY-025 and the microcontroller, we can create the Arduino script that displays the analog and digital value of the reed switch. We go step by step over the program code and I explain what we are doing.

// for Arduino microcontroller
int analogPin = A0; 
int digitalPin = 8;

// for ESP8266 microcontroller
//int analogPin = A0; 
//int digitalPin = D8;

// for ESP32 microcontroller
//int analogPin = 4; 
//int digitalPin = 2;

void setup() {
  pinMode(analogPin, INPUT); 
  pinMode(digitalPin, INPUT); 
  Serial.begin(9600);
}

void loop() {
  int analogVal = analogRead(analogPin);
  int digitalVal = digitalRead(digitalPin);
  
  Serial.print(analogVal);
  Serial.print(" - ");
  Serial.println(digitalVal);
  
  delay(100);
}

At the beginning of the script we have to define the pins that connect the microcontroller with the KY-025 module. Because this script can be used for Arduino, ESP8266 and ESP32 microcontroller, if defined all connections but commented the lines for the ESP8266 and ESP32. Therefore if you want to use the script for an ESP microcontroller board, you have to comment the Arduino lines and uncomment the desired ESP lines.

In the setup function, we define the pin mode of the two connection pins as inputs, because we want to read the sensor data of the reed switch. Also we set the baud rate of the serial connection to the PC to 9600 to view the sensor values in the Arduino IDE. The baud rate must match the baud rate of the serial monitor in the Arduino IDE.

In the loop function we read the analog value from the previous defined analog pin and save the analog sensor value as integer variable. We do the same for the digital value but read of cause the digital value from the digital pin.

Now we print both value to the serial monitor and include a short delay of 0.1 seconds before we read the next sensor values in the next iteration of the loop function.

Results reading Analog and Digital KY-025 Module Values

The following videos and picture show the results of this example, measuring the analog and digital values of the KY-025 reed switch.

The following video shows the influence of a magnetic field on the analog and digital measurement of the KY-025 reed switch module. You see how I move the magnet back and forth to influence the reed switch. If the magnetic field is strong enough to close the contacts of the reed switch, LED2 turns on, indicating that the digital output of the KY-025 module switches from LOW to HIGH.

YouTube

By loading the video, you agree to YouTube's privacy policy.
Learn more

Load video

On the left side, the video shows the analog value in the serial plotter of the Arduino IDE and on the right side you see a screenshot of the serial monitor where the digital values are changing analog to LED2.

YouTube

By loading the video, you agree to YouTube's privacy policy.
Learn more

Load video

KY-025 Digital Arduino

Read the Digital Values of the KY-021 Reed Switch

After the KY-025 we also want to test the KY-021 reed switch that has only a digital output and no build in LED. Therefor I use an external LED with a resistor of 220 ohm in series to visualize that the threshold is exceeded. The LED is connected to a digital pin and ground. Therefore we can use this digital pin to turn the LED on and off.

Wiring between KY-021 Reed Switch and Microcontroller

The following pictures show the wiring between the KY-021 and the Arduino, ESP8266 or ESP32 microcontroller boards. For the power supply you can either use the 5V output like I did for the Arduino boards or use the 3.3V output pins, because the EPS8266 and ESP32 only have an operating voltage of 3.3V.

KY-021 Arduino Nano
KY-021 Arduino Nano

For more information about the Arduino Nano, visit the Arduino Nano Tutorial.

KY-021 Arduino Pro Mini
KY-021 Arduino Pro Mini
KY-021 Arduino Uno
KY-021 Arduino Uno

For more information about the Arduino Uno, visit the Arduino Uno Tutorial.

KY-021 Arduino Mega
KY-021 Arduino Mega

For more information about the Arduino Mega, visit the Arduino Mega Tutorial.

KY-021 ESP32 NodeMCU
KY-021 ESP32 NodeMCU
KY-021 ESP8266 NodeMCU
KY-021 ESP8266 NodeMCU
KY-021 ESP8266 WeMos D1 Mini
KY-021 ESP8266 WeMos D1 Mini

Code to read the Digital Values of the KY-021 Module

Most of the program code that we use for the KY-021 reed switch is the same as for the KY-025. We only have to delete the parts for the analog connection and add the code that the external LED is turned on and off depending on the strength of the magnetic field.

The following section shows the Arduino code for the KY-021 that you can use with Arduino, ESP8266 or ESP32 microcontrollers.

// for Arduino microcontroller
int ledPin = 7; 
int digitalPin = 8;

// for ESP8266 microcontroller
//int ledPin = D7; 
//int digitalPin = D8;

// for ESP32 microcontroller
//int ledPin = 0; 
//int digitalPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT); 
  pinMode(digitalPin, INPUT); 
  Serial.begin(9600);
}

void loop() {
  int digitalVal = digitalRead(digitalPin);
  
  if (digitalVal == LOW) {
    digitalWrite (led, HIGH);
  }
  else {
    digitalWrite (led, LOW);
  }
  
  delay(100);
}

In this first part of the program, we replaced the analog pin with the pin where the LED is connected. Again, you have to comment and uncomment the first part of the script so that the script matches your microcontroller.

In the setup function we also replace the analog pin with the led pin but because we want to control the LED with the microcontroller and therefore change the status of the digital I/O pin, the LED pin has to be defined as output and not as input. The rest of the setup function is the same like for the KY-025.

In the loop function, first we read the digital value with the digital read function on the previously defined digital pin. To control the LED we use a if function is the following manner:

  • If the digital value is 0 (=LOW) the reed switch opens due to the magnetic field. Therefore we turn the LED on
  • Otherwise we turn the LED off

At the end of the script we wait for 0.1 seconds and start to read the digital value again in the next iteration of the loop function.

Results reading the Digital Values of the KY-021 Module

The following video shows how the LED turns on when I get in the near of the KY-021 reed switch and turns off, when I move the magnet back.

This indicates that the digital value changes from LOW to HIGH.

YouTube

By loading the video, you agree to YouTube's privacy policy.
Learn more

Load video

Conclusion

In this tutorial we learned how a magnetic switch is used to build a security system for doors or windows for example. Moreover we used two different reed switches and showed in different examples the difference between the analog and digital operation mode.
If you have any further questions about reed switches, use the comment section below..

Also if you are interested in active switches, I also wrote a tutorial for these kind of switches where I also dive into the debouncing problem of switches and how to reduce the debouncing. Here is the link to the article.

Leave a Comment