Reed Switch Tutorial for Arduino, ESP8266 and ESP32

Reed Switch Tutorial for Arduino, ESP8266 and ESP32

In this tutorial we learn how to build our own security system for doors or windows with a reed switch.

Reed switches are magnetically-actuated electrical switches which switch based on a magnetic field. If magnetic field through a magnet or a strong electrical current is present to the reed switch, the connection inside the switch closes and the circuit is closed so that a current flow is present.

Otherwise if there is no magnetic field, the switch remains open. 

KY-021

Table of Contents

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-003)Reed Switch (KY-025, KY-021)
Switch FunctionTransducer 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 OrientationMagnet has to be perpendicular to magnetic hall sensorMagnet 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.

Use of the Reed 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 like you can see in right picture.

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 that you also find in the door or window detection use case because the build up magnetic field is parabolic.

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.

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

The following table gives you an overview of all components and parts that I used for this tutorial. If you want to support my work, you can buy something from the following links and I will earn a small commission. This does not affect the price you pay for the products.

If you are interested in components and parts that I used in other tutorials, visit the components and parts page.

 Arduino UnoAmazonBanggoodAliExpress
ORESP8266 NodeMCUAmazonBanggoodAliExpress
ORESP32 NodeMCUAmazonBanggoodAliExpress
ANDReed Switch KY-025 and KY-021 in Sensor PackAmazonBanggoodAliExpress

KY-025 Analog Connection

The following pictures show the wiring between the KY-025 and the Arduino and NodeMCU for the analog connection.

With the analog connection we are able to see the magnetic switch but can not measure exactly how strong the magnetic field is because the switch is very fast. Therefore we want to display the switch cycles to the serial output of the Arduino IDE. The sketch is pretty straight forward. The microcontroller reads the analog value of the reed switch and prints it to the serial output.
To stimulate the reed switch I use a magnet with 2 sides (north and south).

int analogPin = A0; // KY-025 analog interface
int analogVal;      // analog readings

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

void loop ()
{
  // Read the analog interface
  analogVal = analogRead(analogPin);
  Serial.println(analogVal); // print analog value
  delay(100);
}
Reed Switch - KY-025 Analog Measurement
KY 025 analog connection

KY-025 Digital Connection

Next we connect the microcontroller with the digital output of the reed switch KY-025. The following pictures show the wiring.

In a digital setup I usually use a LED to display the exceeded threshold. But the KY-025 has a build in LED with the same functionality. Therefor we do not need an extra LED. The following program code reads the digital value of the module and prints

  • 0 for the threshold is not exceeded (build in LED is off)
  • 1 for the threshold is exceeded (build in LED is turned on)

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

int digitalPin = 8; // KY-025 digital interface
int digitalVal;     // digital readings

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

void loop ()
{
  // Read the digital interface
  digitalVal = digitalRead(digitalPin);
  Serial.println(digitalVal); // print digital value
  delay(100);
}
KY 025 digital connection
KY-025 Digital Arduino

KY-025 Analog and Digital Connection

It is also possible of course to connect both, the analog and the digital pin to the microcontroller. In this setup we are able to tune the potentiometer to a value we are able to observe with the analog output. The following fritzing pictures combine the previous connections.

The use case is the following: We want to set the threshold to a specific value. Therefor we have to read the analog values and tune the potentiometer that we exceed the threshold in case of that specific value. The program code is a combination of the analog and digital code. The following video shows how I place the magnet in distance to the reed switch and tune the potentiometer that the LED is turned on by this exact distance.

int analogPin = A0; // KY-025 analog interface
int analogVal;      // analog readings
int digitalPin = 8; // KY-025 digital interface
int digitalVal;     // digital readings

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

void loop ()
{
  // Read the analog interface
  analogVal = analogRead(analogPin);
  Serial.println(analogVal); // print analog value

  // Read the digital interface
  digitalVal = digitalRead(digitalPin);
  Serial.println(digitalVal); // print digital value
  
  delay(100);
}
Reed Switch - KY-025 Analog and Digital Measurement
KY 025 analog and digital connection

KY-021 Digital Connection

The KY-021 has only a digital output and no build in LED. Therefor I use an external LED to visualize that the threshold is exceeded.

The program sketch is the same as for the digital connection for the KY-025. I only added the part that the external LED is turned on and off depending on the strength of the magnetic field.

int led = 9 ;       // LED
int reedswitch = 8; // KY-021 digital interface
int digitalVal;     // digital readings

void setup ()
{
  pinMode(led, OUTPUT); 
  pinMode(reedswitch, INPUT); 
  Serial.begin(9600);
}

void loop ()
{
  // Read the digital interface
  digitalVal = digitalRead(reedswitch);
  // when the Hall sensor detects a magnetic field, turn LED on
  if (digitalVal == LOW) 
  {
    digitalWrite (led, HIGH);
  }
  else
  {
    digitalWrite (led, LOW);
  }
  delay(100);
}
KY 021 digital connection

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