{"id":2523,"date":"2017-06-01T09:44:08","date_gmt":"2017-06-01T08:44:08","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2523"},"modified":"2017-06-01T16:30:53","modified_gmt":"2017-06-01T15:30:53","slug":"controlling-a-led-on-a-raspberry-pi-with-php","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/","title":{"rendered":"Controlling a LED on a Raspberry Pi with PHP"},"content":{"rendered":"<p>I wanted to make a LED light up using PHP on a <a href=\"https:\/\/www.raspberrypi.org\/\">Raspberry Pi<\/a>. Most of <a href=\"https:\/\/thepihut.com\/blogs\/raspberry-pi-tutorials\/27968772-turning-on-an-led-with-your-raspberry-pis-gpio-pins\">the examples I&#8217;ve seen are for Python<\/a>, so I wanted to see if was possible or not.<\/p>\n<p>It is actually pretty easy to do, so I thought I&#8217;d share my work.<\/p>\n<p>The first thing to do is to wire up the LED to the Raspberry Pi. I used a breadboard so there is no soldering required.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED.png\" alt=\"\" width=\"1608\" height=\"990\" class=\"aligncenter size-full wp-image-2525\" alt=\"Wiring a LED to Raspberry Pi GPIO Pin 18\" srcset=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED.png 1608w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED-300x185.png 300w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED-768x473.png 768w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED-1024x630.png 1024w, https:\/\/www.robertprice.co.uk\/robblog\/assets\/Pi-LED-1200x739.png 1200w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><\/p>\n<p>Connect the LED to GPIO pin 18, along with a 330ohm resistor, to GND. We need the resistor to limit the current through the LED otherwise it could burn out.<\/p>\n<p>That&#8217;s the LED attached to Raspberry Pi, now we need to control it with PHP code.<\/p>\n<h2>PHP LED Control Code<\/h2>\n<p>Before we can write any PHP, we need to enable the gpio module on the Raspberry Pi.<\/p>\n<p><code>sudo modprobe w1-gpio<\/code><\/p>\n<p>We need a third party PHP library to be able to talk to the GPIO pins. I&#8217;m using <a href=\"https:\/\/github.com\/ronanguilloux\/php-gpio\">php-gpio<\/a>.<\/p>\n<p>We need to install this with <a href=\"https:\/\/getcomposer.org\/\">Composer<\/a> before we can use it.<\/p>\n<pre>\r\nget http:\/\/getcomposer.org\/composer.phar\r\nphp composer.phar require ronanguilloux\/php-gpio\r\n<\/pre>\n<p>We can now start our PHP code by using the autoloader to bring in the GPIO library.<\/p>\n<pre>\r\nrequire 'vendor\/autoload.php';\r\n\r\nuse PhpGpio\\Gpio;\r\n$gpio = new GPIO();\r\n<\/pre>\n<p>Now to control the LED we need to make sure GPIO pin 18 is set to &#8220;out&#8221;.<\/p>\n<pre>\r\n$gpio-&gt;setup(18, 'out');\r\n<\/pre>\n<p>To turn the LED on we write the value &#8220;1&#8221; to pin 18.<\/p>\n<pre>\r\n$gpio-&gt;output(18, 1);\r\n<\/pre>\n<p>To turn the LED off we write the value &#8220;0&#8221; to pin 18.<\/p>\n<pre>\r\n$gpio-&gt;output(18, 0);\r\n<\/pre>\n<p>Finally, we need to make sure we clean up after ourselves.<\/p>\n<pre>\r\n$gpio-&gt;unexportAll();\r\n<\/pre>\n<h2>The Final Code<\/h2>\n<p>Bringing this all together, we can write a simple PHP script to turn the LED on, wait 1 second, and turn it off again.<\/p>\n<pre>\r\n&lt;?php\r\n\r\nrequire 'vendor\/autoload.php';\r\n\r\nuse PhpGpio\\Gpio;\r\n$gpio = new GPIO();\r\n$gpio-&gt;setup(18, 'out');\r\n\r\necho \"LED on\\n\";\r\n$gpio-&gt;output(18, 1);\r\n\r\nsleep(1);\r\n\r\necho \"LED off\\n\";\r\n$gpio-&gt;output(18, 0);\r\n\r\n$gpio-&gt;unexportAll();\r\n<\/pre>\n<p>The final code needs to be run as root to be able to access gpio.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to make a LED light up using PHP on a Raspberry Pi. Most of the examples I&#8217;ve seen are for Python, so I wanted to see if was possible or not. It is actually pretty easy to do, so I thought I&#8217;d share my work. The first thing to do is to wire &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Controlling a LED on a Raspberry Pi with PHP&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2531,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[84],"tags":[87,86,50,85],"class_list":["post-2523","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hardware","tag-gpio","tag-led","tag-php","tag-raspberry-pi"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Controlling a LED on a Raspberry Pi with PHP - Robert Price<\/title>\n<meta name=\"description\" content=\"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.\" \/>\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\/controlling-a-led-on-a-raspberry-pi-with-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Controlling a LED on a Raspberry Pi with PHP - Robert Price\" \/>\n<meta property=\"og:description\" content=\"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-01T08:44:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-01T15:30:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"858\" \/>\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\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Controlling a LED on a Raspberry Pi with PHP\",\"datePublished\":\"2017-06-01T08:44:08+00:00\",\"dateModified\":\"2017-06-01T15:30:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/\"},\"wordCount\":289,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/LED.jpg\",\"keywords\":[\"GPIO\",\"LED\",\"PHP\",\"Raspberry Pi\"],\"articleSection\":[\"Hardware\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/\",\"name\":\"Controlling a LED 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\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/LED.jpg\",\"datePublished\":\"2017-06-01T08:44:08+00:00\",\"dateModified\":\"2017-06-01T15:30:53+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/LED.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/LED.jpg\",\"width\":1200,\"height\":858,\"caption\":\"LED on a breadboard\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/controlling-a-led-on-a-raspberry-pi-with-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Controlling a LED 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":"Controlling a LED on a Raspberry Pi with PHP - Robert Price","description":"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.","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\/controlling-a-led-on-a-raspberry-pi-with-php\/","og_locale":"en_GB","og_type":"article","og_title":"Controlling a LED on a Raspberry Pi with PHP - Robert Price","og_description":"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/","og_site_name":"Robert Price","article_published_time":"2017-06-01T08:44:08+00:00","article_modified_time":"2017-06-01T15:30:53+00:00","og_image":[{"width":1200,"height":858,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.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\/controlling-a-led-on-a-raspberry-pi-with-php\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Controlling a LED on a Raspberry Pi with PHP","datePublished":"2017-06-01T08:44:08+00:00","dateModified":"2017-06-01T15:30:53+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/"},"wordCount":289,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.jpg","keywords":["GPIO","LED","PHP","Raspberry Pi"],"articleSection":["Hardware"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/","name":"Controlling a LED 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\/controlling-a-led-on-a-raspberry-pi-with-php\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.jpg","datePublished":"2017-06-01T08:44:08+00:00","dateModified":"2017-06-01T15:30:53+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"Using PHP to turn on and turn off a LED on a Raspberry Pi. Full working example code and wiring details.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/LED.jpg","width":1200,"height":858,"caption":"LED on a breadboard"},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/controlling-a-led-on-a-raspberry-pi-with-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Controlling a LED 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\/2523","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=2523"}],"version-history":[{"count":6,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2523\/revisions"}],"predecessor-version":[{"id":2532,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2523\/revisions\/2532"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2531"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}