{"id":18,"date":"2012-10-22T06:16:05","date_gmt":"2012-10-22T06:16:05","guid":{"rendered":"http:\/\/www.millamilla.com\/?p=18"},"modified":"2012-10-22T06:16:05","modified_gmt":"2012-10-22T06:16:05","slug":"using-raspberry-pi-with-pir-motion-sensor","status":"publish","type":"post","link":"https:\/\/www.millamilla.com:443\/?p=18","title":{"rendered":"Using Raspberry Pi with PIR ( motion ) Sensor"},"content":{"rendered":"<p>I have been using the Arduino for a few years now and recently curiosity got the better of me with the excitement around the Raspberry Pi. I purchased my first Raspberry Pi and since my interest is more in line with interacting with the physical than just having a small computer, I needed a second project. Of course, the first project was to blink an LED!<\/p>\n<p>For my second project, I decided to expand on the first. I still use output to blink an LED, but now I also use input to decide when to blink the LED. The way this project works is by sensing motion using a PIR, Passive Infra-Red, sensor.<\/p>\n<p>Using the Adafruit Raspberry Pi Cobbler, here&#8217;s how I connected the Raspberry Pi GPIO pins to the breadboard and wired up the PIR sensor and LED<\/p>\n<p><a href=\"http:\/\/ostrich.hedrickfam.com:8081\/wp-content\/uploads\/2012\/10\/pir_test_bb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-31\" title=\"pir_test_bb\" src=\"http:\/\/ostrich.hedrickfam.com:8081\/wp-content\/uploads\/2012\/10\/pir_test_bb-300x256.png\" alt=\"\" width=\"300\" height=\"256\" srcset=\"https:\/\/www.millamilla.com:443\/wp-content\/uploads\/2012\/10\/pir_test_bb-300x256.png 300w, https:\/\/www.millamilla.com:443\/wp-content\/uploads\/2012\/10\/pir_test_bb-1024x876.png 1024w, https:\/\/www.millamilla.com:443\/wp-content\/uploads\/2012\/10\/pir_test_bb.png 1059w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/ostrich.hedrickfam.com:8081\/wp-content\/uploads\/2012\/10\/pir_test.fzz\" rel=\"attachment wp-att-34\">Download<\/a>\u00a0the\u00a0<a href=\"http:\/\/fritzing.org\/\">Fritzing<\/a>\u00a0diagram. This requires the\u00a0<a href=\"http:\/\/learn.adafruit.com\/using-the-adafruit-library-with-fritzing\/download-the-fritzing-library-from-github\">Adafruit<\/a>Fritzing library.<\/p>\n<h4><\/h4>\n<h4>Here&#8217;s the <a href=\"https:\/\/bitbucket.org\/bhedrick\/raspberry-pi-python-code\/src\/ddf8eac15b311ad44d0dda76295ae0e4e02a6a58\/pir_test\/pir_test.py?at=master\">code<\/a>!<\/h4>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport RPi.GPIO as GPIO\r\nimport time\r\n\r\nPIR = 23\r\nLED = 24\r\n\r\npirState = False                        # we start, assuming no motion detected\r\npirVal = False                          # we start, assuming no motion detected\r\n\r\nGPIO.setmode(GPIO.BCM)\r\nGPIO.setup(PIR, GPIO.IN)\r\nGPIO.setup(LED, GPIO.OUT)\r\n\r\nwhile True:\r\n    pirVal = GPIO.input(PIR)            # read input value\r\n    if (pirVal == True):                # check if the input is HIGH\r\n        GPIO.output(LED, True)          # turn LED ON\r\n        if (pirState == False):\r\n            # we have _just_ turned on\r\n            pirState = True\r\n    else:\r\n        GPIO.output(LED, False)         # turn LED OFF\r\n        if (pirState == True):\r\n            # we have _just_ turned off\r\n            time.sleep(2)\r\n            pirState = False;\r\n<\/pre>\n<h4>Parts List \/ BOM (Bill of Materials) &#8211; Excluding Raspberry PI<\/h4>\n<table class=\"reference\" style=\"width: 60%;\">\n<tbody>\n<tr>\n<th>Part<\/th>\n<th>Source<\/th>\n<th>Image<\/th>\n<th>Approx Price<\/th>\n<\/tr>\n<tr>\n<td>1\/2 Size breadboard<\/td>\n<td><a href=\"http:\/\/www.adafruit.com\/products\/64\">Adafruit<\/a><\/td>\n<td><img decoding=\"async\" src=\"http:\/\/www.adafruit.com\/images\/halfboard.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$5.00<\/td>\n<\/tr>\n<tr>\n<td>Pi Cobbler Breakout Kit for Raspberry Pi<\/td>\n<td><a href=\"http:\/\/www.adafruit.com\/products\/914\">Adafruit<\/a><\/td>\n<td><img decoding=\"async\" src=\"http:\/\/www.adafruit.com\/images\/ID914parts.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$7.95<\/td>\n<\/tr>\n<tr>\n<td>PIR (motion) sensor<\/td>\n<td><a href=\"http:\/\/www.adafruit.com\/products\/189\">Adafruit<\/a><\/td>\n<td><img decoding=\"async\" src=\"http:\/\/www.adafruit.com\/images\/pirsensor.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$10.00<\/td>\n<\/tr>\n<tr>\n<td>220 ohm resistor<\/td>\n<td><a href=\"http:\/\/www.radioshack.com\/product\/index.jsp?productId=2062340\">RadioShack<\/a><\/td>\n<td><img decoding=\"async\" src=\"http:\/\/rsk.imageg.net\/graphics\/product_images\/pRS1C-2160249w345.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$1.19<\/td>\n<\/tr>\n<tr>\n<td>Green LED<\/td>\n<td><a href=\"http:\/\/www.radioshack.com\/product\/index.jsp?productId=2062551\">RadioShack<\/a><\/td>\n<td><img decoding=\"async\" src=\"http:\/\/rsk.imageg.net\/graphics\/product_images\/pRS1C-2160460w345.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$1.69<\/td>\n<\/tr>\n<tr>\n<td>Breadboarding Wire<\/td>\n<td><a href=\"https:\/\/www.adafruit.com\/products\/153\">Adafruit<\/a><\/td>\n<td><img decoding=\"async\" src=\"https:\/\/www.adafruit.com\/images\/ID0153.jpg\" alt=\"\" width=\"100px\" \/><\/td>\n<td>$6.00<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<td>Total<\/td>\n<td>$31.83*<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>* = Prices are the best information from the time this tutorial was written. Prices do not include<br \/>\nshipping.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been using the Arduino for a few years now and recently curiosity got the better of me with the excitement around the Raspberry Pi. I purchased my first Raspberry Pi and since my interest is more in line with interacting with the physical than just having a small computer, I needed a second [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=18"}],"version-history":[{"count":41,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=\/wp\/v2\/posts\/18\/revisions\/64"}],"wp:attachment":[{"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.millamilla.com:443\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}