This tutorial demonstrates how to use a magnetic reed switch. I’ll do a quick overview on how it works and show a project example using an Arduino.
Description
A magnetic contact switch is basically a reed switch encased in a plastic shell so that you can easily apply them in a door, a window or a drawer to detect if the door is open or closed.
The switch that we are going to use has two parts: the switch itself, that usually comes opened, and the magnet. When you buy this switch, it also comes with 4 screws, so that you can attach it to your door.
How does it work?
It’s very very simple.
The electrical circuit is closed when a magnet is near the switch (less than 13 mm (0.5’’) away). When the magnet is far away from the switch, the circuit is open. See the figure below.
Where to buy?
These switches are very cheap. You can buy them on ebay here.
Project Example
In this example, we will turn on a red LED if your door is open and a green LED if your door is closed.
You’ll need the following components:
- 1× Magnetic Reed Switch
- Arduino UNO – read Best Arduino Starter Kits
- 1× green LED
- 1× red LED
- 2× 220Ω resistor
- 1× 10kΩ resistor
- 1× breadboard
You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!
Schematics
Here’s the schematics for this example.
Uploading Sketch
For this example, upload the following code:
/*
Created by Rui Santos
All the resources for this project:
https://randomnerdtutorials.com/
*/
int ledOpen=8;
int ledClose=10;
int switchReed=6;
void setup(){
pinMode(ledOpen, OUTPUT);
pinMode(ledClose, OUTPUT);
pinMode(switchReed, INPUT);
Serial.begin(9600);
}
void loop(){
if (digitalRead(switchReed)==HIGH){
digitalWrite(ledOpen, LOW);
digitalWrite(ledClose, HIGH);
Serial.println("Your Door is Closed");
}
else {
digitalWrite(ledOpen, HIGH);
digitalWrite(ledClose, LOW);
Serial.println("Your Door is Open");
}
delay(1);
}
Demonstration
Here’s a video demo showing this project working.
I hope you found this tutorial useful.
Are you thinking of including a magnetic reed switch in one of your projects? Share your thoughts writing a comment below.
Share this post with a friend that also likes electronics!
You can contact me by leaving a comment. If you like this post probably you might like my next ones, so please support me by subscribing my blog and my Facebook Page.
Thanks for reading,
-Rui Santos
hello Rui,
Why is switchReed an OUTPUT ?
Thanks for bringing that up! I’ve fixed the typo, it is now set as an INPUT!
Cool, Rui! But shouldn’t the reed switch pin be in input mode not output?
pinMode(switchReed, OUTPUT);
Yes Sam, I’ve fixed the typo, it is now set as an INPUT. Thank you!
I think the code is gone from the website. Can I get a link?
Can you see the code now? Can you tell me which browser are you using?
If it still doesn’t see the code, please open this link: https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/Magnetic_Contact_Switch.ino
Yep, it’s there now, thanks.
Suggestion for next project Rui:
How to text my phone when someone opens my door (using the ESP8266)!
Thanks
Hi Rod,
Thank you for the suggestion that was exactly my thought and that’s why I’ve created this companion guide.
I think I’ll post the ESP8266 + Reed Switch in the next week or two.
Have a great day,
Rui
Looking forward to that thanks Rui. Of course there are many other applications for a switch input texter!!
I already have your Home Automation pdf – excellent!.
Hey! What holds the magnet and the switch, is it 3d printed, and if it is, can you please share the stl files?
HI Dimitar,
It’s not 3D printed. The plastic holders came with the magnet + reed switch when I bought them.
Thanks for asking,
Rui
Hello ,
Use loads of magnetic reed switches ( enclosed in glass tubes) on my model railway operated by small magnets under the locos. They prompt Arduinos to change points and signals, accelerate and decelerate the trains. I use Schmitt triggers to get rid of the bounce on the switches.
That’s a great application for these switches.
Thanks for sharing,
Rui
Hi Rui,
Do you have an idea on how to mònitor magnetic reed switch using my internet shield
You can use this sample code. It should be able to read the sensor just fine.
Just keep in mind that you can’t use the pins that require SPI communication between your Arduino and your Ethernet shield.
Nice project.
Isn’t it possible to log the time when tho door is openend?
Hi Thomas,
Yes, you can use a RTC with your Arduino to get the time
Hi Rui.
You really do an excelent work, i have a question like how much current pass from the magnetic swicth, i want to do this, but a little different for example when turn on and off a ligth.
PD. Sorry for my english xD greetings from Honduras, Centroamérica.
You’ll have to measure and you can do what you are looking for if you change the code Oscar.
Thanks,
Rui
Hey, quick Q. Every now and then (e.g. a few times every few seconds) i get the wrong reading from my input pin (it is checked in the loop function, so it is checked very frequently). My input pin mode is set to INPUT_PULLUP. When the magnetic reed switch activates, it connects to ground which pulls the input pin down to low. What could cause it to read as low when it shouldn’t? My wire is about 1m long. Could it be that the wire is so long that it is able to drag the pin down to low intermittently somehow?
It can be noise as you stated that causes false readings…
Hi this is great. I have an ESP8266–01, the new black one. Can I use this so I set pin 0 and 2 as output and pin 3 as input and use this below? Is there a way to run deepsleep with this if I were to use a battery? Also use MQTT to send open and close notifications?
hi and thanks for the tutorial, just learning, i am trying to figure out how the leds get power to light them up. the output from pins 8 and 10 is digital so 1 or 0 or is it a voltage, apols if this is a stupid question, cheers paddy
Hi Paddy.
When you send 1 to a GPIO, it is the same of sending a HIGH signal which means 5V.
When you send 0, it is the same as LOW.
I recommend you follow our Arduino mini course to get started with the basics. The course is completely free, and you can access the course in the link below:
– https://randomnerdtutorials.com/arduino-mini-course/
I hope this helps.
Regards
Sara 🙂
Hi 🙂
I am very new to Arduino.
Why is int switchReed = 6?
Thank you
Hi.
That refers to the pin that is connected to the reed switch. In this case it is connected to pin 6.
Regards,
Sara
Hi,
I have another question. It is possible to show only one message?
So ‘door is open’ 1 time and not every second? And then when I close it, once ‘Door is closed’?
Hi.
The reed switch is like a button.
So, you can use a debounce sketch.
In your Arduino IDE, you can go to File > Examples > Digital > Debounce, or you can chose the StateChangeDetection example.
Then, you just need to make a few modifications to assign the right pins.
Regards,
Sara
that is the purpose of the 10k resistor correct?
Hey there can we connect magnetic hall sensor with nodemcu Instead of Aurdino
Yes.
I need to control up to 10 different doors (very close to each other, a multiple shelve storage with one door to each shelve).
Can this be done using only 1 Arduino, or do I really need 1 Arduino for each door?
I want the system to show a GREEN light when door is open, and RED light when door is closed.
Hi.
You should be able to do it with just one Arduino.
Regards,
Sara
I don’t know if you still have access to this or are you still access it tutorial
What I would like to ask do you know how I would make a battery operated loud siren to connect to contacts on outbuilding doors of my farm so that I would be alerted with the loud extra loud siren when someone broken or opened a door. Just a simple magnetic contact switch connected to a siren and a battery that would go off when the door was open no one seems to know without going into a big rigmarole putting boxes together in all this other stuff that I don’t really need I need about five of them and so do other people on my farm area they would really like this we discussed it but nobody has the technology or know how and when we contact alarm companies they want to sell us these big complicated things that require electricity don’t need it hooked to Wi-Fi I don’t need it hooked to phone systems just loud just asking
I’m a little bit savvy when it comes to putting things together but every thing that I’ve looked up gets way too complicated for the simple situation. Many many thanks if you get this an answer me
Lynn
Hi, is it possible to add :
“Ding Dong” sound when the door Open
“Ding” sound whwn the door close,
Thanks