{"id":17307,"date":"2016-02-03T11:18:35","date_gmt":"2016-02-03T11:18:35","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=17307"},"modified":"2023-04-23T11:10:55","modified_gmt":"2023-04-23T11:10:55","slug":"monitor-your-door-using-magnetic-reed-switch-and-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/monitor-your-door-using-magnetic-reed-switch-and-arduino\/","title":{"rendered":"Monitor Your Door Using Magnetic Reed Switch and Arduino"},"content":{"rendered":"<p>This tutorial&nbsp;demonstrates how to use a magnetic reed&nbsp;switch. I&#8217;ll do a quick overview on how it works&nbsp;and show&nbsp;a project example using an Arduino.<\/p>\n<p><!--more--><\/p>\n<h2>Description<\/h2>\n<p>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.<\/p>\n<p>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.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter wp-image-17333\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/label.jpg?resize=407%2C417&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"label\" width=\"407\" height=\"417\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/label.jpg?w=703&amp;quality=100&amp;strip=all&amp;ssl=1 703w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/label.jpg?resize=293%2C300&amp;quality=100&amp;strip=all&amp;ssl=1 293w\" sizes=\"(max-width: 407px) 100vw, 407px\" \/><\/p>\n<h2>How does it&nbsp;work?<\/h2>\n<p>It\u2019s very very simple.<\/p>\n<p>The electrical circuit is closed when a magnet is near the switch (less than 13 mm (0.5\u2019\u2019) away). When the magnet is far away from the switch, the circuit is open. See the figure below.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-17317 \" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic_reed_switch_howitworks.png?resize=665%2C374&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"magnetic_reed_switch_howitworks\" width=\"665\" height=\"374\"><\/p>\n<h2>Where to buy?<\/h2>\n<p>These switches are very cheap. You can buy them&nbsp;on ebay <a href=\"http:\/\/ebay.to\/1KU10e9\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<h2>Project Example<\/h2>\n<p>In this example, we will turn on a red LED if your door is open and a green LED if your door is closed.<\/p>\n<p>You&#8217;ll need the following components:<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/magnetic-reed-switch\/\" target=\"_blank\" rel=\"noopener noreferrer\">1\u00d7 Magnetic Reed Switch<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>&nbsp;\u2013 read&nbsp;<a href=\"https:\/\/makeradvisor.com\/best-arduino-starter-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">Best Arduino Starter Kits<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1\u00d7 green LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1\u00d7 red LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">2\u00d7 220\u03a9 resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">1\u00d7 10k\u03a9 resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">1\u00d7 breadboard<\/a><\/li>\n<\/ul>\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n<h2>Schematics<\/h2>\n<p>Here&#8217;s the schematics for this example.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-17367\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic-contact-switch_bb-1.png?resize=781%2C599&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"magnetic contact switch_bb\" width=\"781\" height=\"599\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic-contact-switch_bb-1.png?w=1755&amp;quality=100&amp;strip=all&amp;ssl=1 1755w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic-contact-switch_bb-1.png?resize=300%2C230&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic-contact-switch_bb-1.png?resize=768%2C589&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/magnetic-contact-switch_bb-1.png?resize=1024%2C786&amp;quality=100&amp;strip=all&amp;ssl=1 1024w\" sizes=\"(max-width: 781px) 100vw, 781px\" \/><\/p>\n<h2>Uploading Sketch<\/h2>\n<p>For this example, upload the following code:<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  \n Created by Rui Santos\n \n All the resources for this project:\n http:\/\/randomnerdtutorials.com\/\n \n*\/\n\nint ledOpen=8;\nint ledClose=10;\nint switchReed=6;\n\nvoid setup(){\n  pinMode(ledOpen, OUTPUT);\n  pinMode(ledClose, OUTPUT);\n  pinMode(switchReed, INPUT);\n  Serial.begin(9600);\n}\n\nvoid loop(){\n  \n  if (digitalRead(switchReed)==HIGH){\n    digitalWrite(ledOpen, LOW);\n    digitalWrite(ledClose, HIGH);\n    Serial.println(&quot;Your Door is Closed&quot;);\n  }\n  else {\n    digitalWrite(ledOpen, HIGH);\n    digitalWrite(ledClose, LOW);\n    Serial.println(&quot;Your Door is Open&quot;);\n  }\n  delay(1);\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/Magnetic_Contact_Switch.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Demonstration<\/h2>\n<p>Here&#8217;s a video demo showing this project working.<\/p>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/Dq_dTCiEG_Y?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>I hope you found this tutorial useful.<\/p>\n<p>Are you thinking of including a magnetic reed switch in one of your projects? Share your thoughts writing a comment below.<\/p>\n<p><strong>Share this post<\/strong> with a friend that also likes electronics!<\/p>\n<p>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 <a href=\"https:\/\/randomnerdtutorials.com\/download\">subscribing my blog<\/a> and my Facebook Page.<\/p>\n<p>Thanks for reading,<\/p>\n<p>-Rui Santos<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial&nbsp;demonstrates how to use a magnetic reed&nbsp;switch. I&#8217;ll do a quick overview on how it works&nbsp;and show&nbsp;a project example using an Arduino.<\/p>\n","protected":false},"author":1,"featured_media":17333,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[303,2,302,245,231,230,269,215,10],"tags":[],"class_list":["post-17307","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino","category-0-guide","category-arduino-ide","category-genuino","category-guide","category-guide-project","category-sensors","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/02\/label.jpg?fit=703%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/17307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=17307"}],"version-history":[{"count":1,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/17307\/revisions"}],"predecessor-version":[{"id":130646,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/17307\/revisions\/130646"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/17333"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=17307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=17307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=17307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}