Nuffnang Ads

Wednesday, 10 September 2014

Raspberry Pi GPIO - LED on/off (wiring Pi + php)

This entry is the third entry for the GPIO on Raspberry pi. Assuming readers already installed the wiringPi, just follow the below step to make LED can be controllable using web GUI.

1) Type the following command to install the necessary packages at the Raspberry pi. Make sure your Raspberry pi has internet connection.

sudo apt-get update
sudo apt-get install apache2 php5 libapache2-mod-php5

2) Now, from your computer,, open the web browser and type the IP address of your Raspberry pi. You will get the "It works!" page. This means the apache and php successfully installed in your Raspberry pi.

3)Now cd to /var/www and edit the index.html to index.php.

4) Edit the index.php as the following to create two simple button (on,off) that will link to execution of the gpio commands. The command "system("")" is used to execute the linux command

    if (isset($_POST['button']))
         system("gpio mode 0 out");
        system("gpio write 7 1");
    else if (isset($_POST['btn']))
        system("gpio write 7 0");
    <form method="post">
        <button name="button">On</button>
        <button name="btn">Off</button>

5) Save index.php and exit. Open the web browser any machine that connected to the same router and enter the Raspberry Pi's IP address. You will get the page similar to this:-

Click "On"/"Off: to turn on/off the LED

LED on

LED off

6) Done



  1. Hi.. i like your article.
    what if i want using two leds ?

  2. Hi Indra, thanks, you can put more LED depends on the number of GPIO pins supporter by raspberry pi. You can put another LED at another GPIO pin see (
    for GPIO pins. After that, create another sets of on/off button at PHP, link it with "gpio write 0/1"

  3. hi,,, i try your article,,
    in puty console with script ""gpio mode 0 out" and "gpio write 7 1" Or "gpio write 7 0" LED can on/off..
    but i have problem, when i try in php script like your code but LED didn't work on/off, help me sir

  4. Thanks it worked for me.But we need to change the out put pin from 0 to we are sending signal to pin number 7.

  5. Hi...,
    Thanks for this article.i tried this using two LEDs.i created two more on/off buttons and linked the gpio pin 11 to gpio write 0/1.But the LED which i newly connected did not work on/off.Please help me sir if any changes are required.

  6. شركة تسليك مجارى بالرياض افضل شركة تسليك مجارى فالشركة تعتمد على افضل الخدمات المميزة التى تساعد فى الوصول الى افضل النتائج المميزه التى تساعد فى اعمال التسليك من خلال افضل الطرق المميزة التى تساعد فى القيام باعمال التسليك والحصول على افضل النتائج المميزة فمن اهم ما تقوم بيه شركة تسليك مجاري بالرياض السعى الى الانتهاء التام من اعمال التسليك المتكررة والانتهاء ايضا من اى شى غير مالوف وجوده فى المجارى فاذا اراد ان تقوم بكل ذلك فتاكد انك لم تجد افضل من شركة تسليك المجاري
    تعتمد شركة بسمة الرياض افضل شركة تسليك المجارى بالرياض على افضل الكودار البشرية المميزة المتواجده فى الاسواق والتى فى حاله تدريب ميتمر من قبل شركتنا لكى يتم التاكد ان العماله على وعى كبير بكيفيه القيام بالتسليك واختيار الانسب لنوع المجارى المتواجده حتى لا يتم عودتها مرة اخرى فاذا اراد ان تتخلص من اى شىء غير مالوف فعليك ان تتعاقد وتتعاون معنا الان فى القيام بكل ذلك فى اقل وقت ممكن وتخلص من مشاكل التسليك المتكررة .

  7. Now cd to /var/www and edit the index.html to index.php

    how to edit the file from index.html to index.php
    i tried doing it but the access permissions cannot be changed.

  8. Nice info about Php it’s reallyhelpful…. If it possible share some more tutorials……….

  9. Just a note. I had to add a -g option to the GPIO mode and GPIO write to get the LED to turn on and off.
    Great tutorial-Thanks so much

  10. Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.