I'm completely new to Arduino and do not have an electronics background. I'm really keen to do a POC on home automation. As a start, I decided to switch on a tube light using ESP8266, Arduino Uno R3 and isolated relay. I have read that this can be done without arduino also.

I'm able to switch on and switch off the relay without any problem. But when the relay is connected to the tube light, the ESP8266 stops responding. I couldn't find what was causing the issue.

I also read that the ESP2866 should be powered properly to function correctly. I'm following the wiring schema 5 mentioned in the link http://yaab-arduino.blogspot.in/2015/03/esp8266-wiring-schemas.html.

enter image description here

This is my code.

#include <SoftwareSerial.h>

#define DEBUG true
int relay = 8;
SoftwareSerial esp8266(2, 3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
// This means that you need to connect the TX line from the esp to the Arduino's pin 2
// and the RX line from the esp to the Arduino's pin 3
void setup()
  esp8266.begin(115200); // your esp's baud rate might be different
  pinMode(relay, OUTPUT);
  digitalWrite(relay, LOW);

  sendData("AT+RST\r\n", 2000, DEBUG); // reset module
  sendData("AT+CWMODE=3\r\n", 1000, DEBUG); // configure as access point
  sendData("AT+CWJAP=\"xxxx\",\"xxx\"\r\n", 5000, DEBUG); // configure as access point

  sendData("AT+CIPMUX=1\r\n", 1000, DEBUG); // configure for multiple connections
  sendData("AT+CIPSERVER=1,80\r\n", 1000, DEBUG); // turn on server on port 80
  sendData("AT+CIFSR\r\n", 5000, DEBUG); // get ip address
  sendData("AT+CIFSR\r\n", 5000, DEBUG); // get ip address

void loop()
  if (esp8266.available()) // check if the esp is sending a message

    if (esp8266.find("+IPD,"))

      int connectionId = esp8266.read() - 48; // subtract 48 because the read() function returns
      // the ASCII decimal value and 0 (the first decimal number) starts at 48

      if (esp8266.find("=="))
        Serial.print("State : ");
        char c = esp8266.read();
        if (c == '1') {
          digitalWrite(relay, HIGH);
        else {
          digitalWrite(relay, LOW);
      String webpage = "A";

      String cipSend = "AT+CIPSEND=";
      cipSend += connectionId;
      cipSend += ",";
      cipSend += webpage.length();
      cipSend += "\r\n";

      sendData(cipSend, 1000, DEBUG);
      sendData(webpage, 1000, DEBUG);

      String closeCommand = "AT+CIPCLOSE=";
      closeCommand += connectionId; // append connection id
      closeCommand += "\r\n";

      sendData(closeCommand, 3000, DEBUG);

String sendData(String command, const int timeout, boolean debug)
  String response = "";

  esp8266.print(command); // send the read character to the esp8266

  long int time = millis();

  while ( (time + timeout) > millis())
    while (esp8266.available())

      // The esp has data so display its output to the serial window
      char c = esp8266.read(); // read the next character.
      response += c;

  if (debug)

  return response;

The above code works perfectly until I connect the tube light to the relay. The power supply used in 9v 1A wall adapter.

The relay used is http://www.amazon.in/gp/product/B00LL0M6RS?psc=1&redirect=true&ref_=oh_aui_detailpage_o03_s00.

The v+ of the relay is connected directly to the wall adapters voltage and ground to ground of wall adapter. The input of the relay is connected to Arduino digital pin 8.

I'm really struggling hard to proceed here. Any help or hint would be much appreciated.

Thanks in advance.

3 Answers 3


I suspect that you've connected the relay wrong. I think that the LNP of the relay is supposed to be connected to D8 and the v+ to 5/3.3V and GND to GND....

  • 1
    Relay connection is correct. V+ of relay to 9v, GND of relay to GND and INP of relay to D8. As I mentioned in the question, it is working perfectly fine when the AC load is not connected. Oct 17, 2015 at 3:34
  • I suppose it has something to do with the relay. I'm going to try with Solid state relay. Oct 17, 2015 at 3:36
  • 1
    Noise more likely. Try optoisolating or decoupling (attach >100uF electrolytic capacitor to the power pins) the supply to the ESP.
    – Avamander
    Dec 16, 2015 at 12:34

If you are doing what you have said you are doing then I think the answer is so obvious no one will ever see it.

You say you have a 9V 1A power supply. Since you are powering the Arduino from this it is DC. Is that correct?

You are connecting an "AC Tube light" to the relay (which supports 1A 12V DC). If you are connecting an AC device to a DC supply then things probably do go wrong.

To test everything is working properly can you switch the AC light for a DC light?

I think you can get Relays with AC output and a DC Switching signal, but that will need a bit of research.

Hope that helps, if not comment and I'll try again.

  • "I think you can get Relays with AC output and a DC Switching signal" - This is what I'm doing. By the way I got it working with using only ESP8266. I'm not sure whether I can post that answer here as I don't use adruino. Jan 15, 2016 at 4:46
  • @SherinMathew, sorry mate I had to check. :) Jan 15, 2016 at 5:18
  • If you can get the circuit working without the relay, and you can get it working without the Arduino can you get the Arduino driving the relay (no esp) ? Jan 15, 2016 at 5:20
  • Yes, I was able to drive the relay using Arduino. I was using a PIR sensor to switch on the same tubelight using Arduino and relay. That was my first experiment. Then I decided to control the same using my phone. That is when ESP8266 came into picture. Jan 15, 2016 at 5:24
  • I think you might be drawing too much current because the parts work but not the whole. When you ran the esp8266 test were you powering the 3v from the Arduino? Could you power the relay from a separate psu and use a transistor to let the Arduino switch it? Jan 15, 2016 at 5:33

Finally I got this working using only ESP8266 and minor tweaks. I'll post the circuit here just in case if somebody is trying to do the same thing.

Please note that this was done as a POC and I'm not from an electronics background.

enter image description here

Kindly note the blue wire from the GPIO 01 to resistor and an LED. I had to do this because when the circuit is powered on a HIGH signal is sent from the GPIO and the relay is turned on. I think it is the default behaviour of ESP8266. To suppress this initial flow, I did this (There may be a better way to do this according to electronics experts).

Diode used is 1N4001 Diode

Decoupling capacitor is - 10nf/100V (0.01uf/100V) Box Capacitor - Polyester

Relay used is http://www.amazon.in/gp/product/B00LL0M6RS?psc=1&redirect=true&ref_=oh_aui_detailpage_o05_s00

Transistor is bc547.

Before connecting the devices, I flashed nodemcu firmware to ESP8266 so that I can write code in Lua script. Refer any of these to flash the firmware - https://www.youtube.com/watch?v=Gh_pgqjfeQc https://www.youtube.com/watch?v=pp6O96B1-Jk

I used LuaLoader to transfer the following code to the ESP8266. http://randomnerdtutorials.com/esp8266-web-server/

I'm posting my init.lua below.



cfg =

led1 = 3
led2 = 4
gpio.mode(led1, gpio.OUTPUT)
gpio.mode(led2, gpio.OUTPUT)
 gpio.write(led1, gpio.LOW)
 gpio.write(led1, gpio.LOW)
    conn:on("receive", function(client,request)
        local buf = "";
        local _, _, method, path, vars = string.find(request, "([A-Z]+) (.+)?(.+) HTTP");
        if(method == nil)then
            _, _, method, path = string.find(request, "([A-Z]+) (.+) HTTP");
        local _GET = {}
        if (vars ~= nil)then
            for k, v in string.gmatch(vars, "(%w+)=(%w+)&*") do
                _GET[k] = v
        buf = buf.."<html><head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"></head><body><h1> Control Panel</h1>";
        buf = buf.."<p>Tubelight <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p></body></html>";
        local _on,_off = "",""

        if(_GET.pin == "ON1")then

              gpio.write(led1, gpio.HIGH);

        elseif(_GET.pin == "OFF1")then
              gpio.write(led1, gpio.LOW);
        elseif(_GET.pin == "ON2")then
              gpio.write(led2, gpio.HIGH);
        elseif(_GET.pin == "OFF2")then
              gpio.write(led2, gpio.LOW);

        client:send("HTTP/1.1 200 OK\r\n");
        client:send("Content-type: text/html\r\n");
        client:send("Connection: close\r\n\r\n");

If everything is running, connect your phone or computer to the same wifi using "<ssid>","<password>" and access the IP to see a webpage with one buttons "ON" and "OFF". Click on it an check whether it is working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.