Wifi Controlled LED Using Raspberry Pi 3

About: Studies EEE at CET, Trivandrum.

This project demonstrates a basic idea of the Internet of Things(IOT) whose applications are extending from smart homes to wearables to healthcare.

"The Internet of Things has the potential to change the world just as the Internet did. Maybe even more so" - Kevin Ashton.

Here in this project, I am demonstrating a method to switch on/off and blink an LED from a website which can be accessed worldwide. This project can be developed further making it useful in our day to day lives. Instead of the LED used here, if we connect the PI to AC mains via a relay, it becomes a Home Automation.

Step 1: Components Required :

The components required for this project are :

1. Raspberry Pi 3

2. LED

3. 270 ohm resistor

The raspberry pi should be provided with an SD card loaded with an appropriate OS(preferably Raspbian) and should be connected to a monitor via HDMI to VGA converter or to a laptop via SSH. Apart from the above components a router with internet connection should also be present, with the raspberry pi being connected to it.

Step 2: Connections

The connections in this project are very simple. Connect the positive pin of LED to GPIO 17 pin and the negative to a 270 ohm resistor, the other side of which is connected to GND pin.

Step 3: Installing WiringPi Library

WiringPi is basically a GPIO interface library for Raspberry Pi. There are 2 methods to install wiringPi library.

(All the commands given below are to be executed on the Pi's terminal).

Method 1

1. Make sure your Pi is up to date with latest versions of Raspbian by :

sudo apt-get update

2. Install git by the command:

sudo apt-get install git-core

3. Obtain WiringPi using git by:

git clone git://git.drogon.net/wiringPi

4. To build/install WiringPi library

cd wiringPi

Method 2

Try out this method only if method 1 doesn't work out.


Click the above URL and check for the link marked snapshot at the right hand side. Click the top one which will download a tar.gz file like wiringPi-xxxxxxx.tar.gz (xxxxxxx represents a combination of letters and numbers which is unique for each download).

Then open the terminal and type these commands:

tar xfz wiringPi-xxxxxxx.tar.gz
cd wiringPi-xxxxxxx ./build

Thus the WiringPi library will be installed by one of the above methods. For more details about WiringPi library, please visit http://wiringpi.com/. To make sure Wiring Pi is installed and works properly, run the gpio -v command; it should return the current version of Wiring Pi along with the basic Raspberry Pi info.

Step 4: Installing a Web Server

Apache is a popular web server application you can install on the Raspberry Pi to allow it to serve web pages. On its own, Apache can serve HTML files over HTTP, and with additional modules can serve dynamic web pages using scripting languages such as PHP.

In our project we are using an HTTP server and its PHP extension. MySQL database is not used here.

First install Apache HTTP server and its PHP extension by:

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

In order to test if the Apache server is working properly, navigate to the browser and type your Pi's IP address.

(To get Raspberry Pi's IP address type ifconfig on the terminal. Next to the wlan0 entry you will see inet addr: 192.168.x.x which is the IP address of the Raspberry Pi.)

You should see an It works! page.

Now we should test whether its PHP extension is working. The above "It works!" html page is present in "/var/www/" directory as index.html. Now, delete this html file and create a new PHP file index.php. Then type the php code below :


Save it and refresh the page in your browser. A long page with lots of information about PHP will appear. Thus the PHP extension is installed properly. If any of the pages do not appear try reinstalling apache server and its PHP extension.


Step 5: Start Coding

Here in this project, we will be coding in PHP to control the GPIO pins, which is embedded in an HTML page. GPIO pins of the Raspberry Pi can be controlled by PHP using shell_exec() function. This function executes command via shell and returns the complete output as a string.

Now delete the code in index.php file and insert PHP code to control GPIO pins inside body of HTML code. If you are unable to edit the index.php file due to some administrative problems, navigate to "/var/www/" directory using cd /var/www/ command and type the code - sudo chmod777 index.php. Now you will be able to edit the php file.

First create a form in HTML consisting of 3 buttons - ON, OFF and BLINK .

Then set the GPIO 17 pin as output with the help of WiringPi library using the statement:

shell_exec("/usr/local/bin/gpio -g mode 17 out");

Check for the values returned by the button and accordingly make the GPIO pin HIGH/LOW using if else-if statements.


echo "LED is off";

shell_exec("/usr/local/bin/gpio -g write 17 0");


else if(isset($_GET['on']))


echo "LED is on";

shell_exec("/usr/local/bin/gpio -g write 17 1");


else if(isset($_GET['blink']))

echo "LED is blinking";

for($x = 0;$x<=4;$x++)


shell_exec("/usr/local/bin/gpio -g write 17 1");


shell_exec("/usr/local/bin/gpio -g write 17 0");




The entire code can be downloaded from the link below.

Step 6: Running Code on Local Host

After completing the code, the next step is to run the code on your browser. Type the ip address of raspberry pi on the browser. You will see 3 buttons - ON, OFF and BLINK as shown in the image and you will be able to control your LED by clicking those buttons.

This control can be obtained from only those devices connected to your router(local network). The LED cannot be controlled from any other network. In order to do that, follow the instructions in the next step.

Step 7: Port Forwarding and Obtaining a Free Domain Name

Now we are able to control the LED by the local host network. In order to make it accessible world-wide, we need to change few settings in the router and also obtain a free domain name.

First obtain the unique public IP address of your router from https://www.whatismyip.com/.

The next step is to log in to the router by typing this address. Take the port forwarding option and add a new port(not 80 since it is the default port) by providing the ip address of the raspberry pi and enable it.

Now if you type type your public ip address and the port number separated by a colon in the format:

<Public ip address>:<Port number> in the browser of any network, you will be able to access the page.

Since this ip address is too long and hard to remember, you can get a free domain name from http://www.noip.com/ where you have to configure the device by setting up the right port.

This will enable you to control the LED from anywhere in the world.



    • Faux-Real Contest

      Faux-Real Contest
    • Safe and Secure Challenge

      Safe and Secure Challenge
    • Warm and Fuzzy Contest

      Warm and Fuzzy Contest

    19 Discussions


    5 weeks ago on Step 5

    Got it all working, but one slight issue is that the blinking prompt only comes up after the 4 LED flashes have finished. Any ideas?

    1 reply

    Reply 17 days ago

    Hi Paul. I have the same issue - did you manage to fix it?


    4 weeks ago

    Hi, in my case i had to look for the newest versión of php, it was 7 instead 5, if someone is having trouble downloading php apache.
    also in the php test, it worked when tiped the code like this:


    1 year ago

    Thanks for this tuto. I am quite a newbie - will it work generally with any LED bulb? I would like to go with https://www.sortedreviews.com/active-light-cool-to-warm-white-bulb-e27/ but I am a bit confused if I manage to make it work.

    1 reply

    Reply 5 weeks ago

    Depending on the LED you might need to change the resistor value so as not to overdrive it and damage it.


    Question 9 months ago on Step 7

    After getting the webpage, if i click on any of the buttons say on, it shows page not found. Please help.

    1 answer

    Answer 9 months ago

    you need to create another php file name it gpio.php besides the index.php file and add the php code that is given into the php file

    in index.php just add the html code

    Chami Jay

    1 year ago

    Hi!! I'm currently trying out this project.
    But I get a error when I try to press one of the three
    buttons. My error is,

    Not Found

    The requested URL /gpio.php was not found on this server.

    Apache/2.4.10 (Raspbian) Server at IP of my Pi

    Any suggestions?

    3 replies
    Chami Jaykabhishek247

    Reply 1 year ago

    yeah..just simply replace the action="gpio.php" to action="index.php". It should work now.


    1 year ago

    Hi, thanks for the tutorial.

    Actually, i cannot make the LED light up. Isn't only run the php code to make it light up or we have another .py file?

    Anyone can help? Thank you.


    1 year ago


    It all worked until I got to the part to have it work anywhere in the world.

    I got my IPaddress and logged into the router and changed the port as stated.

    I have xfinity so I made the common service = Other, then I gave it a service name, I then entered my Pi IP4 Address for the Pi and made the port 3389 and saved it.

    It will not recognize when I put in the "IPaddress:Port" into the browser.

    Any thoughts


    1 year ago

    hey i did everything correctly up to the webpage part but when i press any buttons no action is to be seen in the pi..what to do which step must hav i gone wrong???


    2 years ago

    Great post, Vaishnav. The instructions are very detailed and easy to follow.

    James C Dcruz

    2 years ago

    Let it be a starting 4 u,inspirational