ESP01/01S RELAY MODULE TUTORIAL

136,455

79

82

Introduction: ESP01/01S RELAY MODULE TUTORIAL

Description

This WiFi relay based on AI-Thinker ESP-01/ 01S WiFi module, we use the GPIO0 of ESP-01/ 01S to control the relay by low level. It’s easy to DIY your smart switch to any device by your phone anywhere with this smart relay.

Specifications

  • Working voltage: DC 5V-12V
  • Working current: ≥250mA
  • Communication: ESP01 or ESP 01S
  • WiFi module Transmission distance: the maximum transmission distance is 400m (open environment, mobile phone equipped with WiFi module)
  • Load: 10A/ 250VAC, 10A/ 30VDC, 10A/ 30VDC, 10A/ 28VDC
  • Size: 37 x 25mm

Step 1: Material Preparation

In this tutorial, we only used the following materials:

  1. USB to UART FTDI Converter
  2. ESP8266 WiFi Serial Transceiver Module

and last but not least, ESP01/01S Relay Module.

We connect ESP8266 WiFi Serial Transceiver Module to ESP01/01S Relay Module so that we can control the relay via WiFi. To program ESP8266, FTDI Converter is required to be connected with ESP8266.

Step 2: Hardware Installation

As for the connection between ESP8266 and FTDI Converter, it is shown as diagram above or listed as below:

  1. RX -> TX
  2. TX -> RX
  3. VCC -> VCC
  4. CH_EN -> VCC
  5. GPIO-0 -> GND
  6. GND -> GND

The pins for ESP8266 is also labeled as diagram 2.

After done uploading the code to ESP8266, simply connect it to ESP01/01S Relay Module.

Step 3: Uploading the Code

For coding part, change the SSID and PASSWORD to your WiFi SSID and Password respectively. In the code, it is mentioned that serial begin is 115200, so make sure that the serial monitor is 115200 otherwise it will not display anything. We set the URL to https://192.168.0.178/ and it will be used later on.

Step 4: Results

When we access the URL, it will show as diagram 1 above. Make sure that you have been activated the ESP8266 along with ESP01/01S Relay Module with a range of 5V to 12V so that you can access the URL. The results for the module is shown as diagram 2 which LED lighted up indicated that the relay is ON.

Once we pressed the OFF in URL, the relay will be turned OFF simultaneously and vice versa for ON option.

6 People Made This Project!

Recommendations

  • Laser Challenge

    Laser Challenge
  • Unusual Uses Contest

    Unusual Uses Contest
  • Made with Math Contest

    Made with Math Contest

82 Comments

0
Dr Chandan Parashar

mine not working
the error message is below
Arduino: 1.8.12 (Windows 10), Board: "Generic ESP8266 Module, 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), dtr (aka nodemcu), 26 MHz, 40MHz, DOUT (compatible), 1MB (FS:64KB OTA:~470KB), 2, nonos-sdk 2.2.1+100 (190703), v2 Lower Memory, Serial, None, Only Sketch, 115200"
Executable segment sizes:
IROM : 246344 - code in flash (default or ICACHE_FLASH_ATTR)
IRAM : 28148 / 32768 - code in IRAM (ICACHE_RAM_ATTR, ISRs...)
DATA : 1260 ) - initialized variables (global, static) in RAM/HEAP
RODATA : 1264 ) / 81920 - constants (global, static) in RAM/HEAP
BSS : 25208 ) - zeroed variables (global, static) in RAM/HEAP
Sketch uses 277016 bytes (28%) of program storage space. Maximum is 958448 bytes.
Global variables use 27732 bytes (33%) of dynamic memory, leaving 54188 bytes for local variables. Maximum is 81920 bytes.
esptool.py v2.8
Serial port COM4
Connecting........_____....._____....._____....._____....._____....._____....._____
Traceback (most recent call last):
File "C:\Users\Dr. Chandan Parashar\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.7.1/tools/upload.py", line 65, in <module>
esptool.main(cmdline)
File "C:/Users/Dr. Chandan Parashar/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 2890, in main
esp.connect(args.before)
File "C:/Users/Dr. Chandan Parashar/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.7.1/tools/esptool\esptool.py", line 483, in connect
raise FatalError('Failed to connect to %s: %s' % (self.CHIP_NAME, last_error))
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header
This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

can you tell what is the mistake I am committing

0
SauvikB
SauvikB

Reply 19 days ago

This happens when your ESP01 is not in program mode, you need to connect GPIO0 to ground -> and then reconnect your ESP

0
Gazzzza
Gazzzza

Reply 1 year ago

I have/had the same problem, spent a couple of days trying, put a 10uF accross the 3.3v, seemed to make it better, then I found that is I momentarily disconnected the GPIO0 when it go to this bit "Connecting........_____", it continued on loading the data all OK, why I don't know...If I find out more, I will report.

0
crossy7980
crossy7980

Reply 1 year ago

Are you sure you have the correct port and have your Tx and Rx the right way round? It looks like the ESP isn't responding at all.
I've had one occasionally sit looking at me which recovered after pulling the USB and re-plugging.

0
crossy7980
crossy7980

1 year ago

Well, after spending an inordinate time trying to solve the problem of the ESP webserver becoming unresponsive (but with the device still responding to a ping) including adding a watchdog and sending a heartbeat from the IoT controller I gave up and moved over to ESPAsyncWebServer which you can get here:-
https://github.com/me-no-dev/ESPAsyncWebServer/

Rui Santos has an excellent working example here:-
https://raw.githubusercontent.com/RuiSantosdotme/Random-Nerd-Tutorials/master/Projects/ESP8266/ESP8266_Relay_Module_Web_Server.ino
You'll need to change NUM_RELAYS and the GPIO assigned to your relays but after that it just works and has not locked up in a couple of days (touch wood).

All good fun :)

0
Tav0809
Tav0809

Reply 6 months ago

I too encountered this issue and managed to solve it.

SOLUTION:
Add:

client.stop();

to the end of the loop cycle and

client.setTimeout(10);

to the top of the loop cycle.

This will disconnect the clients and prevent the hang/freeze. Be aware that with these modifications, the server will accept the command (ON or OFF) and instantly disconnect you.
This is an ideal scenario if you have a third party app controlling the relay since its the fastest way to activate the relay and free the webserver.

0
nicksweb
nicksweb

Reply 8 months ago

I experienced the same issues with the server becoming unresponsive -
tried implementing no sleep options etc but nothing seemed to work. Your
suggestion of ESPAsyncWebServer worked for me. For those looking to get
this working with the same system as described in this article, it will
take a bit of effort as you'll need to install about three libraries
and as mentioned by @crossy7980 you'll need to update the code to suit -
but after that it's smooth sailing with the USB programmer and ESP101S.

0
Borbastico
Borbastico

7 months ago

It works, just that my Network changed the IP address, so I had to search on my router for the new IP address.

0
Average1Guy
Average1Guy

9 months ago on Step 4

I've noticed that the relay engages momentarily when first powered on. I moved the write to before the pinmode to see if it helped but it didn't. Depending on the application, this could be problematic.

0
lotus49
lotus49

1 year ago on Step 4

Thank you for the Instructable that was clear and informative but I'm baffled by something.

This didn't work for me. The sketch works fine when the ESP-01 is connected to a UART to USB adapter but when it's connected to the relay module, nothing happens and I think I know why.

All the reports I have read suggest that these modules have two faults that prevents them booting, which is why I'm baffled. I don't understand how this worked for anyone. The two issues are that the CH_PD pin is not pulled high and GPIO0, which needs to be pulled high at boot time is actually pulled to ground via a 10k resistor. The descriptions I've seen elsewhere suggest that this can only work if the CH_PD pin is permanently connected to Vcc and either that the 10k resistor pulling GPIO0 is removed or another is added to connect GPIO0 to Vcc. I'm going to try this today. If it works (as has been widely reported elsewhere), I'll report back.

[Edit]
I have now tried this and it worked.
I had to remove the 10k resistor marked as R2 on my board (which looks absolutely identical to yours) and connect CH_PD to Vcc to get the ESP-01 module to boot. Once I'd done that, everything worked exactly as you'd expect. Until I did this, it didn't even boot. This is a fundamental design flaw with this board. The fix is very easy if you have the tools to desolder tiny SMD components. I suppose you could just break the resistor with wire cutters if you cannot desolder the resistor.

0
19cool53
19cool53

Reply 1 year ago

Looks like it's depending on the manufacturer.
Most of my devices work out of the box while plugged into USB with an USB->Serial converter. However, the chips I got with the relay boards refused to connect.
After -fix- wiring the CH_EN to VCC it works perfectly adn flashed latest Tasmota (8.x)
Suggest to solder a 4k7 resistor between VCC and CH_EN.
Thx for the hint.

0
KobiT1
KobiT1

Reply 10 months ago

Thanks a lot for this...

I was able to figure out that CH_EN wasn't pulled up and made this change already, but, couldn't understand why the ESP-01 won't connect unless I pull it out and back in at a certain angle which probably prevented GPIO0 from being pulled down for just enough to load normally.

After removing R2 it works just fine.

0
thhudson
thhudson

Reply 1 year ago

CH_PD ? Do you mean CH_EN?
Theses no CH_PD on the picture above!

0
federicksly1927
federicksly1927

Reply 1 year ago

Yeah it's the same. CH_PD = CH_EN

0
viriyah1983
viriyah1983

Question 11 months ago

I am seeking Application Matching with this project (android)

0
vtnguyen0611
vtnguyen0611

1 year ago

I got it working OK within my WLAN. How could I get control it over the internet?

0
gald33
gald33

Reply 11 months ago

You should add a forwarding rule in your router from a specific port of your choosing (say 1234) to your wlan ip address of the esp.
When connecing from the internet add a colon followed by 1234 after your (external) ip.
If you have a dynamic address from youe isp then you might want to use a service like duckdns.org to make sure you have a 'permanent address'.

0
jebepav362
jebepav362

1 year ago on Introduction

I'm using the ESP-01S/ESP8266-Relais. Upload and relay-website works fine.
But if I connect the ESP-01S to the relais-socket and power up,
there is no wifi-connection.

If connect only the VCC und GND, the wifi connects.

After that I can manually connect the GPIO0 (orange cable) to the relais-board und then everything works.

How do I change the configuration, so that I can just power up the device??
Should I remvoe an resistor? Thanks.

relais88.jpg
0
vishalgarg652
vishalgarg652

1 year ago

Did anyone faced any problem...such as relay getting on/off continuously as soon as the relay board is powered on. I am facing this irritating problem, any help?

0
nsetiaji
nsetiaji

Reply 1 year ago

i assume you're using v4 module. just add a 470uf/16v elec. capacitor direcly on optocoupler pin 1 (+) and 2 (-)

FP7YS39K4PEMONQ.jpg