Introduction: How to Use Arduino to Turn on a Lamp Remotely, Directly From Your IPhone/Android

Picture of How to Use Arduino to Turn on a Lamp Remotely, Directly From Your IPhone/Android

With Arduino you can really make so many fun and useful projects, especially to facilitate some tasks at home. In this article we will see how to use the Arduino to remotely turn on a lamp, or any device that is connected to the outlet of our home.
Let 's see what devices are needed to make this project:


1 x Arduino Uno cost 12 Euro, sold on Ebay
1 x ENC28J60 Ethernet card cost 5 euros, sold on Ebay
1 x Relay 220 V 10 A cost 3 Euros, sold on Ebay
1 x Ethernet cable 1 meter cost 2 Euro


Arduino IDE
Library for ENC28J60
Sketch code lamp relay + + WebServer

Step 1: What Is a Relay?

Picture of What Is a Relay?

A relay, from the external point of view is a simple switch capable of passing current or not. This phenomenon occurs through a physical principle, based on the coupling magnet that is generated through a passage a small excitation current, which is supplied by the Arduino. There are two configurations with regard to the relay:

1. NO (Normally open)
2. NC (Normally closed)

These two configurations differ only in the code you need to write in Arduino. The first configuration, in the case of switching off the relay, no current is passed (the circuit is open) and that the lamp is turned on you need to get out of an Arduino signal "HIGH". The NC configuration is dual, in case the relay is not energized, the current flows in the device and that the lamp is off, you need to do to get out of an Arduino signal "LOW".
To recap from the point of view of the code:

NO configuration

digitalWrite(relay,LOW); // Lamp off
digitalWrite(relay, HIGH); // Lamp on

Configurazione NC

digitalWrite(relay,LOW); // Lamp on
digitalWrite(relay, HIGH); // Lamp off

For those who wish to learn more about how this circuit element, you can visit the website of Wikipedia, which talks about the various types of relays.

Step 2: How to Connect the Various Cables Between Devices

Picture of How to Connect the Various Cables Between Devices

The links that need to be done to make the project are quite simple. We have two devices to be connected to the Arduino: 1) the relay and 2) the Ethernet card.
Concerning the first, it will be necessary power the relay with 5 V, the ground and the pin-out, to be connected to Arduino pin set in the code, which in this case is 5.

The microcontroller Ethernet is slightly a bit 'more complicated, because they are well connected by flexible cables 6. There are two usual power cables, VCC (3.3 V, though!) And GND and 4, which are used to send and receive data over the Internet. Here is a table that summarizes the connections:

Vcc  -> 3.3v
SCK -> Pin 13
SO -> Pin 12
SI -> Pin 11
CS -> Pin 8

Step 3: Software Installation of the Various Components

The functions that allow you to operate your ethernet card is not present in the Arduino IDE. For this reason it is necessary to install this library, inside the folder "libreries" Arduino. To do so, just download the file in. Zip, unzip it and move it to the folder of the Arduino, without opening the program. Alternatively, you can do this procedure directly from the IDE software.
Now that the library is present, we can proceed to load the file on our Arduino Uno.
To do this we download the source code from this link and press the button in the top left, to transfer it to our microcontroller. After a few seconds, if all goes well, will come the message "Loading completed." Now we can finally go to practice!

Questo codice permette di accendere una lampada connessa al relè da remoto, sfruttando
la scheda di rete ENC28J60
SCK -> 13
SO -> 12
SI -> 11
CS -> 8
Vcc -> 3.3 V

#include <EtherCard.h>
#define RELAY_PIN 5
#define REQUEST_RATE 5000

static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
char* on  = "ON";
char* off = "OFF";
boolean relayStatus;
char* relayLabel;
char* linkLabel;

byte Ethernet::buffer[700];

void setup () {
  Serial.println("Getting IP via DHCP");
  if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
    Serial.println( "Failed to access Ethernet controller");

  if (!ether.dhcpSetup())
    Serial.println("DHCP failed");
  ether.printIp("My IP: ", ether.myip);
  // ether.printIp("Netmask: ", ether.mymask);
  ether.printIp("GW IP: ", ether.gwip);
  ether.printIp("DNS IP: ", ether.dnsip);

  digitalWrite(RELAY_PIN, LOW); 
  relayStatus = false;
  relayLabel = off;
  linkLabel = on;

void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
  if(pos) {
    if(strstr((char *)Ethernet::buffer + pos, "GET /?ON") != 0) {
      relayStatus = true;
      relayLabel = on;
      linkLabel = off;
    } else if(strstr((char *)Ethernet::buffer + pos, "GET /?OFF") != 0) {
      relayStatus = false;
      relayLabel = off;
      linkLabel = on;
    digitalWrite(RELAY_PIN, relayStatus);

    BufferFiller bfill = ether.tcpOffset();
    bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
      "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
      "<html><head><meta name='viewport' content='width=200px'/></head><body>"
      "<div style='position:absolute;width:200px;height:200px;top:50%;left:50%;margin:-100px 0 0 -100px'>"
      "<div style='font:bold 14px verdana;text-align:center'>Relay is $S</div>"
      "<br><div style='text-align:center'>"
      "<a href='/?$S'><img src='$S.png'></a>"
      "<div>Controlla la lampada<div>"
      ), relayLabel, linkLabel, linkLabel);

Step 4: Internet of Things

Picture of Internet of Things

The first step is to get the local ip address of the Arduino. To do this there are several possibilities: the first is to open the serial port, or the other option is to download a program to scan the host in the local network.

Now that we have the IP address, open the browser and type in our phone its address. You will see a web page like the one in the photo. 

Once you press the green button, a new page will appear, with which you can turn off the light.

To turn on the lamp, even remotely, you must open port 80, the control page of our router, choosing the corresponding IP.

Step 5: And for Ethernet Controller W5100 ?

Picture of And for Ethernet Controller W5100 ?

You can complete the project if you have the Ethernet Controller W5100. The correct code is the one in this link.

Questo programma permette di accedere e spegnere una lampada da remoto

#include <SPI.h>
#include <Ethernet.h>

int pinLED = 9; // pin a cui è connesso il LED
boolean acceso = false;
// Mac Address di Arduino
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED

// Viene inizializzata la libreria Ethernet di Arduino e il webserver gira sulla porta 80
EthernetServer server(80);

void setup() {
  // Viene inilizzato il webserver e la connessione di rete
  Serial.print("server is at ");

void loop() {
  // Vengono ascoltati nuovi client
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // Finisce una richiesta HTTP
    boolean currentLineIsBlank = true;
    String postText ="";
    while (client.connected()) {
      if (client.available()) {
        char c =;
          postText +=c;
        // Se viene completato l'invio della richiesta HTTP, allora il server invia la risposta
        if (c == '\n' && currentLineIsBlank) {
          // Viene fatta una risposta HTTP, in pratica viene creata una pagina WEB in HTML
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // Dopo la risposta la connessione si interrompe
          client.println("<meta charset=UTF-8>"); // serve per inserire i caretteri speciali
          client.println("<!DOCTYPE HTML>");
          client.println("<head> <TITLE>Arduino</TITLE> </head>"); // Viene creato il Titolo
          client.println("<body> <h1> Benvenuto nel Webserver Arduino </h1>"); // Viene inserito del testo
          client.println("<h3> Attraverso questa pagina è possibile accendere e spegnere lampada, connessa ad un relè </h3>");
        if (c == '\n') {
          currentLineIsBlank = true;
        else if (c != '\r') {
          currentLineIsBlank = false;
    // Se l'utente ha premuto l'icona per accendere il LED
    if(postText.indexOf("?on") >0){
          Serial.println("Accendi LED");
          acceso = true;
     // Se l'utente ha premuto l'icona per spegnere il LED
     if(postText.indexOf("?off") >0 ){
        Serial.println("Spegni LED");
        acceso = false;
      // Viene cambiata la pagina WEB a seconda che il LED sia spento, oppure acceso
          client.println("<a href=\"./?off\"> <img src = \"\"alt = \"Spegni\" ></a>");
          client.println("<h3> La lampada è accesa </h3>");
          client.println("<a href=\"./?on\"> <img src = \"\"alt = \"Accendi\" ></a>");
          client.println("<h3> La lampada è spenta </h3>");
    // Viene chiusta la connessione
    Serial.println("client disconnected");

Step 6: Android Application !

Picture of Android Application !

Now you can use your Android to control in an easy way your lamp remotely !


ayikb (author)2017-06-21

hello this is what i looking for my home

but can you make sketch using 2 relay moduls

SanderJ2 (author)2016-09-20

Hello, I have connected my arduino to the internet and uploaded the program but i only says "Getting IP via DHCP", after 5 minutes it was still the same how can i fix this?

jackbell16 (author)SanderJ22016-09-22

May be that your router has disabled the DHCP protocol, or the ethernet cable is not well inserted in the Arduino

SanderJ2 (author)jackbell162016-09-22

I already know what it is, if you use the Arduino Mega 2560 then you need to use other pins: 50, 51, 52, 53 for the ENC28J60 and you can use the relay pin on 5

AlvianP (author)2016-09-22

hei, very good project !

i tried your program using ENC28J60 and its working, i can turn ON/OFF remotely from my browser.
but once i try to open the IP address for my Arduino from another device (my phone), it doesnt load the page.

so i assume that this code only work for local network. (cmiiw)

how can i change it so it can work online too?

thanks in advance

jackbell16 (author)AlvianP2016-09-22

The problem is that by default you can't access remotely to your router, but you need to perform an operation that is called port forwarding.
From your local network you have to open the console page of your router; move to the page of port forwarding and insert the port 80 for the TCP connection and the IP address of Arduino. Once completed this, type on google My IP address and get it. Now you can remotely switch on and off your lamp from your smartphone

BAN PP (author)2016-06-17

Hi, can i switch a bulb/lamp or led by using remote control tell me.


jackbell16 (author)BAN PP2016-06-21

yes you can switch on/off your lamp/led with this tutorial. The relay is able to open and close the current flow.

eceb2 (author)2016-02-19

where is the code for arduino if i want to use ENC28J60?

Wuzi (author)2015-10-02


How could i make it to work online, not only local?

Thanks !

jackbell16 (author)Wuzi2015-10-04

The trick is use the NAT of your Router. For this purpose, you have to port forwarding the port number 80 of the IP address of Arduino. Once done this, you have to get the public IP of your broadband connection and that's all.

MostafaE2 (author)jackbell162016-02-02

can you give me example about it?

also control from PHP


jackbell16 (author)MostafaE22016-02-03

Do you want to turn on and off from a PHP application your lamp?

MostafaE2 (author)jackbell162016-02-04

yes please , but online not local

i hope you can give me example


jackbell16 (author)MostafaE22016-02-10


$url = 'YOUR_PUBLIC_IP';

$http = newHttpRequest($url, HttpRequest::METH_POST);

$http->addPostFields(array('value' => '?on'));

$response = $http->send();


I've not tested it, but I think should work without any problem. Of course you have to insert your public ip of your Arduino and use the second version of the code, the one for the official ethernet shield for Arduino.

If you don't know your public ip, this is a useful web site: If you haven't done it, you have to open the port 80 on your router for the local ip address of Arduino, to let someone else from external, to access the Arduino.

عبداللها11 (author)2015-11-28

hii >>after i get the public ip and the port >is there any changes in the code

HarisH2 (author)2015-07-24

How to use it with RN-42 bluetooth?

jackbell16 (author)HarisH22015-10-04

Why do you need to use the bluetooth technology ?

jagmeetsinghsaluja (author)2014-04-07

Can i control Air Condition (Load of 16A) with this???


the answer is no. The problem is that the relay works with current below to 10A. I think you will be able find in the market something about. In all case I suggest you to view my tutorial about how to control the A/C with Arduino . You will find out a better way to control the A/C. So if you want to use this solution, I mean the Arduino Uno, you can use, but you must keep attention to the relay load. If you want to use a better solution, please visit my other tutorial.

In you ac tutorial you are controlling via remote led but i want type of automation.

I want to control my hotel with switches and do automation. The scneario would be like this:

If customer wants ac room , i will put relay 'ON' from my reception control. And the customer further can switch it on off with ac remote.

If customer wants non-ac room, i will put relay 'OFF' from my reception control. And customer cant switch on the ac with ac remote. because relay wont allow current to pass. I searched net for ip relays and found some 30a relays too. But i dont that would work or not. Also i was looking for some cheaper solution.


I think you will be able to keep the price low. You can use ATMEGA328 (the micro of Arduino, without PCB ) and you can use the Ethernet controller to get connection. So the price would be:

Atmega 328 plus some items 5-6$ for each room

Enc28J60 3-4$ for each room

Relay 10A 1 $

One Arduino Mega as the main station 15-20 $

Oliverhall (author)2014-03-23

Great tutorial! If you need to buy relay modules, this webstore is selling them for a good price:

isnacku (author)2014-03-20

hi there jack, thanks for the great project. am trying to make similar as yours.

can you please explain am using ethernet shield w5100, do i need to change the library.

jackbell16 (author)isnacku2014-03-21


thank you for your comment. You have to modify the code to control the lamp from Arduino Uno, using W5100 Ethernet Controller. You can find the correct code from my post . In all case, just a few months a go, there is a new library who let coding with ENC28J60, like using the Ethernet W5100 Controller. If you have any question, please feel free to contact me !

About This Instructable




Bio: I'm an Arduino Maker, I like technology, playing tennis
More by jackbell16:How to let comunicate Arduino with KeyPad to DB MySQL.Arduino water system to wet your plantHow to make an alarm system with Arduino
Add instructable to: