loading

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>Nice instructable, base on your instruction, i made remote light switch through phone.</p><p>Anyone interested this project, please visit my blog: </p><p>http://engineer2you.blogspot.com/2016/06/raspberry-pi-remote-light-switch.html</p>
<p>Have been trying to setup a web interface for the past few days. Found a few but none of them really worked for some or the other reason. This is the only one which worked the first time through! Amazing.Thank you so much.</p>
<p>hi, nice project.. i want to ask. how to add password to active each button? how the about the code... </p>
<p>I have never made this but the information on this tutorial is good. Useful and is in my bookmarks is my slow learning journey that is raspberry pi.</p>
<p>Ah, thank you sooo much. It is an excellent tutorial and rather easy to tweak. Thank you!</p>
<p>wonderful work !!! keep up the good work. I've managed to make it work on orange pi pc too!!!</p>
<p>Fantastic! Worked like a charm. Minor correction to the text...&quot;<em>sudo chown -R www-data /var/www&quot; </em>will probably work better with &quot;<em>chgrp</em>&quot; instead of &quot;<em>chown</em>&quot; to change the group ownership. I'll be using this to help teach young kids getting started with the RPi, although I have a bit to learn up on the Apache/PHP side of things! Thanks much!</p>
<p>Thanks alot bro! Its a good tutorial for beginner like me :P</p>
<p>Hello guys I'm asking how could I put the pictures in the rasberry I have difficulties doing this ? </p><p>would you help me? I will appreciate it :)</p>
<p>use filezilla and ftp to your raspi, very easy and effective</p>
<p>Hi , is it possible to automatic refresh and password protecting ?</p>
hi <br>does it works on raspberrry pi 2 ?
<p>Works on Pi 2 and 3b</p>
You may need to make some minor changes but I think yes.
<p>Hi, a great project, I got it working first time. However the pins I want to you are different.</p><p>I want to change to GPIO pins 17,27,22,5,6,13 but I cannot see how to change the pins in your scrips.</p><p>Any help would be very welcome.</p>
<p>I was really hoping he'd answer this. It was also asked before and it was stated that it is already in the instructions how to. Really though, all that is in the instructions was how to wire your GPIO to pins 0 -7, not how to re-wire your existing project to those pins... </p><p>I think question could be better asked is how to alter from pins 0-7 so those of us not starting from scratch can use the pins already in use. </p>
Hi!<br><br>I think I already answered this question in another comment but if I didn't then here's how you can use whatever pins you want:<br>-Use the same code<br>-In gpio.php add an array making the link between numbers 0 to 7 and the pins' number you want to use. For instance [5,6,7,8...,13] if you want to use pins 5 to 13. It doesn't need to be in a particular order. That way, the user still asks for pin 0 which will, due to the array, corresponds to its first element. Here it would be 5.<br><br>Hope I Helped,<br>TheFreeElectron
<p>Thank you for answering! So would an example of that be near the top?</p><p>0 = 5</p><p>1 = 6</p><p>etc?</p><p>Thanks.</p>
<p>That's it. So instead of putting $pin (from 0 to 7) in the exec function, you put $array[ $pin ] (array for 5 to 13)</p>
<p>Sorry if I am not getting this but here is how my exec line reads in gpio.php</p><p>exec (&quot;gpio read &quot;.$pic, $status, $return );</p><p>I don't get at all what you have said to swap out into this line or where 0-7 are getting pulled from this line to begin with? Here is the entire gpio.php file if you could replace in here exactly how to add these random pins for easy example: 0,2,3,5,8,9,12,13.</p><p>Thanks again for your time~!</p><p>&lt;?php</p><p>//TheFreeElectron 2015, <a href="http://www.instructables.com/member/TheFreeElectron/" rel="nofollow"> http://www.instructables.com/member/TheFreeElectr...</a></p><p>//This page is requested by the JavaScript, it updates the pin's status and then print it</p><p>//Getting and using values</p><p>if (isset ( $_GET[&quot;pic&quot;] )) {</p><p> $pic = strip_tags ($_GET[&quot;pic&quot;]);</p><p> //test if value is a number</p><p> if ( (is_numeric($pic)) &amp;&amp; ($pic &lt;= 7) &amp;&amp; ($pic &gt;= 0) ) {</p><p> //set the gpio's mode to output</p><p> system(&quot;gpio mode &quot;.$pic.&quot; out&quot;);</p><p> //reading pin's status</p><p> exec (&quot;gpio read &quot;.$pic, $status, $return );</p><p> //set the gpio to high/low</p><p> if ($status[0] == &quot;0&quot; ) { $status[0] = &quot;1&quot;; }</p><p> else if ($status[0] == &quot;1&quot; ) { $status[0] = &quot;0&quot;; }</p><p> system(&quot;gpio write &quot;.$pic.&quot; &quot;.$status[0] );</p><p> //reading pin's status</p><p> exec (&quot;gpio read &quot;.$pic, $status, $return );</p><p> //print it to the client on the response</p><p> echo($status[0]);</p><p> }</p><p> else { echo (&quot;fail&quot;); }</p><p>} //print fail if cannot use values</p><p>else { echo (&quot;fail&quot;); }</p><p>?&gt;</p>
<p>At the beginning of the code, create $array = [5,...,13]<br>Then in the system and exec commands, replace $pic by $array[$pic]</p>
<p>I'm not sure I am understanding how to use various GPIO pins that are not in any sequence... </p><p>So $array = [5,13,19,18,22] would be OK? and $pic is a mystery.. but the same?</p>
<p>Ok, I did exactly as you said and now the page errors if you try to click on a on/off button. This is to the point of going to look for another instructable. </p><p>I edited to what you said as you can see below. I am not sure why you would not just post a complete example to get this simple question out of the way.</p><p>&lt;?php</p><p>//TheFreeElectron 2015, <a href="http://www.instructables.com/member/TheFreeElectron/" rel="nofollow"> http://www.instructables.com/member/TheFreeElectr...</a></p><p>//This page is requested by the JavaScript, it updates the pin's status and then print it</p><p>//Getting and using values</p><p>if (isset ( $_GET[&quot;pic&quot;] )) {</p><p>$pic = strip_tags ($_GET[&quot;pic&quot;]);</p><p>$array = [0,27,2,3,4,5,6,7]</p><p>//test if value is a number</p><p>if ( (is_numeric($pic)) &amp;&amp; ($pic &lt;= 7) &amp;&amp; ($pic &gt;= 0) ) {</p><p>//set the gpio's mode to output</p><p>system(&quot;gpio mode &quot;.$array[$pic].&quot; out&quot;);</p><p>//reading pin's status</p><p>exec (&quot;gpio read &quot;.$array[$pic], $status, $return );</p><p>//set the gpio to high/low</p><p>if ($status[0] == &quot;0&quot; ) { $status[0] = &quot;1&quot;; }</p><p>else if ($status[0] == &quot;1&quot; ) { $status[0] = &quot;0&quot;; }</p><p>system(&quot;gpio write &quot;.$array[$pic].&quot; &quot;.$status[0] );</p><p>//reading pin's status</p><p>exec (&quot;gpio read &quot;.$array[$pic], $status, $return );</p><p>//print it to the client on the response</p><p>echo($status[0]);</p><p>}</p><p>else { echo (&quot;fail&quot;); }</p><p>} //print fail if cannot use values</p><p>else { echo (&quot;fail&quot;); }</p><p>?&gt;</p>
<p>Try to ask directly for /gpio.php?pic=0 in your browser to see what it gives you. In function of that you should be able to troubleshoot the error: print variables, try separate commands, etc, simply do what a programmer would do.<br><br>If you want to look for another instructable, you are obviously absolutely free.</p>
<p>more headache than it's worth.. thank you anyway. Really too bad you couldn't have just answered differently to solve vs idk what. How hard would it have been for you to just look at the code I posted and corrected it? If we were coders we likely wouldn't be on this site and if you just confuse people you are not helping them learn.. </p>
<p>You really need to take a look at yourself. This is an excellent tutorial, and the author has been extremely helpful to you and many others.</p><p>Helping them learn != doing it for them. </p><p>Be nice!</p>
<p>hi, i'm a beginner, i have a problem where the pictures don't appear</p><p>I was confused by putting this files (src='data/img/red/red_&quot;.$i.&quot;.jpg') on your source code. where i should put this file on raspberry pi 3? i use raspberry pi 3.</p><p>I've put this file in (src='/home/pi/data/img/red/red_&quot;.$i.&quot;.jpg'). but didn't seem his image. I thought i misplaced this file.</p><p>I appreciate any help</p>
<p>hello, thanks for the tutorial</p><p>i have a problem with change the pict, the pict can't show why i change the pict. i have already change the name of pict with same than your code. but the pict what i want don't show. can you help me for fix the problem ?</p><p>the function of controll work perfect, but the pict won't show correct</p>
<p>Look im really new and would love if someone could help me out, how do i get the pictures in the raspberry pi because i cannot get them there.For that matter im not sure i put the codes in the right place could someone please walk me through this .I'm new like i said I created a file for index.php,gpio.php,and script.js in /var/www/ as for the data i cannot get it in there.Mine looks similiar to yours</p>
<p>nevermind i got it</p>
<p>Hello,</p><p>Maybe you forgot to update the access rights of the pic? If you didn't, Apache cannot use and display these pictures.</p><p>Hope I helped,<br>TheFreeElectron</p>
thanks for reply me.<br><br>i found the problem, the problem is i forgot change the permission pict. <br><br>when i change the permission, the pict change correctly :D
Thank you so much! I was wondering how to make it so something is active as long as I am pressing down a button on the web page ui. I want to make a motor move forward as long as I am holding the button down and stop when I let it go.
<p>Hello, i followed this tutorial and I just ran into a &quot;problem.&quot; The server works perfectly when I access it from my laptop. The problem is that when I try to access it from my android phone it doe not load and gives me the error &quot;This webpage is not available.&quot; Can anybody help me with this??? On my phone browser I am writing my pi's ip address so im just writing &quot;169.254.11.15&quot; on google chrome.</p>
169.x.x.x is an apipa address. It means it hasn't picked up an IP address from dhcp. My guess is the pi has a couple of nic's, maybe one wifi and one wired? Chances are you're using the wrong IP. Did you figure this out in the end?
<p>Great project! I have one problem with my set up. It functions properly the first time clicking each button, however if I try to click any button the second time it does not change state. Its like the gpio.php is not getting called the second time. Any Ideas?</p>
<p>Update: This appears to be an issue when using Internet Explorer 11. Works fine on my Iphone.</p>
<p>Hey guys!</p><p>A little update....</p><p>Whew! A month and a half waiting for 555 timers from china! Worth the 1.00 for 5 though IMO.....they cost around $7 locally.</p><p>The wiring is done, but I'm having a problem where the LED blinks when the relay is closed. Not sure if I have to change the config of the relay pins, then the code, or if theres something I can do to the 555 circuit. It's also very dim, so I'll have to power them from an external power supply. I used a 6v wallwart and they were bright when all 8 were on. The resistors used for the LED's are 220Ohm which were calculated for using 5v as the power source.</p><p>I'm just waiting for the spare time to sit down and finger it out.</p><p><strong>I JUST HAD A GREAT IDEA THAT MADE ME JUMP ONLINE RIGHT NOW</strong></p><p>I'd like to monitor how often each relay is left on.</p><p>Maybe even with the ability to view the actual times that it's on (12am-2pm etc...).</p><p>I daydreamed a bit further and thought how convenient it would be to be able to print out my usage for the pins every month to keep track of how long each device is powered on. </p><p>I'm not up to the task of trying to code it from scratch, but I'm sure I could find something on the net to piece this together. I'm sure I'll have to buy the RTC for the PI, right?</p><p>Anyways I mostly came on to show that I havent forgotten about u guys and that it's still a work in progress. I blame China for tyhe long delay :D</p>
<p>Your instructable is very very USEFUL!!!</p><p>but when i run my php file raspi server through web,their appears to be 404 url error(url no found)...even i used these commands(<em>sudo chown -R pi /var/www and </em></p><p><em><br></em></p><p><em>sudo chown -R www-data /var/www</em>&quot; ) but nothing happened!!!</p><p>what should i do ?</p>
<p>I found that when I moved the file to /var/www/html it worked. Hope it does for you.</p>
<p>Is this the solution for the no longer existing RS 232 and Parralelle port on modern PC/Laptop . Work is currently in Linux , the machine I used ( milling )is working on the RS232 or parrallel signals .</p>
<p>This was fantastic!! I'm turning on a tea kettle remotely with this and it works like a charm. I am controlling two outlets with this so I edited the code down to two options, no need for loops in this case (but in the future....). My question is, and I think it was asked earlier: If you turn on the LED, then refresh the page, the page doesnt initially read the value of the Pin. </p><p>I have two relays I am controlling, if I turn them on, they show on in the browser, then I refresh browser, they show off on the browser but are still on. They will turn off if I click the button, and then the cycle is properly rest and the buttons turn on and off.</p><p>I am trying to write up a javascript read function that reads the status of the pins when the page loads...no luck in the last 15 minutes or so but unless someone else figures it out, I will post when I'm done! This isnt a serious issue, just would be nice to have. great instructable!! oh... I did it!!</p>
<p>quite a simple solution... :) I just told GPIO via php to set all status to zero as first line...this way, no way any outlet could be accidentally forgotten or left in an unknown but truly &quot;On&quot; state </p>
Sorry but that is a rather ugly solution just imagine:<br>You and your spouse/dontknowwhat are using the same interface for controlling <br>Now you want to turn on the tv in the living room and she the one in the kitchen <br>(Pls dont critisize for PC... Its notnmeant to offend just can't think of a better example)<br>So when she opens the page she turns off you tv right?<br><br>I'll look into the code tomorrow and will present you my findings :)<br><br><br>
Yes, i had the same.problem. as i said both index.php and script.js have to be edited with the correct state (0 or 1) in order for it to work. Simple changing one file and not the other will result in the problem u are having. Coming from almost NO programming background, it was my projects Achilles heel u til it was solved.
<p>Thanks for posting this. It provides an easy, fast way of exposing the pi to the user and interfacing with the gpios. I can think of so many applications!</p>
<p>HEY!!!</p><p>One last quick question...</p><p>I am almost finished my enclosure! :)</p><p>I was wondering if it was possible to add a status led for when my relays are powered?</p><p>I am already using an 8CH relay with status LED's, but these will now be closed inside the case. I'd like to add LED's on the cover of the case, so I can see which relays are on without opening the cover...</p><p>Would I be correct to breakout the pin of the GPIO I wanted to control, and add an LED (with a corresponding resistor) like that??</p><p>I (tried) to draw what I'm trying to say in paint. You may want to grab some popcorn before viewing; it's quite entertaining... </p><p>Basically the RPi, pin for relay board, and led's anode would all connect in the same row, with the cathode and the relays &quot;-&quot; connecting in a row of its own on the protoboard. Any future LED's would be connected the same way.</p><p>IS THIS CORRECT? :S (thk u for your time)</p><p>I'll take some pics when everything is complete :)</p>
Hey!<br> <br> Here's a link for you about how you should connect your LED. The way you did it seems to be the good one&nbsp;<a href="http://forum.arduino.cc/index.php?topic=126629.0" rel="nofollow">http://forum.arduino.cc/index.php?topic=126629.0</a>&nbsp;(Although your drawing is as you said, entertaining... :p). Also, don't forget to connect the Rpi's GND to the relay board's GND.<br> Btw, try fritzing to draw your schematics, it's a very nice and free software.<br> <br> Why not publishing an 'Ible about your project later?<br> <br> Hope I helped,<br> TheFreeElectron
<p>Fritzing is like my long lost friend thk u!!!!!</p><p>I've decided to incorporate 555 timers to make the external status LED's blink when the circuits are closed.</p><p>I've gotten everything tested, just waiting for my parts to come in from China (a 555 timer locally is something like $5, I got 10 from China for under $2) to complete everything (finally).</p><p>Once finished I will post back with the completed project. </p><p>I never thought of making an 'ible but it's a great idea!!!! I've always wanted to contribute some way and it seems like a good place to start!</p><p>Thanks a million again!!! :D:D</p>
<p>What's that twisted blue wire connected to the RPi? I have an RPi 2 so the layout is different, but from looking at diagrams of the model B, I don't see anything that would plug in where it looks like that wire is connected.</p>