{"id":2543,"date":"2017-06-05T16:40:22","date_gmt":"2017-06-05T15:40:22","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2543"},"modified":"2017-06-05T16:40:22","modified_gmt":"2017-06-05T15:40:22","slug":"using-motion-sensor-raspberry-pi-php","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/","title":{"rendered":"Using a motion sensor on a Raspberry Pi with PHP"},"content":{"rendered":"<p>Adding a motion sensor to a Raspberry Pi is easy. In this article I&#8217;ll show you how to use it with PHP.<\/p>\n<p>The <a href=\"https:\/\/www.amazon.co.uk\/gp\/product\/B01DM8MX6A\/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&#038;tag=robertprcouk-21&#038;camp=1634&#038;creative=6738&#038;linkCode=as2&#038;creativeASIN=B01DM8MX6A&#038;linkId=c273fb037beae1458886bf78bf22414e\">HC-SR501<\/a> is a pyroelectric infrared PIR motion sensor module, that connects to the Raspberry Pi using just 3 wires.<\/p>\n<p>Connect GND to GND on the Pi, VCC to +5V on the Pi, and OUT to GPIO 17 on the Pi. That&#8217;s all that&#8217;s needed.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/motion-sensor-pi.png\" alt=\"Added a HC-SR501 motion detector to a Raspberry Pi\" width=\"972\" height=\"990\" class=\"aligncenter size-full wp-image-2544\" srcset=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/motion-sensor-pi.png 972w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/motion-sensor-pi-295x300.png 295w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/motion-sensor-pi-768x782.png 768w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>To use the sensor with PHP we use the <a href=\"https:\/\/github.com\/calcinai\/phpi\">PHPi<\/a> library. This library supports event driven bindings for the Raspberry Pi GPIO. We install this using <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a>.<\/p>\n<pre>\r\ncomposer require calcinai\/phpi\r\n<\/pre>\n<p>To use the PHPi library we need to bring it in using the autoloader, and add some namespace shortcuts.<\/p>\n<pre>\r\nrequire 'vendor\/autoload.php';\r\n\r\nuse Calcinai\\PHPi\\Pin;\r\nuse Calcinai\\PHPi\\Pin\\PinFunction;\r\n<\/pre>\n<p>We then create a board<\/p>\n<pre>\r\n$board = \\Calcinai\\PHPi\\Factory::create();\r\n<\/pre>\n<p>We now need to declare that we&#8217;re using GPIO pin 17 for input.<\/p>\n<pre>\r\n$pin = $board-&gt;getPin(17)\r\n             -&gt;setFunction(PinFunction::INPUT)\r\n             -&gt;setPull(Pin::PULL_UP);\r\n<\/pre>\n<p>As this is event driven programming, we need to listen to the pin to see when it changes. When the pin goes high, we know motion has been detected and we can do something. In this example, we&#8217;ll just write a message to the screen.<\/p>\n<pre>\r\n$pin-&gt;on('level.high', function() {\r\n        echo \"Motion detected\\n\";\r\n});\r\n<\/pre>\n<p>Finally we start the start the loop running, so the script is listening and reacting to events.<\/p>\n<pre>\r\n$board-&gt;getLoop()-&gt;run();\r\n<\/pre>\n<p>Putting it all together<\/p>\n<p>Here&#8217;s the final working PHP to detect motion using the HC-SR501<\/p>\n<pre>\r\n&lt;?php\r\n\r\nrequire 'vendor\/autoload.php';\r\n\r\nuse Calcinai\\PHPi\\Pin;\r\nuse Calcinai\\PHPi\\Pin\\PinFunction;\r\n\r\n$board = \\Calcinai\\PHPi\\Factory::create();\r\n\r\n$pin = $board-&gt;getPin(17) \/\/BCM pin number\r\n             -&gt;setFunction(PinFunction::INPUT)\r\n             -&gt;setPull(Pin::PULL_UP);\r\n\r\n$pin-&gt;on('level.high', function() {\r\n        echo \"Motion detected\\n\";\r\n});\r\n\r\n$board-&gt;getLoop()-&gt;run();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Adding a motion sensor to a Raspberry Pi is easy. In this article I&#8217;ll show you how to use it with PHP. The HC-SR501 is a pyroelectric infrared PIR motion sensor module, that connects to the Raspberry Pi using just 3 wires. Connect GND to GND on the Pi, VCC to +5V on the Pi, &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using a motion sensor on a Raspberry Pi with PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2546,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[84],"tags":[87,50,85,88],"class_list":["post-2543","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hardware","tag-gpio","tag-php","tag-raspberry-pi","tag-sensors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Using a motion sensor on a Raspberry Pi with PHP - Robert Price<\/title>\n<meta name=\"description\" content=\"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using a motion sensor on a Raspberry Pi with PHP - Robert Price\" \/>\n<meta property=\"og:description\" content=\"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-05T15:40:22+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Using a motion sensor on a Raspberry Pi with PHP\",\"datePublished\":\"2017-06-05T15:40:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/\"},\"wordCount\":221,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/pi-hc-sr501-sensor.jpg\",\"keywords\":[\"GPIO\",\"PHP\",\"Raspberry Pi\",\"sensors\"],\"articleSection\":[\"Hardware\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/\",\"name\":\"Using a motion sensor on a Raspberry Pi with PHP - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/pi-hc-sr501-sensor.jpg\",\"datePublished\":\"2017-06-05T15:40:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/pi-hc-sr501-sensor.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/pi-hc-sr501-sensor.jpg\",\"width\":1200,\"height\":600,\"caption\":\"HC-SR501 motion sensor attached to a Raspberry Pi\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/using-motion-sensor-raspberry-pi-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using a motion sensor on a Raspberry Pi with PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using a motion sensor on a Raspberry Pi with PHP - Robert Price","description":"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.","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":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/","og_locale":"en_GB","og_type":"article","og_title":"Using a motion sensor on a Raspberry Pi with PHP - Robert Price","og_description":"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/","og_site_name":"Robert Price","article_published_time":"2017-06-05T15:40:22+00:00","og_image":[{"width":1200,"height":600,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg","type":"image\/jpeg"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Using a motion sensor on a Raspberry Pi with PHP","datePublished":"2017-06-05T15:40:22+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/"},"wordCount":221,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg","keywords":["GPIO","PHP","Raspberry Pi","sensors"],"articleSection":["Hardware"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/","name":"Using a motion sensor on a Raspberry Pi with PHP - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg","datePublished":"2017-06-05T15:40:22+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"A working example showing to wire and use a HC-SR501 PIR motion sensor to a Raspberry Pi, and how to detect motion using PHP.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/pi-hc-sr501-sensor.jpg","width":1200,"height":600,"caption":"HC-SR501 motion sensor attached to a Raspberry Pi"},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/using-motion-sensor-raspberry-pi-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Using a motion sensor on a Raspberry Pi with PHP"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2543","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2543"}],"version-history":[{"count":2,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2543\/revisions"}],"predecessor-version":[{"id":2547,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2543\/revisions\/2547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2546"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2543"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2543"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2543"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}