Skip to Content

Interfacing Magnetic Switch To The Arduino

Interfacing Magnetic Switch To The Arduino

This article will show how to interface a magnetic switch to Arduino. Magnetic switches benefit security applications, open window detection, and many more. 

A magnetic switch opens or closes depending on whether the reed switch part is in the proximity of a magnet. 

Having an Arduino in these applications enables you to add intelligence. For example, you can automatically disable the security alarm at a particular time of the day. 

You can also use magnetic switches to detect the open window. This enables you to raise the alarm or take action to prevent energy loss.  

This article will take you through the magnetic switch operation principle and various applications involving Arduino.

I will give you circuit diagrams, a step-by-step connection guide, and the Arduino code required to complete the project. 

Let’s begin!

Components Needed To Build Arduino And Magnetic Switch Project

Hardware Components 

Software

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

Basics of A Magnetic Reed Switch

The reed switch is also known as the magnetic switch. The reed switch is an electromechanical switch. To close the reed switch, you use a magnet. 

The switch will close when you bring the magnet closer. The below image presents the working of the reed switch more simply.

The above image shows a reed switch with two blades inside the housing. The contacts are made up of ferromagnetic materials. There will be a tiny gap (usually in micrometers) between the contacts. 

When you bring a magnet close to the pins, the contacts come in contact with each other, closing the circuit and allowing the current to flow. 

The image below shows the reed switch’s condition when the magnet is closer and far from the reed switch.

The below animation gives you a better idea.

animation gives you a better idea

Did you know that reed switches are also used in laptops? They detect whether the laptop’s lid is open or closed.

When you close the lid, the magnet turns the reed switch on, indicating that the lid is closed to the processor. The laptop then goes to sleep mode!

You may also come across the reed switches with three terminals instead of two. I will summarise the types of reed switches you commonly find. 

  • Normally Closed reed switch – In this type of switch, the connection will be closed when the magnet is near. The current will flow through the switch. When the magnet is removed, the switch will open, cutting the current flow.
  • Normally open reed switch – The switch will be open when you keep the magnet closer to the reed switch. The current will not flow. The reed switch closes as soon as you remove the magnet.
  • Three terminal reed switch – Both options will be available for you. You can select which two pins to use. One of the terminals will be common.
NC and NO pins

In the above image, you can notice that there are three pins. One end of the switch has one pin, and the other has two pins. The single pin is called a common pin. The two pins on the other end are NC and NO pins. 

A simple representation is shown below

SPDT

Let us go through a datasheet of a reed switch and see the critical parameters we should look at before choosing one for your application. 

  • Physical dimensions include height, width, soldering footprint, and length. These are nonfunctional features but still important to consider.
  • Voltage rating – You should continuously operate the switch below this voltage. Any voltage near or above the ratings will fail the switch. There are possibilities for arc generation. 
  • Current rating
    • Switching current rating – the current rating of the switch when the current is being switched ON and OFF frequently. 
    • Carry current rating – The continuous current rating without switching. The carry current rating will be higher than the switching current rating. 
  • Contact rating – The wattage rating is an important parameter. You are allowed to reach either the maximum voltage rating or the current rating. You cannot simultaneously operate the switch at the maximum possible voltage and the maximum possible current. Hence, a vendor will always specify a wattage rating. This should be respected while choosing the operating conditions. 
  • Resistance
    • Contact resistance – The resistance between the terminals when the switch is closed. Usually less than an ohm.
    • Insulation resistance – The resistance between the terminals when the switch is open. Usually more than 1 Mega Ohms. 
  • Operating temperature – The safe range of temperature (including self-heating) where you can operate the switch

Operating time – Usually less than 1 ms. The time is taken by the switch to completely close or open.

How To Connect The Magnetic Switch To The Arduino Uno?

In this section, we will connect the Arduino to the magnetic switch. The connections are simple and easy to understand.

Step 1: Let us begin with the Ground connection

Let us begin with the Ground connection

It is an excellent practice to start the connections with the GND connection first. You can choose any of the GND pins available on the Arduino board.

Note that the magnetic switch I am using has only two terminals. Do not worry about the polarity of the two pins on the switch.

When connecting a resistor, you ignore which side of the resistor goes where. Similarly, you can ignore the reed switch polarity.

If you use a three-terminal magnetic switch, you must plan whether to use N.C. (normally open) or N.O. (normally closed) pins.

Please refer to the earlier section, where I have covered them in detail.

Step 2: Connect the Signal pin

Connect the Signal pin

Connect Pin 5 of the Arduino UNO to the second pin of the magnetic switch. This completes the essential connection to detect the status of the magnetic switch. 

Let us add an LED so we can drive the LED to indicate the status of the switch. You can also add a buzzer if you like. 

Step 3: Connect the LED to Arduino

Connect the LED to Arduino

Connect Pin 12 of the Arduino to a 220 Ohms resistor. Connect the other end of the resistor to the Anode of the LED. Connect the cathode of the LED to one of the GND pins of the Arduino. 

When you want to turn on the LED, you must drive Pin12 high. Do not miss the resistor. C

onnecting the current limiting resistor protects both the LED and the Arduino Uno pin from damage. 

Congratulations! You have completed the connection needed to interface the magnetic switch and an indication LED with the Arduino.

Arduino Code Example For The Reed Switch Project

In this section, I will take you through a simple Arduino program that turns an LED on whenever you take a magnet close to the reed switch. 

I am also enabling internal pullup so that we dont have to connect an external pullup to the Arduino GPIO pin where you have connected the reed switch. 

The Complete Arduino Code

Open the Arduino IDE and click on the “File” option. Under the file options, select “New.”

//Choose the Pin to which you have connected the LED
int LED = 12;
 
//Choose the Pin to which you have connected the Reed switch
int reed_switch = 5;
 
//Variable to store the reed switch status
int reed_status;
 
 
void setup() {
  pinMode(LED, OUTPUT);
 
  //Enable internal pullup
  pinMode(reed_switch, INPUT_PULLUP);
}
 
void loop() {
  reed_status = digitalRead(reed_switch);
 
//check if the magnet is not present
  if (reed_status == 1) {
    digitalWrite(LED, LOW);
  }
 
//check if the magnet is present
  else
  {
    digitalWrite(LED, HIGH);
  }
  delay(1000);
}

FAQs About The Magnetic Switch And The Arduino Projects

I have brought up several frequently asked questions about the magnetic switch and its applications in Arduino projects. In this section, you will find all the FAQs answered.

If you have any further questions, please post them in the comments section. 

1) How do I use a reed switch with Arduino?

Reed switches are electromechanical switches. You can use a reed switch as any other standard two-pin switch.

When the reed switch is open, the resistance between the pins will be very high (>10 Mega Ohms). When the reed switch is closed, the resistance will be very low (less than 10 ohms). 

You can use a reed switch to build the circuit to either detect logic high or logic low.

Arduino input pin to GND

In the above image, the reed switch is used to pull the Arduino input pin to GND when a magnet is close to the reed switch.

When the magnet is away, the reed switch will be open. Hence the line will now be pulled to high via the resistor R1. The Arduino will read the input as logic high. 

If you have to inverse the logic, follow the below connection.

Arduino input pin

When the reed switch is ON, the Arduino input pin is connected directly to the 5 V supply. Hence, Arduino will read it as logic 1.

When the reed switch is OFF, the Arduino input pin will be pulled low by the R1 resistor. 

2) How is a reed switch activated?

A reed switch can be activated by bringing the reed switch into proximity to a magnetic field. In the case of magnetic door switches, there will be a permanent magnet on the door. The reed switch will be stationary.

When the position of the door changes, the magnetic field applied to the reed switch changes. This either turns off the switch or turns on the switch based on the type. 

3) How do you connect a reed switch to an Arduino?

The reed switch is a two-terminal switch. You can one end of the reed switch directly to the Arduino. You will need external circuitry, internal pullup, or pulldown logic to use a reed switch in a helpful manner. 

In the previous section of this article, you can find several connection diagrams where a reed switch is connected to an Arduino.

4) Do the magnetic switch fail?

Like any other mechanical parts, reed switches also fail. There are multiple ways in which a magnetic switch can fail. 

  • Failure to close the connection – In this case, even in the presence of a magnetic field, the switch doesn’t close. This is also known as “missing.”
  • Failure to open the connection – In this case, the switch always remains in the close condition irrespective of whether there is a magnet close to it or not. This is also called “jamming.” 
  • Failure to provide the least resistance – In this case, the switch opens and closes. But the ON state resistance will be higher when the switch is in a closed state. This leads to inappropriate voltages across circuits or Arduino pins, leading to wrong readings.

Conclusion

I hope you have enjoyed the article on magnetic switches. Though seemingly simple, I had to learn a lot when I first started using reed switches in the applications.

I hope you are now confident to build a few Arduino projects using reed switches.

I have used reed switches in a classic window open detection project. If the window is not closed within a minute, the Arduino UNO will drive a buzzer.

This project ensured that nobody left the door open for a duration more than necessary. 

I am sure that the article was easy to follow. If you have any doubts or questions, please post them in the comments section.

Also, feel free to share your projects in the comments below.