Simple and intuitive web interface for your Raspberry Pi

Picture of Simple and intuitive web interface for your Raspberry Pi
     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: Thanks to Rleddington for his amazing project.

Remove these adsRemove these ads by Signing Up

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: 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: (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).

Step 2: Installing and using the Wiring Pi library

     As said before, Wiring Pi is a library. It simplifies a lot using the Raspberry Pi GPIOs (one command instead a long process). It also means that you can use it in any of your C codes. However, we won't build and use a C program but the Gpio utility. It's a software made by Gordon and coming with the library. It allows you to control the GPIOs in a bash script or directly in a command line. Using this utility is however a lot slower than a C program.

     We first need to install it. Gordon himself is explaining it very well on his website: You just need to download it from GIT then to build it using the ./build command.
     You should now be able to use the Gpio utility, type the "gpio mode 0 out" command to test it out. If nothing special appears, everything's fine. Else, if the board is printing "command not found error" or something like that, be sure that you've followed the guide and build the library.

     Let's turn on and off the first LED (Wiring pin 0). You first need to set the pin as an output. Use the "gpio mode 0 out" command to do so. "0" is the wiring pin number and "out" simply stands for output. Now, use the "gpio write 0 1" command to turn on your LED. "0" is again the pin number and "1" is the status (1 for ON and 0 for OFF). If everything's fine you should see your LED shining. To turn it off, simply use the "gpio write 0 0" command.
     Just a little tip, if you want to use the actual pin number (GPIO-17) instead of the Wiring Pi number (0 is corresponding to GPIO-17), use the -g flag in your command. Ex: "gpio -g write 17 1" instead of "gpio write 0 1".

      There is also the "gpio read" command which allows you to read the pin's status. It may seems useless when the pin has been set as an output but it allows you to be sure of the pin's status when you can't see the LED. Using it is as simple as before, just type "gpio read 0" where "0" is the wiring pin number. The command is returning the pin's status (again 1 for ON and 0 for OFF).

     Finally the Wiring Pi library is containing a lot of other commands/functions but I'm not gonna cover them in this instructable since there are not useful here. See this page if you are more curious: (library's functions) and or the "man gpio" command for the Gpio utility.

     Now that you can use this utility, let's play a little bit with it. You can first, if it's not already the case, control remotely your Raspberry Pi with SSH. You can use Putty for Windows or ServerAuditor for your Smartphone. Then have fun with bash scripts such as this one which is turning on LEDs 0 to 7, waiting 2 seconds, then turning them off again:

#set mode to output
for i in 0 1 2 3 4 5 6 7;
   do gpio mode $i out;

#turn on LEDs 0 to 7
for i in 0 1 2 3 4 5 6 7;
    do gpio write $i 1;

#wait 2 seconds
sleep 2;

#turn LEDs off
for i in 0 1 2 3 4 5 6 7;
    do gpio write $i 0;

Step 3: Installing a web server then transferring your website to it

Picture of Installing a web server then transferring your website to it
     Controlling the LEDs remotely with SSH is pretty cool but the interface (console) isn't very user friendly and typing the commands every time is long and annoying. That's why we need a graphical interface for our project.
     Programming an app for each OS (IOS, Android, Windows phone, Mac, Linux, Windows,...) would be too long and would require to know a lot of different languages for nearly nothing. It would also require to do an application running on the Raspberry Pi. Making it this way would be overkill and time wasting.
     That's why a website is the best solution, it's compatible with all devices and you "only" need to know four languages: HTML (for the page's skeleton), CSS (page's style), PHP (interactions with the server) and JavaScript (interactions with the user).

     We indeed need to install a web server on the Raspberry Pi. In our case, we don't need a MySQL database, only a HTTP server and its PHP extension.
     After updating your Raspberry Pi with the "sudo apt-get update" command, type "sudo apt-get install apache2 php5 libapache2-mod-php5" to install Apache HTTP server and PHP5 extension. You can now test if your server is working by typing the IP of your Raspberry Pi in your browser. You should now see a "It works!" page with two other lines. If you don't, then check your board's IP, try re-installing Apache or rebooting your Raspberry Pi. This page is showing that your Apache server is working properly but not its PHP extension. To check it, navigate to your "/var/www/" directory by using the "cd /var/www/" command. If you use the "ls" command, you should have only one file named "index.html". This file corresponds to the "It works!" page. You can now delete it ("sudo rm index.html") and create another one called "index.php" (use "sudo nano index.php"). Then type the following text:


     After saving it using ^o (Ctrl + o), exit nano editor with ^x (Ctrl + x). Now if you refresh your browser, you should see a long page with lots of information about your server and PHP. If you don't, check the index.php file, try re-installing PHP or try to understand the error displayed instead of the page (Google it if necessary).

      If both pages were correctly displayed, then you now have a fully functional Apache/PHP server but using nano every time is annoying and not very comfortable. We indeed need to transfer files from your computer to your Raspberry Pi. You may want to install a FTP server but it isn't necessary, you can already transfer files using the SFTP protocol. All you need is an SFTP client on your computer. I'm personally using  WinSCP for Windows but there are Cyberduck for mac and Filezilla for Linux. If you try transferring files before reading what's next, you'll probably have issues such as "access refused" or "cannot write here". It's due to the fact that the user pi isn't owning the www directory. Indeed, if you try the "ls -l /var/www" command, you'll see that only root (the super user) is owning the www directory. You can (like I did) use the "sudo chown -R pi /var/www" command to change it or create a group named www-data in which you place the pi user then use the "sudo chown -R www-data /var/www" command. The -R flag is standing for recursive, it means that the user/group isn't owning only the directory itself but also everything inside (index.php as example).
     You now have your server ready to work and to receive web pages. Have fun with it if know HTML, CSS and PHP.

Step 4: Controlling the LEDs with PHP

Picture of Controlling the LEDs with PHP

We now have a web server and a library, let' put them together.
PHP stands for "PHP: Hypertext Preprocessor", It's a server side scripting language. It means that the PHP code is executed once (each time the page is requested) by the server and cannot be seen by the client. I used this language because it's the most popular (and that's the only one I know) but you have to know that they are other server side languages like Python, Ruby, Lua, Perl, ... However, I don't know if the functions we are gonna use have their equivalents in these languages.

Executing applications with a PHP code can be done with two different functions: exec (for execute) and system. Firstly, the "system" function. It takes two parameters: "system ( string $command, int $return_var )", as you guessed it, the first parameter is the command to execute and the second one is the returned status of the executed command. The second parameter isn't compulsory. You can use this function if you don't expect an answer from the command executed. Thus, you can use it if you need to execute "gpio mode 0 out" or "gpio write 0 1" commands. Example:

     system ( "gpio mode 0 out" );
     system ( "gpio write 0 1" );

Then, the "exec" function. This function is making exactly the same work than "system" but it reads and stores what the command printed. It takes three parameters: "exec ( string $command, array $output, int $return_var )", again $command and $return_var are the same parameters and the only difference is the $output array. As it's name says it will store the command's output in an array. Thus, you can use this function if you need what the command prints like with the "gpio read 0" command. Example:

     exec ( "gpio read 0", $status );
     print_r ( $status );

You can now execute nearly whatever command you want but let's make a little PHP example to practice: We will turn on LEDs 0 to 7, then wait 2 seconds, then turn them off. Just like we did with the bash script. Edit the index.php file with the following code:

$status = array ( 0 );
//set pins mode to output
for ($i = 0; $i <= 7; $i ) {
     system ( "gpio mode ".$i." out" );
//turns on the LEDs
for ($i = 0; $i <= 7; $i ) {
     system ( "gpio write ".$i." 1" );
//reads and prints the LEDs status
for ($i = 0; $i <= 7; $i ) {
     exec ( "gpio read ".$i, $status );
     echo ( $status[0] );
//waits 2 seconds
sleep ( 2 );
//turns off the LEDs
for ($i = 0; $i <= 7; $i ) {
     system ( "gpio write ".$i." 0" );

Step 5: Making the interface

We can now control our Raspberry Pi with simple PHP scripts but there is no interaction with the user and thereby we can't choose the LED to turn on/off. Let's make the interface!
It's composed of pictures I've found on Google images (search for "on/off button"). One was green and the other one red, I just added the number using The Gimp. Each picture/button is corresponding to its LED, so if you click on one of these, the corresponding LED will be turned on/off and the picture will be changed to its green/red version. The page's skeleton will be made with HTML, the server interactions and page's generation with PHP and at last JavaScript to manage interactions with the user and page's animation. If you don't know, JavaScript is a client side language and unlike PHP, it's executed not once, but continuously by your browser. That's why you can change the page's look without reloading it or accessing to an other. If you are wondering why I spoke about CSS before, it's just because we need it for some style and page-layout like the the black background. I didn't make a full .css file because it wasn't necessary here.
We first need an "index.php" file (extension is .php and not .html cause we will use PHP code, it helps the server to know if there is PHP to execute before sending the generated page). This page is the main page containing the 8 buttons. These buttons are first generated with a "exec ( "gpio read ".$i, $output );" in a for loop. Then we need to detect when the user is clicking on one of these buttons. That's where the JavaScript is useful, I put it in a separate file called "script.js" but it's still included in index.php. The script is simply adding an event listener to all of the eight buttons and each time one of these is pressed, it uses a function which is asking for gpio.php, receiving the answer then returning it. Finally, in function of this, the JavaScript changes the button to red (for OFF) or to green (for ON). Now, the last page: gpio.php. It contains the PHP code to turn on/off the LEDs in function of what the JavaScript function sent. The user shouldn't normally ask for this precise page but there is one golden rule when creating websites and you should always remember this one: "NEVER TRUST THE USER". In other words, never think the user is always gonna do what you think he's gonna do. Thus, I added some securities at the begin of the PHP code like making sure the user gave a correct value and not a letter as example. I made a small diagram to sum up all this text.

You can download the full project directly on this website below.

Web.zip701 KB

Step 6: Conclusion and ideas of improvements

This small but long explained project was fun and I learned a lot. I hope you did the same. However, controlling LEDs isn't very useful. That's why what we made is rather a tool than a real project. Christmas is soon (about one and a half months from the day I wrote this instructable) so why not replacing LEDs by relays and controlling lights around your house. There are some pretty good relay boards for the Raspberry Pi on Ebay and more generally on the Internet. Alternatively, and if you're not scared about working on your house, you can even control your house's lights, garage door, coffee machine, heating system, ... The only limit is your imagination.
There are also a lot of possible improvements like changing the interface, adding more LEDs via a shift register, using vocal recognition, ... In addition, with PHP, you are not limited to gpio write or read. You can use the full Gpio utility and thus interact with other devices with UART or any other implemented protocol. You can also use PWM (Pulse Width Modification) to control servos, ...
Writing this Instructable and sharing my little knowledge was a great pleasure for me and I hope it was the same for you to read it. I tried to keep it simple but at the same time to teach the most possible. I didn't want to do a simple and dumb step by step: "download this code, run it, you're done". I think that something is useless to learn until you understand how it works or why you do this and not that. Let me know if you think it's the good way or if I should do it otherwise.

PS: This is my very first instructable and English is not my native language so if you have any comment, advice, suggestion, idea, ... Just let me know, I'll be glad to answer you and of course to learn.

1-40 of 163Next »
mikes137 days ago

Very informative instructable. I am working on a project where I need to control relays. I'm thinking this instructable covers about everything I want to do except I'd like to use tasker to send an http post to execute the command. The problem is I'm a terrible coder. Maybe you could point me in the right direction. I want to be able to send a http post that would turn on relay 1 then wait 1 second and turn on relay 2 for 2 seconds then turn both off and then wait for another command. Or send a different http post that would turn on relay 1 then wait 1 second and turn on relay 3 for 5 seconds then turn both off. Please help if you can. Thanks!

TheFreeElectron (author)  mikes137 days ago
Hi mike!
Thank you! to control relays, you can take a look at the project linked in the intro, it's directly using relays and wall plugs.
Concerning your sequence (turn on relay 1, wait...), you have several possibilities:
--You do it via the client (Tasker in your case) and ask it to send the appropriate request to the gpio.php page (since the index is just an interface for us humans ;) After, I've never used tasker so I have no idea if you can create a small script and execute it.
--You do it via the gpio.php page and replace the simple "gpio write command" by a longer batch script. However I don't know if it is possible to execute a full script in the same exec (if not, use multiple exec and sleep in php). This option being, I think, the simplest to put in place.
--You do it via a python daemon (or whatever language) but if not multi-threaded, your daemon will be blocked for the time of execution. That last option being the hardest to put in place but I described it more precisely in other comments.

These are different option you have, I let you choose. Google is also a very good friend, I'm sure that if you search for Rpi home automation you find even examples of code.

LukasG29 days ago

Hello FreeElectron. Very nice Tutorial. But I've got a problem. I can't switch my LEDs over the web interface. Wiring Pi, my webserver and php works. Also I think i set the right permissions. Where could be the problem? Can you help me please? Thank you very much

TheFreeElectron (author)  LukasG28 days ago
Hi Lukas,

Thank you! Concerning your issue, I can't tell a lot about it, if everything is working separately, It means the problem is happening between a system and another, between php and wiring Pi for instance. This way, the more you know about what is happening and the more chance you have to find the problem.Therefore, try several simple scripts and commands to test each connection between two systems, one by one, and if everything is still not working, think a little bit more time about it before restarting from zero. That's how I do in such a case.

That's all I can say thousand miles away, I hope you'll find the problem and also remember that google is men's best friend! :)


Thank you. I found the Problem. I hab run the lighttpd server in place of the apache2 server. I read on another site, a variant with lighttpd. I thought the lighttpd server runs, but there was something wrong which i don't know.

But thanks for your Help. With Apache2 Server I didn't get problems. It runs very good.

TheFreeElectron (author)  LukasG28 days ago

No Problem, I'm glad it now works for you!

Running Lighttpd would actually be a quiet good idea as purposed in another comment, since as its name says, it's lighter than Apache and therefore better for the memory and CPU restricted Rpi but I never tried it so can't tell. Also at our scale (max 10 requests per second), performances aren't so important so apache is still good.

Have fun with your Rpi!

So I really like what you are trying to do here. As I really am looking for functionality like this to interface with my already working Arduino system. But for someone who's never touched a Raspberry Pi it is a bit dense :/
Which would have been fine had I gone into it thinking I couldn't do it... But I thought it would have instructions for us simpletons. :/

TheFreeElectron (author)  Electrolight15 days ago
Thank you!
That's always hard to imagine how someone with less experience will understand your 'Ible and I tried to make it as simple as possible. Unfortunately I do not have time to rewrite all the 'Ible or at least to make it less "dense" as you said. And you are right on that point, every steps is teaching something quiet new and doing it rather quickly. So it is completely normal if you think (think is important) you cannot do it. But you are all wrong on that point. As I said in one of the first comments, begin with a simpler tutorial and as soon as you gained experience on the Rpi, come back here and you'll see, it's gonna be super easy! Me too, I had to learn before doing this project.

Concerning communicating with the Arduino, you'll have to do it via the serial port (USB) so in this case, the python daemon I described in other comments would be more appropriate.

Do not give up, have fun and keep going!  ;)

Hello again,

I re-read my comment and it seemed more critical than I intended. I am still really grateful for your contribution to the community. I have already bought my raspberry pi and it gets here tomorrow. So I am looking forward to doing some introductory tutorials to have fun and gain experience with it. Hopefully in the near future I can come back to your tutorial and walk right through it ^^.

And to communicate with arduino I can use usb? That never even crossed my mind but it sounds genious. I was ready to to interface with arduini using the digital I/o pins which sound less "clean" as a final solution.

Thanks again,

PS: Your name is awesome! ^^
TheFreeElectron (author)  Electrolight15 days ago

No problem, I was far from being offended. Personally, I began with Adafruit's tutorials and several 'Ibles/random web pages, especially on setting up a web-server.

I've never tested Serial communication on the Rpi but seen several tutorials. It would allow you to transfer data at a much better rate to the arduino and back from it, also, doing it gives you the possibility to do other tasks from simple calculation to recording in a file... And in that case, everything is more independent. It's a bit like:
Interface <-> Web server <-> [Sockets] <-> Python daemon <-> [Serial] <-> Arduino <-> Amazing

It may sound quiet hard, but becomes very easy when you understood the principle and know python (or any language that would work by the way)

I hope I helped and gave you an idea that will lead to an interesting project and maybe your own 'Ible! ;)

Ps: Thanks and your name's awesome too!

Thank you for this information. It works perfectly :)

Thank you, glad to know it worked!
KHOR25 days ago

sir,sorry,may i know that is it i need to transfer your file to raspberry pi and run all of the document in lxterminal?

TheFreeElectron (author)  KHOR24 days ago
You just need to place the www folder's content (after extracting into the server's www folder.

thanks, i have done my interface , but i cant turn on my led .... , is that any problem? my power supply to pi is 5v 2amp for model B+

TheFreeElectron (author)  KHOR21 days ago

no problem, did you try turning your led on directly via the command line? (eg: "gpio mode LED_PIN out" then "gpio write LED_PIN 1")There are 2 possibilities:

-This command is not working and the problem is coming or from the gpio soft or a wrong pin connection...

-This command works and the problem is further, are all the other php test scripts working? Also try your browser's developer's mode, it's often useful to understand what's happening.

That's all I can say with what you said.

Hope I Helped,

jopiezettie23 days ago

Nice tutorial. My Christmas lights are working remote controlled from all over the world.

Just a question/remark. I used your files. When I use IE, I only can switch it on/off only one time. after that the GPIO are not responding anymore, but the pictures on the screen are still changing.

TheFreeElectron (author)  jopiezettie23 days ago
Thank you!

Concerning your issue, It may depend on your IE version, older versions are sometimes not able to execute some functions that other browsers or newer versions of IE can use.
Also try to check the code, they are maybe some errors or things like that, the developer's mode in Chrome is really useful.

Hope I helped,
ThaddeusW26 days ago

Nice Tutorial. But a few words of caution:

I would avoid using wood in any electronics enclosure, especially one carrying mains voltage. It has a lower point of ignition and absorbs moisture. It might also be against code to have wood in any electrical enclosure. You can buy plastic or metal NEMA rated enclosures from manufactures such as Altech or Bud Industries. They are pretty cheap and can be purchased from a number of online retailers (Allied, Newark etc.) Also, the NEC wants the receptacles (sockets) mounted via the holes on the metal tabs on each end. You can do this from the inside using tapered flathead screws and a metal rail, perhaps a piece of aluminum angle, 15mm and 1.5-2mm thick (1/2 inch and 1/16 thick). The screws that come with the receptacles will do , they are 6-32 and you can purchase hardware for them at a hardware store quite easily. This way the face can be free of screw heads but you still have securely mounted receptacles that are up to code and considered safe for use.

I also would suggest using lighttpd. It is less bulky than Apache and better suited for embedded use cases like the memory constrained pi. It also handles PHP so no problems there.

TheFreeElectron (author)  ThaddeusW24 days ago
Hi ThaddeusW,
Thank You, concerning the enclosure, I really agree with you but, as I remember, I never spoke about a wooden enclosure in this project and even have a plastic enclosure for the Rpi (the top was removed to access the pins)
I also agree with you concerning lighttpd but I used Apache simply because of my habits and that this 'Ible is rather instructional than a real 24/7 running project. Still, in that last case, your option is better.
amaanu1 month ago

What if i want to control a 50w load?

TheFreeElectron (author)  amaanu1 month ago
The Raspberry Pi isn't powerfull enough, you better look at the project linked in the intro which is using relays, therefore allowing whatever load your relay can support.
Zipkin1 month ago



It work!!!!!

TheFreeElectron (author)  Zipkin1 month ago
No problem, glad to know you did it :)

You might want to have people install the g++ libraries. They were not installed for me so I was having compiling problems.

sudo apt-get install libc-dev g++ build-essential

I've never needed to execute that command but thank you for anybody that could need it.
RobertL51 month ago

Nicely done! I'm thinking about adding a crontab to the PHP code you provided. That way when I go on vacation my house will random turn on and off light making it seem like someone is home.

TheFreeElectron (author)  RobertL51 month ago
That is an amazing idea! But you better use directly a bash script, you do not need php since php is just here to make some treatment but especially to execute commands as you would do by yourself in the terminal. And that's super easy to use crontabs with a. Bash script.
Also randomly is not what I would recommend. Try using a lighting schedule that makes sense.
khims2 months ago


i downld u project web file.. when i try to call ip raspberry on web, only show black background and the image button didn show.. please help..

TheFreeElectron (author)  khims2 months ago
Hi Khims,
all required files were at there respective places when I downloaded the project and I haven't made any modification.
Is Apache well configured, JavaScript, PHP?
Do you have the small icon meaning "mising picture" or something like that?

That's all I'm thking about. I never experienced such troubles.


yes i have smal icon like u say. how can i fix it??

TheFreeElectron (author)  khims1 month ago

That means two things: -Or the pic's link is pointing at the wrong place -Or the link is right but the pic isn't at its place. You should check the link by taking a look at the page's code (developer's mode or Ctrl+u) then see where the link is pointing at and if the pic is at this place.

thanks my interface web is work. but I still have problems..

is it true ? before running the web, I have to run script.js ? when i run the script.js on the terminal, appear message like this:

var buuton_0 = document.getElementById("button_0")

referenceError: document is not defined

at Object.<anonymous> (/var/www/script.j:3:16)

at Module._compile (module.js:446:26)

at ......... and more

I 've installed nodejs. pless help.

TheFreeElectron (author)  khims1 month ago

JavaScript is ran automatically by your browser, you have nothing to do. So the answer is wrong.

It is completely normal to get this error since script.js was written to be executed by a browser and is making references to the web page. When running it with node.js, these objects are therefore undefined. Node.js is in this case completely useless.

phoneman12 months ago

Did I miss the CSS file?

TheFreeElectron (author)  phoneman12 months ago

CSS code is directly within the index.php file. Creating a separate CSS file wasn't necessary to change only background's color and some layout.

HabibN2 months ago

Oh great instructable!

I want to control lights' bedroom with my RPI. I made the web interface but nothing good..I use CM15 modules..Please help!
PS : My English is just bad! Sorry

TheFreeElectron (author)  HabibN2 months ago
Hi HabibN,

Thanks, I never tried CM15 modules but you should take a look at the 'Ible linked in the first step and google "CM15 Raspberry pi", this should help you.

Hope I Helped,
oamparo2 months ago

Hello TheFreeElectron,

I actually made this to work but i would like to make some modifications i just would like it to be a non latching push button switch. So when i push the button for 1 second it will turn on then a second later the pin will automatically goes off. Can you help me? I don't know where to start. Thank you so much.

TheFreeElectron (author)  oamparo2 months ago
Hi Oamparo,
I don't really have so much time now but that's all about the JavaScript, Therefore, you should begin by learning it especially timing functions. This should allow you to really understand the code then to modify it at your convenience.

Hope I Helped,
1-40 of 163Next »