{"id":463,"date":"2017-08-11T01:34:44","date_gmt":"2017-08-11T00:34:44","guid":{"rendered":"http:\/\/192.168.1.18\/?page_id=463"},"modified":"2018-09-26T15:31:08","modified_gmt":"2018-09-26T14:31:08","slug":"raspberry-pi-connecting-pir-switch-motion-detector","status":"publish","type":"page","link":"http:\/\/www.bulis.co.uk\/?page_id=463","title":{"rendered":"Raspberry Pi: Connecting a PIR Switch (motion detector)"},"content":{"rendered":"<div id=\"attachment_465\" style=\"width: 209px\" class=\"wp-caption alignright\"><img aria-describedby=\"caption-attachment-465\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-465\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/pir_sensor.png\" alt=\"PIR Sensor\" width=\"199\" height=\"164\" \/><p id=\"caption-attachment-465\" class=\"wp-caption-text\">PIR Sensor<\/p><\/div>\n<p>Passive Infra Red sensors are commonly used in security systems to detect intruders. All objects whose temperatures are above zero emit infra red radiation.<br \/>\nThe sensor is referred to as passive because it does not send out any signal in order to detect movement. It adjusts itself to the infra red signature of the room it\u2019s in and then watches for any changes. Any object moving through the room will disturb the infra red signature, and will cause a change to be noticed by the PIR sensor.<br \/>\nThere are three pins on the PIR sensor one requires 5 volts, one is the ground and the other is the sensor (the middle connector) which outputs 3 volts when motion is detected. These can be connected to the GPIO pins on the Raspberry Pi (please see Connecting Hardware to a Raspberry Pi):<\/p>\n<p>&nbsp;<\/p>\n<div id=\"attachment_466\" style=\"width: 489px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-466\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-466\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/pir_sensor_wiring.png\" alt=\"PIR Sensor Wiring\" width=\"479\" height=\"432\" \/><p id=\"caption-attachment-466\" class=\"wp-caption-text\">PIR Sensor Wiring<\/p><\/div>\n<p>We&#8217;re going to use the Python programming language to write some code that will detect movement and print out some text. When movement is detected the PIR motion sensor applies power to its OUT pin, which I have chosen to connect to GPIO pin 32 on the Pi. So in our code we just need to continually check pin 32 to see if it has power or not.<\/p>\n<p>If a pin has power we call it HIGH and if not we call it LOW.<\/p>\n<p>The program is pretty simple. We will first set up the Raspberry Pi GPIO pins to allow us to use pin 32 as an input; it can then detect when the PIR module sends power. We need to continually check the pin for any changes, so a while True loop is used for this. This is an infinite loop so the program will run continuously unless we stop it manually with Ctrl + C.<\/p>\n<p>We then use two Boolean (True or False) variables for the previous and current states of the pin, the previous state being what the current state was the preceding time around the loop. Inside the loop we compare the previous state to the current state to detect when they&#8217;re different. We don&#8217;t want to keep displaying a message if there has been no change.<\/p>\n<p>The Python GPIO <a href=\"http:\/\/code.google.com\/p\/raspberry-gpio-python\/\">library<\/a> that comes with Raspbian makes it easy to enable the individual GPIO pins. To test the PIR and wiring open a terminal window and type the following to start the Python interpreter:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo python\r\n<\/pre>\n<p>Next type the following:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport RPi.GPIO as GPIO\r\nimport time\r\n\r\nPIR_PIN = 32\r\nGPIO.setmode(GPIO.BOARD)\r\nGPIO.setup(PIR_PIN, GPIO.IN, GPIO.PUD_DOWN)\r\nGPIO.setwarnings(False)\r\n\r\nprevious_state = False\r\ncurrent_state = True\r\n\r\nwhile True:\r\n  time.sleep(0.1)\r\n  previous_state = current_state\r\n  current_state = GPIO.input(PIR_PIN)\r\n  if current_state != previous_state:\r\n    new_state = \u201cHIGH\u201d if current_state else \u201cLOW\u201d\r\n    print(\u201cGPIO pin %s is %s\u201d % (PIR_PIN, new_state))\r\n\r\n<\/pre>\n<p>If you start moving or waving the sensor pin will go HIGH. Keep on waving and it will stay HIGH, and only go back to LOW if you keep still again. If you see the sensor behave like this, then everything is working correctly:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nGPIO pin 32 is HIGH\r\nGPIO pin 32 is LOW\r\nGPIO pin 32 is HIGH\r\n<\/pre>\n<p>Press Ctrl + C when you want to exit.<\/p>\n<p>On the PIR sensor you should see two orange components with sockets that fit a Phillips screwdriver. These are called <em>potentiometers<\/em>, and they allow you to adjust the sensitivity of the sensor and the detection time. I would suggest setting the sensitivity to max and the time to min, but the choice is yours.<\/p>\n<div id=\"attachment_464\" style=\"width: 159px\" class=\"wp-caption alignright\"><img aria-describedby=\"caption-attachment-464\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-464\" src=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/pir_adjustments.png\" alt=\"PIR Adjustments\" width=\"149\" height=\"103\" \/><p id=\"caption-attachment-464\" class=\"wp-caption-text\">PIR Adjustments<\/p><\/div>\n<p>There are a number of useful things we can do with PIR\u2019s for example:<\/p>\n<ul>\n<li>Change the colour of a Tri-Coloured LED from red to green<\/li>\n<li>Connect a camera and take a snapshot of the intruder<\/li>\n<li>Send an Email or Twitter Notification<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Passive Infra Red sensors are commonly used in security systems to detect intruders. All objects whose temperatures are above zero emit infra red radiation. The sensor is referred to as passive because it does not send out any signal in order to detect movement. It adjusts itself to the infra red signature of the room [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":166,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.12 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/www.bulis.co.uk\/?page_id=463\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower\" \/>\n<meta property=\"og:description\" content=\"Passive Infra Red sensors are commonly used in security systems to detect intruders. All objects whose temperatures are above zero emit infra red radiation. The sensor is referred to as passive because it does not send out any signal in order to detect movement. It adjusts itself to the infra red signature of the room [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/www.bulis.co.uk\/?page_id=463\" \/>\n<meta property=\"og:site_name\" content=\"Phantom Raspberry Blower\" \/>\n<meta property=\"article:modified_time\" content=\"2018-09-26T14:31:08+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/pir_sensor.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=463\",\"url\":\"http:\/\/www.bulis.co.uk\/?page_id=463\",\"name\":\"Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower\",\"isPartOf\":{\"@id\":\"http:\/\/www.bulis.co.uk\/#website\"},\"datePublished\":\"2017-08-11T00:34:44+00:00\",\"dateModified\":\"2018-09-26T14:31:08+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=463#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/www.bulis.co.uk\/?page_id=463\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/www.bulis.co.uk\/?page_id=463#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/www.bulis.co.uk\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=57\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Raspberry Pi Tutorials\",\"item\":\"http:\/\/www.bulis.co.uk\/?page_id=166\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Raspberry Pi: Connecting a PIR Switch (motion detector)\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/www.bulis.co.uk\/#website\",\"url\":\"http:\/\/www.bulis.co.uk\/\",\"name\":\"Phantom Raspberry Blower\",\"description\":\"Blowing Raspberrys since 1989\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/www.bulis.co.uk\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/www.bulis.co.uk\/?page_id=463","og_locale":"en_GB","og_type":"article","og_title":"Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower","og_description":"Passive Infra Red sensors are commonly used in security systems to detect intruders. All objects whose temperatures are above zero emit infra red radiation. The sensor is referred to as passive because it does not send out any signal in order to detect movement. It adjusts itself to the infra red signature of the room [&hellip;]","og_url":"http:\/\/www.bulis.co.uk\/?page_id=463","og_site_name":"Phantom Raspberry Blower","article_modified_time":"2018-09-26T14:31:08+00:00","og_image":[{"url":"http:\/\/www.bulis.co.uk\/wp-content\/uploads\/2017\/08\/pir_sensor.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/www.bulis.co.uk\/?page_id=463","url":"http:\/\/www.bulis.co.uk\/?page_id=463","name":"Raspberry Pi: Connecting a PIR Switch (motion detector) - Phantom Raspberry Blower","isPartOf":{"@id":"http:\/\/www.bulis.co.uk\/#website"},"datePublished":"2017-08-11T00:34:44+00:00","dateModified":"2018-09-26T14:31:08+00:00","breadcrumb":{"@id":"http:\/\/www.bulis.co.uk\/?page_id=463#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["http:\/\/www.bulis.co.uk\/?page_id=463"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/www.bulis.co.uk\/?page_id=463#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/www.bulis.co.uk\/"},{"@type":"ListItem","position":2,"name":"Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=57"},{"@type":"ListItem","position":3,"name":"Raspberry Pi Tutorials","item":"http:\/\/www.bulis.co.uk\/?page_id=166"},{"@type":"ListItem","position":4,"name":"Raspberry Pi: Connecting a PIR Switch (motion detector)"}]},{"@type":"WebSite","@id":"http:\/\/www.bulis.co.uk\/#website","url":"http:\/\/www.bulis.co.uk\/","name":"Phantom Raspberry Blower","description":"Blowing Raspberrys since 1989","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/www.bulis.co.uk\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"}]}},"_links":{"self":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/463"}],"collection":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=463"}],"version-history":[{"count":0,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/463\/revisions"}],"up":[{"embeddable":true,"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=\/wp\/v2\/pages\/166"}],"wp:attachment":[{"href":"http:\/\/www.bulis.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}