The Raspberry Pi is an amazing 35 dollars mini-computer. It allows you to do everything you could do with a regular Linux computer (Connecting to the internet, watching videos, launching applications, ...) but also to interact with the world surrounding it, just like an Arduino. That's why I qualify it as a mix between a computer and a micro-controller.
That's also why I chose it for this project. I'm going to show you how to control LEDs with your Raspberry Pi. Firstly directly from the Raspberry Pi itself, then from any device in your house like your Smartphone or your tablet.

Here is a very good example of what you can achieve after reading this Instructable: http://www.instructables.com/id/Web-Controlled-8-Channel-Powerstrip/. Thanks to Rleddington for his amazing project.

Update: French version here

Step 1: The electronic part

     The electronic part is nothing special, it's only 8 LEDs with their protecting resistor. The only hard part is to connect the LEDs to the good pin on the Raspberry Pi. Indeed, I'll be using later a library called Wiring Pi made by Gordon Henderson (You can visit his website at: http://wiringpi.com/) and the pin's numbers used in this library aren't the same than the ones on the Raspberry Pi. See this page for matches: http://wiringpi.com/pins/ (just be careful about your Raspberry Pi revision, the pinout isn't exactly the same). I'll be using Wiring pins 0 to 7.
     Concerning the resistors, they should be 270Ω but since I don't have this precise value, I'm using 560Ω resistors (LEDs are just less bright).
     Finally, I've made two schematics to make it simpler. The first one (with the complete Raspberry Pi) is showing you the real pins as they are shown on the board. The second one is a simplified version, it's showing you only the useful pins and their matches in the Wiring Pi library (GPIO Wiring number/Actual number on the board).
<p>Hi. </p><p>This is by far the best tutorial for controlling the raspberry gpio and the Pi over the web. I have been playing with the raspberry for a month or so looking for a tutorial. Thank you so much for doing this. </p>
hey I have been buiding pages and stuff for my raspberry pi for a little while now, and I feel I know it decent enough,but I have tiptoed around the javascript, avoiding it like it's the plague. though I downloaded your example, and it looks very complicated. do you think you can run through each line of code and explain what it does? I know the basics of javascript, but I just can't follow your code.
<p>Hello, i have managed to get all of your code working, but i cannot figure out what pins i should be connecting to. I have the Raspberry PI Model B, &amp; i think its the rev 1. Could you help please as im getting confused as an image i found said gpio 27 but i cannot find that gpio and only have 26 pins.</p><p>Also ever since i have restarted my pi i cannot access the index.php page from my browser via the ip address, do i have to manually start the service again or can i set it to start automaticaly?</p><p>Thank You</p>
Hi!<br> Links between pin's number and name are quiet confused. See this&nbsp;<a href="http://wiringpi.com/pins/" rel="nofollow">http://wiringpi.com/pins/</a>&nbsp;page where everything is shown. Also you can easily choose and control other pins. The pinout is in one of the steps.<br> Try to see if the apache service is running (google it to find the command), if not start it again and see if it can be ran at every start-up, if yes the issue is somewhere else. In any case, I'm not very good with linux commands so google you problem and if no solution is working. Simply re-install apache.<br> <br> Hope I Helped,<br> TheFreeElectron
<p>Hi thank you for your reply i have now managed to get all the pins correct and working, BUT when clicking the buttons they are the opposite way round, when the button shows as on (green) it is actually off :/ I have looked at the code and tried editing it as well as changing the numbers from 1 - 8 rather than 0 - 7 and then restarted the pi. But nothing seems to of changed or updated.</p><p>Do i have to put something on the command line to update a file or rebuild it at all? Any help would be great.</p><p>Thank You</p>
<p>No problem! <br>I actually never had a such issue but the more you know about what's happening and the more likely you'll find the solution. The first way is to use the browser's developer's mode where you can see what the server gets and send back. It will tell you if the issue is somewhere on the server's side or rather on the client's one. </p><p>Normally, the only commands you need to type are the one written in this 'Ible. Also do not forget you can begin with a fresh start (re-installing the OS) so you should no longer have issues (this is a kind of last stand :)</p><p>Hope I Helped,<br>TheFreeElectron</p>
<p>Hi thank you for the reply, i now have everything working fine :) Is there any way i can change the button numbers to go from 1 - 8 ? And also what would i need to do to add two buttons, one for all on and one for all off?</p><p>Thanks</p>
<p>Glad to know it works!<br>Buttons are actually images so simply use paint or gimp to modify them.<br>For the two other buttons, add two of them to the index.php page then in the javascript and finally add two new functions to the gpio.php page; one switching everything on, the other off. Also, since the release of the 'Ible, I've successfully put the 8 (or ten buttons) in an array which makes the javascript MUCH cleaner so give it a try. ;)</p>
<p>I have figured out that the numbers are in the images. But what i wanted to do was create a table in the index.php file and then call each button seperately, is this possible? As i am new to JS i have no idea, i have tried playing around with it but having no luck. By what i have gathered it just calls all the buttons over by one lot. I have added new images and changed it so i only have the two now, on &amp; off. Which works. </p><p>Any ideas?</p>
<p>Of course it is possible to call each button separately. If you really have 8 buttons, each of them has an id, a &quot;name&quot; by which you can call and use them in the JavaScript. I've never worked with DW or any soft like that so I cannot really help you in finding these id's.</p><p>I attached an &quot;updated&quot; version of the scripts to the comment, try to see if you can inspire from them to solve your problem.</p><p>Hope I Helped,<br>TheFreeElectron</p>
<p>Is there a possibility to upload the file once more? I'm trying to make a button that changes on GPIO pin 24 value. </p>
<p>Thank you for the reply, is there any chance you can email me the files please as for some reason it will not let me open them to save them.</p><p>callum.payne@live.co.uk</p><p>Thank You</p>
<p>Are you sure you cannot save them? I have absolutely no problem opening or saving them with &quot;save link as&quot; or copy/paste. What kind of error do you get?</p>
<p>I try to download the php file and i get forbiden sa i would like that layout also </p>
<p>I followed these instructions exactly...but when I navigate to the site I can only turn a pin on, not off. I can refresh the page, but whatever pins I turned on are stuck on until I reboot my Pi. The only difference in my setup is that I am using a Pi 2. Is this still supposed to work, or is it incompatible with the Pi 2?</p>
<p>After tinkering with the PHP I managed to get it to work....sometimes the picture doesn't update when I click it, but it still switches the pins which is all I really care about.</p><p>I have been an expert with HTML and MS command prompt for years, but am somewhat of a noob with PHP and Unix command line</p>
<p>Thank you!</p>
Hey nice project!!! <br>If I am using port 1, where should I make the changes. <br>I am using only ine LED. <br>I am a not a pro. <br>THNX!!!
<p>Hello, i am looking for some help. I started using this tutorial a while ago. And have made a few mods on the website. BUT i was wondering if somebody could help with abit of code. </p><p>The website fully works, but if i turn a light on and either refresh the site or open it on a different device.without turning the light off. It site on the other device shows as being off when its really on.</p><p>I am wanting the site to grab the state of the lights as either on or off on the page loading.</p><p>HOPE SOMEBODY CAN HELP</p><p>I can provide the code i have if somebody can help me out.</p>
<p>great example for learning, just what I wanted to get started.</p><p>In script.js for the conditional inputs why have you used 'if ( !(data.localeCompare(&quot;0&quot;)))' and not simply if (data==0)?</p><p>looking up localeCompare it seems to be more for comparing strings in alphabetical order?</p>
<p>Hello, I used your project for your purposes.<br> It is, however, one drawback, with some users do not refresh buttons.<br> In the user that switches actually running, while in another which has an open side does not.<br> I've added a page refresh every 10 seconds.<br> Question for you.<br> One of the keys I'd like to do as a momentary - it has something to attach for 2 seconds and then turn off, the state of the button to download from one of the input pin for example, electric roller shutters 23. I have you run a garage inpulsem 2 seconds, after opening signal from the reed switch input will be forwarded to pin 23 having an open state for the button to change to green.<br> Your solution secured and password encrypted https additionally joined CRON schedule.<br><br> Litter a modified version of my script action.<br> I changed the settings but does not work as I want.<br><br> Greetings<br><br> Irek from Polish.</p>
<p>Hello, I used your project for your purposes.<br>It is, however, one drawback, with some users do not refresh buttons.<br>In the user who tunes actually running, while in another that is not open side.<br>I've added a page refresh every 10 seconds.<br>question for you.<br>One <br> of the keys would do as temporary - it has something to attach for 2 <br>seconds and then turn off, the state of the button to download from one <br>of the input pin for example, 23. I have an electric garage door blind <br>you run one inpulsem 2 seconds, after opening signal from the reed will <br>be forwarded to the input pin 23 having an open state for the change to <br>green.<br>Your solution zbezpieczyłem szfrowanym password and https additionally joined CRON schedule.<br><br>Litter a modified version of my script action.<br>I changed the settings but does not work as I want.<br><br>Regards<br><br>Irek from Polish.</p>
<p>Could this code be modified to have one &quot;switch&quot; control multiple relays?</p>
<p>Hi Guys, and Gals, </p><p>Could you please help me, I need only one button. I know nothing about coding, just enough to mix few lines together, yet I can't seem to get only one button working... I'd appreciate it greatly! - Great tutorial, helped me a-lot! </p><p>Chris</p>
<p>hello , I want to display room temp on the web interface , is it possible ? </p>
<p>hello, could you please list he components needed for this project. i had be awesome if you could also direct us to the retail site to purchase them</p>
<p>THANKS SO SO Much for this tutorial...<br>I have used it on PI and also on SainSmart iMatic V2 Kit Wireless... And had to change the code a bit and its working perfect.. With out this tutorial i would not have done it.. Thanks ALOT...<br><br>If some one want the code for controlling SainSmart iMatic V2 Kit Wireless just tell ask...<br>Regards<br>T</p>
<p>Happy for you! Have fun! ;)</p>
<p>Thank you for a nice tutorial and web interface but i have a question. I need to have the relays start in the on state instead of the off state. </p>
<p>Hello,<br> <br>Unfortunately your program is wrong, your use of <br>the AJAX function is totally false.<br> <br>See French Correction:</p><p><a href="http://forums.framboise314.fr/viewtopic.php?f=71&t=1117" rel="nofollow">http://forums.framboise314.fr/viewtopic.php?f=71&amp;t=1117</a></p><p>Bruno</p><p><a href="http://tsin.langevin-la-seyne.fr/SIN/" rel="nofollow">http://tsin.langevin-la-seyne.fr/SIN/</a></p>
<p>Hello,</p><p>Unfortunately, correcting the code was easy as pie. Though, I used another simpler and shorter version made for a similar project and updated the link.</p>
<p>$status = array (0, 0, 0, 0, 0, 0, 0, 0);</p><p> for ($i = 0; $i &lt; count($status); $i++) {</p><p>system(&quot;gpio mode &quot;.$i.&quot; out&quot;);</p><p>exec (&quot;gpio read &quot;.$i, $status[$i], $return );</p><p>//if off</p><p>if ($status[$i][0] == 0 ) {</p><p>echo (&quot;&lt;img id='button_&quot;.$i.&quot;' src='data/img/red/red_&quot;.$i.&quot;.jpg' alt='off'/&gt;&quot;);</p><p>}</p><p>Can you explain the code? What Does the exec command uses the variables $ return and $status[$i]? $ status [$ i] [0], why did you write &quot;[&quot; $ i &quot;] [0]&quot;?</p>
<p>exec puts the terminal's response in the $status[$i] variable and transforms it into an array. So adding [0] will select the first case which is what we want. Code has been updated btw.</p>
<p>I used this guide along with several others and hooked the GPIO up to a relay and a remote control to remotely control my lights from my tablet, awesome guide. Thank you so much for it, I originally made a seperate python script, then a bash script with wiring pi to try and get it to work, but none of these would have worked, so I just used a PHP script in the end, it was the first time I ever looked at PHP, but now it works wonderfully.</p><p>Thank you so much! :)</p>
<p>I also tried all these techniques before this! ;)<br>The code has been updated btw, some corrections and bug fixes.</p>
<p>thank you beutiful informative tutorial. </p><p>First tutorial I have made with web server.</p><p>I have a question. </p><p>What do I have to change in the ''script.js'', ''index.php'' and ''gpio.php'' to use actual pin number instead of the wiring pi numbering. </p><p>Did use in shell the next commands succesfully</p><p>''gpio mode -g write 4 out''</p><p>''gpio -g write 4 1''</p><p>because the wiring pin number 7 is my gpio pin 4 I have succesfully tested the web page with button 7. But I would like to use the otter pins with gpio pin numbering</p><p>Hope you can help me thank you in advance :)</p>
<p>Glad you made it! ;)<br>If you want to change the pin numbering, the easiest solution is to create an array in gpio.php. It will contain the match between the default button's numbers (0 to 7) and the pins you wanna use (whatever you want but be careful about pin numbering and the name you're using). Then you use the array's value instead of the default value (array[$pin] instead of $pin). The Source code has been updated btw.<br><br>Hope I Helped,<br>TheFreeElectron</p>
<p>Hello!</p><p>Thank you for your (very) informative tutorial. I am currently thinking about modifying your solution to work &quot;in reverse&quot; i.e., to check whether a particular GPIO input has been closed (shorted to GND) or not, and display that fact via the WWW interface. Will simply changing the GPIO mode from &quot;out&quot; to &quot;in&quot; work for me?</p><p>Thanks in advance!</p>
<p>Hi!</p><p>Thank you!</p><p>You may need to adapt the code too, changing the mode from &quot;out&quot; to &quot;in&quot; will transform your 5V output into a &quot;button&quot; so you can use the &quot;gpio read&quot; command to check whether it's on or off. Also you'll have to use a refresh function in the JS so that pin are updated every x seconds. Finally, having clickable buttons isn't useful in your case. So lot of modifications ahead! ;)</p><p>Hope I Helped,<br>TheFreeElectron</p>
<p>Thanks for posting this article! I have a question.I am thinking to make LED to blink when I push the button. Do you know how I can give a delay on php or js so that LED can have some time when toggling between On and Off?</p><p>Thanks!</p>
<p>You have three solutions:<br>*In Js, you write a function using the change_pin funct, wait a bit, use the change_pin again, wait, then repeat. This will however rely on many parameters (connection, charge, ..) that will make the blinking unregular.<br>*In PHP, Do the same but directly with system or exec. The script can be executed several times simultaneously but this may give weird results.<br>*Create the little Daemon, I described in many other comments so you precisely control what's happening.<br><br>Hope I Helped,<br>TheFreeElectron</p>
<p>Hi TheFreeElectron!</p><p>Great information you have here. Plans on putting this to use in my home project to control fans and lights.</p><p>What Pi would you recommend as there are several different kinds out there.</p><p>Also like the interface that <a href="http://www.instructables.com/member/enggsam/" rel="nofollow">enggsam</a> has. Would you mind sharing that with me?</p><p>Thanks in advance!</p>
<p>Raspberry Pi Model B+ works fine. The snapshot shown is of the code running on a B+</p>
<p>Awesome. Thanks, enggsam!</p>
<p>Hi Wxmanbna!<br>Thank you! And it looks you have a great project there!<br>I don't think there is one to recommend, any of them since the model B 512MB should work but you better buy the latest version which is actually twice more powerful. So you won't be limited in any other project.<br>Concerning my graphics, there are all in the project's file you can download in one of the last steps. For Enggsam, send him a PM. Basically he just changed the pictures and HTML.</p>
<p>Thank you. I appreciate the information. :)</p>
<p>perfectly working, thanks a lot :)</p>

About This Instructable