ESP8266 BASIC IoT Light




Introduction: ESP8266 BASIC IoT Light

About: Make it yourself if you cannot buy one!

This Instructables show how to use ESP8266 BASIC remote control a household light.

Step 1: Why Remote Switch?

My home hall light switch hide behind the Cabinet and far from the door. It is hard to turn on the light when I came home or turn off the light when I leave. So I want to control the light remotely.

Before do that, here are some concern about the remote switch:

  • other people may gain the access to control the remote switch too
  • remote receiver normally consume power below 1 W, but it run in 7x24 basis, so it still use up some extra power
  • it may have some risk being shocked while accessing AC power

After evaluated the concern, I still want a remote light switch, so I go to next step.

Step 2: Turn Off the Main Power Before Any AC Access

For safety reason, you should switch off and double check no AC current before access any AC wire.

Step 3: Check Wires in the Switch Socket

Same as many remote receiver, ESP8266 require a DC current to operate, so I need a AC/DC converter. However, in some case, the switch socket may only have the Live wire but did not have the Neutral wire. In this case, it cannot provide DC current in the socket.

After checked it have both Live and Neutral wire in the socket, we can keep going to the next step.

Step 4: Choosing Remote Receiver

There are many cheap remote light switch in the market, but I guess it is very easy to jam other home remote channels.

ESP8266 use WiFi connect to my own AP with encrypted signal should be good enough.

There are still many firmware support ESP8266: NodeMCU, Arduino, MicroPython, ESP8266 Basic...

ESP8266 Basic have a very good feature, once I flashed the firmware, all program work in web base. That means I can hide the board in the socket and no need to take out for reprogram. (except upgrade firmware :P)

ESP8266 firmware ref.:

Step 5: Preparation

  • ESP8266, any version should be OK, this time I am using ESP-07
  • A USB FTDI programmer for flashing ESP8266 Basic firmware
  • A light switch socket with USB charge plug (for AC/DC conversion)
  • A 10 k Ohm resistor
  • A 2N2222 transistor
  • A 3 V / 250 V 3 A relay
  • Few wires for AC circult
  • Few wires for DC circult
  • A 3 pin Screw Terminal Block Connector
  • A 5 V to 3.3 V DC/DC convertor

and it need some hot glue and clear tape for insulation

Step 6: Disassembly the Switch Socket

I need to connect the extra screw terminal block connector, squeeze my component in it, access the AC/DC converter and wire out the 5 V power source, so I need disassembly the switch socket first.

Step 7: Install 3 Pin Screw Terminal Block Connector

  1. Mark the Pin in the Socket
  2. Drill the holes
  3. use hot glue to fix the connector

Step 8: Program ESP8266 Basic

If you are using ESP-07 too, here is the connect reference:


  • Tx -> FTDI Rx
  • Rx -> FTDI Tx
  • GPIO15 -> FTDI GND
  • VCC -> 3.3 V
  • EN (CH_PD) -> 3.3 V

After flashing ESP8266 BASIC, remember remove the GPIO0 connection and reset (simply re-plug the USB) for testing.


Step 9: Connect and Config ESP8266 BASIC

After removing the GPIO0 connection and reset (simply re-plug the USB), you can search a WiFi AP called ESP by the mobile or notebook.

Connect to the AP ESP and open the URL

Firstly, make it connect to the home WiFi AP:

  1. press [setting] link
  2. fill the AP name and password
  3. press save button
  4. before restart, open a serial console (Arduino have one) in the computer connected ESP through the USB FTDI programmer
  5. press restart button
  6. if everything goes right, you can see ESP connected to the home WiFi AP and show the current IP address in the serial console, remember the IP address
  7. try connect to the IP address with web browser
  8. it is recommended config the DHCP server associate fix IP address to the ESP device

Step 10: Simple Toggle Switch Code

I want the light switch can be controlled by both physical switch and web base interface, so I write a simple toggle logic to alter the GPIO output.

Here is my sample code, edit, paste and save to ESP8266 BASIC: (ESP will restart after save)

let OutputPin = 5
button "Toggle" [Toggle]

if PinStatus == 1 then goto [On] else goto [Off]

PinStatus = 0
po OutputPin 1

PinStatus = 1
po OutputPin 0

Step 11: Soldering Work

Here is the summary of the connection:

5 V to 3.3 V DC/DC convertor

  • +/- IN -> USB charge panel
  • +/- OUT -> ESP


  • GPIO15 -> GND
  • GND -> GND
  • EN -> 3.3 V
  • VCC -> 3.3 V
  • GPIO5 -> 10 K Ohm resistor -> 2N2222 transistor base

2N2222 transistor

  • base -> 10 K Ohm resistor -> ESP GPIO5
  • collector -> relay coil terminal -> 3.3 V
  • emitter -> GND


  • Common terminal -> Terminal Block Connector
  • Normally open terminal -> Terminal Block Connector
  • Normally close terminal -> Terminal Block Connector
  • Thanks to CaptClaude remind me it should put a diode parallel to the relay coil (ref.:

If you are using ESP-01, some points to be noted. Connect a NPN transistor to GPIO have a side effect that pull down the pin. ESP-01 only have GPIO0, GPIO2, Tx(GPIO1) and Rx(GPIO3), pull down GPIO0 or GPIO2 at power on will enter flash mode; connect 2N2222 to Tx also cannot start up normally. Hopefully it can still connect to GPIO3, everything can work fin.

Step 12: Insulation

Use hot glue to seal all AC connected parts and then use some clear tape to cover the DC/DC converter and ESP board.

Step 13: Re-assembly the Switch Socket

Squeeze all component to the socket and then re-assembly the switch socket.

Step 14: Replace the Old Socket

I want the light can control by physical switch and ESP connected relay, so it need to connect the switch and relay in serial.

Step 15: Test Connection

Ensure you mobile connected home WiFi:

  1. open the ESP IP address with the browser
  2. click [RUN] link
  3. press toggle button
  4. the light should turn on and off while you press toggle button

Step 16: Happy IoT!

This ESP8266 BASIC light actually only connected to the home LAN but not Internet, but it still can remote control the light outside the home if you have setup the home VPN.

If you really want your light become a real IoT, try Blynk, I have tested it works while research, just a little bit difficult than ESP8266 BASIC :P



    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest
    • Tiny Home Contest

      Tiny Home Contest

    43 Discussions


    1 year ago

    I have problem
    after save Toggle Switch Code, see this error
    Error at line 3: The arguments must be 2!
    Error at line 3: Halted
    can help me?!

    1 reply

    I have just purchased an ESP8266-12 and cannot link it via my USB to my pc
    Can you please help , I can pick up the device on my phone via wifi and I can link to it but that is it

    1 reply

    whats mean cannot link? can you show me how you connect the USB serial cable to the ESP-12E board?

    陳先生 你好:

    我把你的程式加上一段設定WIFI AP的指令並進行連接,然後用"print ip()"就可以在行動裝置上看到所回傳分配到的動態IP值了,這時只要重新啟動瀏覽器並打上新的IP就可以透過WIFI Router連接上ESP8266了!

    let ssid = ""

    let password = ""


    let OutputPin = 5

    button "Toggle" [Toggle]

    print "SSID:"

    textbox ssid

    print "PASSWORD:"

    textbox password

    button "Connect" [connect_to_ap]



    connect ssid password

    print ip()



    if PinStatus == 1 then goto [On] else goto [Off]


    PinStatus = 0

    po OutputPin 1



    PinStatus = 1

    po OutputPin 0



    很高興能看到你的大作,不過我有個建議,如果能將設定及連結WIFI AP的動作也融入程式中,然後可將分配到的IP直接傳到使用者手機端的話,這樣一般的使用者就不用再透過筆電等裝置去取得不就更方便了嗎?

    1 reply

    seems great, but how to do that? here are the steps I think:
    1. connect esp default ap with mobile.
    2. write code to connect home ap and save
    3. run the code and show the obtained ip address on the result page
    is it correct?


    2 years ago

    Cool inspiriting project.

    My advices:

    - on your transistor, you should add a pull-down resistor (~100Kohm) to avoid unwanted driving of your relay when the driving pin is in float state (can occurs when ESP82666 is starting-up / reseting or dead).

    - be carefull with the duck you used. It is not an isolated duck. Also the hot glue does not have any official isolated properties. In addition, with the heat provided by the AC/DC 5V converter, the glue could warm up and melt => you will lost the isolation and cables could move and create short circuit. Keep in mind that you are working on mains !

    2 replies

    thx for your remind, sinzu. I am not familiar to this scope, can you suggest another material instead of hot glue?

    Instead of standard duck, use electrical duck or kapton duck (electrical isolated + heat resistant).
    Instead of glue you could use potting to full your project box with resin. It is more expensive and does not allow modification once dry, but it is isolating and very resistant for a long life.

    Just wondering if there's any other way to do this such that the physical light switch triggers the relay instead? And some feedback such the user can monitor whether the light is on or off. Also, what are the resistor and transistor for?

    Anyway this instructable really helped me understand more about the ESP8226 and IoT. Thanks!


    Congratulations for this nice tutorial !!! I have 2 requests:

    1/ I am looking over the internet for this Simenzis switch socket with USB charge plug, but it is just impossible to find it, even an equivalent. Do you have the reference for it ?

    2/ the neutral wire is not used. What is the real issue for not using it ?

    Best regards from Paris !!


    1 reply

    2/ USB panel require both Live and Neutral wire for converting 5 V DC

    ESPBASIC is the best man gotta love it ...

    I went ahead and made a dimmer switch and an android app to control everything for the living room I am very pleased with it and have shared my project as well on esp8266 website under mmiscool basic section and subsection is programming examples AC Light Dimmer....

    Also I included an android application and I also wrote a small beginners guide to basic on the nodemcu/basic wifi dev kit.

    Hope you all stop by the forums and check it out at esp8266 dot com.


    2 years ago

    You can do it by bluetooth in these link:


    2 years ago

    wow it's cool, switch only have 2 cable right ? how u add netral cable ? what component ? because i like see connector with blue cable , thanks for ur answer :)

    2 replies

    sorry for misleading, all 3 wires break out from the relay are Live wires. But I don't have enough brown wire :P

    and then ? haha i don't get it -_- switch is a switch(2 cable) how can u change switch become (source(220vac) +switch) ?