{"id":39803,"date":"2017-06-08T09:05:09","date_gmt":"2017-06-08T09:05:09","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=39803"},"modified":"2019-04-02T10:23:28","modified_gmt":"2019-04-02T10:23:28","slug":"build-a-night-security-light-with-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/build-a-night-security-light-with-arduino\/","title":{"rendered":"Build a Night Security Light with Arduino"},"content":{"rendered":"<p>In this project you\u2019re going to build a night security light with a relay module, a photoresistor and an Arduino.<\/p>\n<p><!--more--><\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-5085 aligncenter\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2016\/11\/Night-Security-Light-GIF.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"night-security-light-gif\" width=\"480\" height=\"270\" \/><\/p>\n<p>A night security light only\u00a0turns on when it&#8217;s dark and when\u00a0movement is detected.<\/p>\n<p>Here\u2019s the main features of this project:<\/p>\n<ul>\n<li>the lamp turns on when it\u2019s dark AND movement is detected;<\/li>\n<li>when movement is detected the lamp stays on for 10 seconds;<\/li>\n<li>when the lamp is ON and detects movement, it starts counting 10 seconds again;<\/li>\n<li>when there\u2019s light, the lamp is turned off, even when motion is detected.<\/li>\n<\/ul>\n<h2>Recommended resources<\/h2>\n<p>The following resources include guides on how to use\u00a0the relay module and the PIR motion sensor with the Arduino, which might be useful for this project.<\/p>\n<ul>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/guide-for-relay-module-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Guide for Relay Module with Arduino<\/a><\/li>\n<li><a href=\"https:\/\/randomnerdtutorials.com\/arduino-with-pir-motion-sensor\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino with PIR Motion Sensor\u00a0<\/a><\/li>\n<\/ul>\n<h2>Parts required<\/h2>\n<p>Here\u2019s a complete list of the parts required for this project:<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-39806\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Parts-required.jpg?resize=700%2C460&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"460\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Parts-required.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Parts-required.jpg?resize=300%2C197&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/compatible-arduino-uno-r3-board\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino UNO<\/a>\u00a0\u2013 read\u00a0<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\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noopener noreferrer\">PIR Motion Sensor\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/photoresistor-light-dependent-resistor-ldr\/\" target=\"_blank\" rel=\"noopener noreferrer\">Photoresistor\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/resistors-kits\/\" target=\"_blank\" rel=\"noopener noreferrer\">10kOhm resistor<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/5v-2-channel-relay-module-optocoupler\/\" target=\"_blank\" rel=\"noopener noreferrer\">Relay module\u00a0<\/a><\/li>\n<li>Lamp cord set (<a href=\"http:\/\/ebay.to\/2feHELc\" target=\"_blank\" rel=\"noopener noreferrer\">view on eBay)<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noopener noreferrer\">Breadboard\u00a0<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper wires\u00a0<\/a><\/li>\n<\/ul>\n<p>Besides these electronics components, you also need an AC male socket, an AC wire and a lamp bulb holder (a lamp cord set). My lamp cord set is the one in the figure below.<\/p>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-39807\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/lamp-cord-set.jpg?resize=700%2C418&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"418\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/lamp-cord-set.jpg?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/lamp-cord-set.jpg?resize=300%2C179&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\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>Code<\/h2>\n<p>Download or copy the following\u00a0code to your Arduino IDE, and upload it to your Arduino board.<\/p>\n<p><strong><span style=\"color: #ff0000;\">Warning:<\/span> do not upload a new code to your Arduino board while your lamp is\u00a0connected to the\u00a0mains voltage. You should unplug the lamp from mains voltage, before upload a new sketch to your Arduino.<\/strong><\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n * Rui Santos \n * Complete Project Details http:\/\/randomnerdtutorials.com\n *\/\n \n\/\/ Relay pin is controlled with D8. The active wire is connected to Normally Closed and common\nint relay = 8;\nvolatile byte relayState = LOW;\n\n\/\/ PIR Motion Sensor is connected to D2.\nint PIRInterrupt = 2;\n\n\/\/ LDR pin is connected to Analog 0\nint LDRPin = A0;\n\/\/ LDR value is stored on LDR reading\nint LDRReading;\n\/\/ LDR Threshold value\nint LDRThreshold = 300;\n\n\/\/ Timer Variables\nlong lastDebounceTime = 0;  \nlong debounceDelay = 10000;\n\nvoid setup() {\n  \/\/ Pin for relay module set as output\n  pinMode(relay, OUTPUT);\n  digitalWrite(relay, HIGH);\n  \/\/ PIR motion sensor set as an input\n  pinMode(PIRInterrupt, INPUT);\n  \/\/ Triggers detectMotion function on rising mode to turn the relay on, if the condition is met\n  attachInterrupt(digitalPinToInterrupt(PIRInterrupt), detectMotion, RISING);\n  \/\/ Serial communication for debugging purposes\n  Serial.begin(9600);\n}\n\nvoid loop() {\n  \/\/ If 10 seconds have passed, the relay is turned off\n  if((millis() - lastDebounceTime) &gt; debounceDelay &amp;&amp; relayState == HIGH){\n    digitalWrite(relay, HIGH);\n    relayState = LOW;\n    Serial.println(&quot;OFF&quot;);\n  }\n  delay(50);\n}\n\nvoid detectMotion() {\n  Serial.println(&quot;Motion&quot;);\n  LDRReading = analogRead(LDRPin);\n  \/\/ LDR Reading value is printed on serial monitor, useful to get your LDRThreshold\n  \/\/Serial.println(LDRReading);\n  \/\/ Only turns the Relay on if the LDR reading is higher than the LDRThreshold\n  if(LDRReading &gt; LDRThreshold){\n    if(relayState == LOW){\n      digitalWrite(relay, LOW);\n    }\n    relayState = HIGH;  \n    Serial.println(&quot;ON&quot;);\n    lastDebounceTime = millis();\n  }\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\/Night_Security_Light_with_Arduino.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2>Schematics<\/h2>\n<p><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-29199\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=700%2C200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"200\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=150%2C43&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=300%2C86&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=80%2C23&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=220%2C63&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=250%2C71&amp;quality=100&amp;strip=all&amp;ssl=1 250w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=280%2C80&amp;quality=100&amp;strip=all&amp;ssl=1 280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2016\/11\/warning-m-1.png?resize=510%2C146&amp;quality=100&amp;strip=all&amp;ssl=1 510w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>Here\u2019s the schematics for this project.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Schematics_f.png?quality=100&#038;strip=all&#038;ssl=1\" target=\"_blank\" rel=\"noopener noreferrer\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-39804 size-full\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Schematics_f.png?resize=700%2C448&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"\" width=\"700\" height=\"448\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Schematics_f.png?w=700&amp;quality=100&amp;strip=all&amp;ssl=1 700w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Schematics_f.png?resize=300%2C192&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: if you have an earth (GND) connection in the mains voltage cable &#8211; a yellow and green cable &#8211; it should go outside the relay module, like the blue wire (neutral).<\/p>\n<h2>Demonstration<\/h2>\n<p>Here\u2019s your circuit in action:<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"size-full wp-image-5085 aligncenter\" src=\"https:\/\/i0.wp.com\/rntlab.com\/wp-content\/uploads\/2016\/11\/Night-Security-Light-GIF.gif?resize=480%2C270&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"night-security-light-gif\" width=\"480\" height=\"270\" \/><\/p>\n<h2>Wrapping up<\/h2>\n<p>In this project you\u2019ve built a night security light with a photoresistor and a PIR motion sensor.<\/p>\n<p>This is a great project to practice with relays and with the PIR motion sensor.<\/p>\n<p>If you like Arduino projects, make sure you check our latest Arduino course:\u00a0<strong><a href=\"https:\/\/randomnerdtutorials.com\/arduino-step-by-step-projects\/\" target=\"_blank\" rel=\"noopener noreferrer\">Arduino Step-by-step Projects &#8211; Build 25 Projects<\/a><\/strong><\/p>\n<p>Thanks for reading,<\/p>\n<p>Sara Santos<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project you\u2019re going to build a night security light with a relay module, a photoresistor and an Arduino.<\/p>\n","protected":false},"author":1,"featured_media":39804,"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,267,2,256,225,264,10],"tags":[],"class_list":["post-39803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-0-arduino","category-arduino-project","category-arduino","category-electronics","category-home-automation","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/06\/Schematics_f.png?fit=700%2C448&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/39803","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=39803"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/39803\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/39804"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=39803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=39803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=39803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}