{"id":3684,"date":"2014-08-18T13:09:38","date_gmt":"2014-08-18T13:09:38","guid":{"rendered":"http:\/\/randomnerdtutorials.com\/?p=3684"},"modified":"2019-04-02T10:23:36","modified_gmt":"2019-04-02T10:23:36","slug":"arduino-with-pir-motion-sensor","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/arduino-with-pir-motion-sensor\/","title":{"rendered":"Arduino with PIR Motion Sensor"},"content":{"rendered":"<p>In this project you&#8217;re\u00a0going to create a simple circuit with an Arduino and PIR motion sensor that can detect movement.\u00a0An LED will light up when movement is detected.<\/p>\n<p><!--more--><\/p>\n<h2>Watch the video below to see how it works<\/h2>\n<p style=\"text-align:center\"><iframe width=\"720\" height=\"405\" src=\"https:\/\/www.youtube.com\/embed\/vJgtckLzoKM?rel=0\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<h2>Introducing the PIR Motion Sensor<\/h2>\n<p>The PIR motion sensor is ideal to detect movement. PIR stand for \u201cPassive Infrared\u201d. Basically, the PIR motion sensor measures infrared light from objects in its field of view.<\/p>\n<p>So, it can detect motion based on changes in infrared light in the environment. It is ideal to detect\u00a0if a human has moved in or out of the sensor range.<\/p>\n<p><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-29749\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=300%2C251&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"pir\" width=\"300\" height=\"251\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?w=300&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=150%2C126&amp;quality=100&amp;strip=all&amp;ssl=1 150w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=80%2C67&amp;quality=100&amp;strip=all&amp;ssl=1 80w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=220%2C184&amp;quality=100&amp;strip=all&amp;ssl=1 220w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=120%2C100&amp;quality=100&amp;strip=all&amp;ssl=1 120w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=179%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 179w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/pir.jpg?resize=284%2C238&amp;quality=100&amp;strip=all&amp;ssl=1 284w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>The sensor in the figure above has two built-in potentiometers to adjust\u00a0the delay time (the potentiometer at the left) and the sensitivity (the potentiometer at the right).<\/p>\n<h2>Pinout<\/h2>\n<p>Wiring the PIR motion sensor to an Arduino is pretty straightforward \u2013 the sensor has only 3 pins.<\/p>\n<ul>\n<li>GND\u00a0\u2013 connect to ground<\/li>\n<li>OUT\u00a0\u2013 connect to an Arduino digital pin<\/li>\n<li>5V\u00a0\u2013 connect to 5V<\/li>\n<\/ul>\n<h2><\/h2>\n<h2>Parts required<\/h2>\n<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter wp-image-3686\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/1parts.jpg?resize=554%2C369&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"1parts\" width=\"554\" height=\"369\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/1parts.jpg?w=650&amp;quality=100&amp;strip=all&amp;ssl=1 650w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/1parts.jpg?resize=300%2C199&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 554px) 100vw, 554px\" \/><\/p>\n<p>Here&#8217;s the required parts for this project<\/p>\n<ul>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/pir-motion-sensor-hc-sr501\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x PIR Motion Sensor (HC-SR501)<\/a><\/li>\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\/3mm-5mm-leds-kit-storage-box\/\" target=\"_blank\" rel=\"noopener noreferrer\">1x LED<\/a><\/li>\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noopener noreferrer\">Jumper Cables<\/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>Assemble all the parts by following the schematics below.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/Arduino-with-PIR-motion-sensor-schematics.jpg?quality=100&#038;strip=all&#038;ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"aligncenter size-full wp-image-3688\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/Arduino-with-PIR-motion-sensor-schematics.jpg?resize=648%2C387&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Arduino with PIR motion sensor schematics\" width=\"648\" height=\"387\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/Arduino-with-PIR-motion-sensor-schematics.jpg?w=648&amp;quality=100&amp;strip=all&amp;ssl=1 648w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/Arduino-with-PIR-motion-sensor-schematics.jpg?resize=300%2C179&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 648px) 100vw, 648px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h2>Code<\/h2>\n<p>Upload the following code.<\/p>\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*  \n    Arduino with PIR motion sensor\n    For complete project details, visit: http:\/\/RandomNerdTutorials.com\/pirsensor\n    Modified by Rui Santos based on PIR sensor by Limor Fried\n*\/\n \nint led = 13;                \/\/ the pin that the LED is atteched to\nint sensor = 2;              \/\/ the pin that the sensor is atteched to\nint state = LOW;             \/\/ by default, no motion detected\nint val = 0;                 \/\/ variable to store the sensor status (value)\n\nvoid setup() {\n  pinMode(led, OUTPUT);      \/\/ initalize LED as an output\n  pinMode(sensor, INPUT);    \/\/ initialize sensor as an input\n  Serial.begin(9600);        \/\/ initialize serial\n}\n\nvoid loop(){\n  val = digitalRead(sensor);   \/\/ read sensor value\n  if (val == HIGH) {           \/\/ check if the sensor is HIGH\n    digitalWrite(led, HIGH);   \/\/ turn LED ON\n    delay(100);                \/\/ delay 100 milliseconds \n    \n    if (state == LOW) {\n      Serial.println(&quot;Motion detected!&quot;); \n      state = HIGH;       \/\/ update variable state to HIGH\n    }\n  } \n  else {\n      digitalWrite(led, LOW); \/\/ turn LED OFF\n      delay(200);             \/\/ delay 200 milliseconds \n      \n      if (state == HIGH){\n        Serial.println(&quot;Motion stopped!&quot;);\n        state = LOW;       \/\/ update variable state to LOW\n    }\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\/Arduino_with_PIR_motion_sensor.ino\" target=\"_blank\">View raw code<\/a><\/p>\n<h2 style=\"color: #222222;\">Wrapping Up<\/h2>\n<p>This post shows a simple example on how to use the PIR motion sensor with the Arduino. Now, you can use the PIR motion sensor in more advanced projects. For example, you can build a\u00a0<a href=\"https:\/\/randomnerdtutorials.com\/build-a-night-security-light-with-arduino\/\" target=\"_blank\" rel=\"noopener noreferrer\">Night Security Light project<\/a>.<\/p>\n<p>If you&#8217;re an absolute beginner, and you&#8217;re just getting started, we recommend taking a look at our <a href=\"https:\/\/randomnerdtutorials.com\/arduino-mini-course\/\" target=\"_blank\" rel=\"noopener noreferrer\">Free Arduino Mini Course<\/a>.<\/p>\n<p style=\"color: #404040;\">Thanks for reading. If you like this post probably you might like our next ones, so please support us by <a href=\"https:\/\/randomnerdtutorials.com\/download\/\" target=\"_blank\" rel=\"noopener noreferrer\">subscribing our blog<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this project you&#8217;re\u00a0going to create a simple circuit with an Arduino and PIR motion sensor that can detect movement.\u00a0An LED will light up when movement is detected.<\/p>\n","protected":false},"author":5,"featured_media":3685,"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":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2,302,269,264,10],"tags":[],"class_list":["post-3684","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-0-guide","category-guide-project","category-project","category-a-tutorials"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2014\/08\/Thumbnail-Blog.jpg?fit=%2C&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/3684","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=3684"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/3684\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/3685"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=3684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=3684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=3684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}