ESP01/01S RELAY MODULE TUTORIAL

97,811

54

74

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.

3 People Made This Project!

Recommendations

  • Battery Powered Contest

    Battery Powered Contest
  • Plywood Challenge

    Plywood Challenge
  • Plastic Contest

    Plastic Contest

74 Discussions

0
jebepav362
jebepav362

27 days 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

2 months 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 2 months ago

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

FP7YS39K4PEMONQ.jpg
0
Gazzzza
Gazzzza

Reply 5 weeks ago

I got the HW-655 with the ESP8266 and when I power it up (before even programming the ESP8266) I got the relay on the HW-655 operating and releasing about every second, haven't worked out why yet, if anyone knows why, I'd love to hear, cheers

s-l640.jpg
0
Dr Chandan Parashar
Dr Chandan Parashar

5 months ago

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
Gazzzza
Gazzzza

Reply 5 weeks 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 5 months 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
lotus49
lotus49

6 months 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 8 weeks 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
thhudson
thhudson

Reply 5 months ago

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

0
federicksly1927
federicksly1927

Reply 3 months ago

Yeah it's the same. CH_PD = CH_EN

0
vtnguyen0611
vtnguyen0611

3 months ago

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

0
crossy7980
crossy7980

5 months 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
crossy7980
crossy7980

5 months ago

All working OK for me thanks.

I've added code to store the state of the relay in eeprom (it's actually the flash on the ESP) so it remembers the state of the relay in the event of a power failure / reset.

0
YJ4
YJ4

Reply 5 months ago

Can you please share the code? That's a good feature to have.

0
crossy7980
crossy7980

Reply 5 months ago

Sure, you'll need to download the ESP_EEPROM.h library from github.
I've also added a Ticker based watchdog, currently set to 60 seconds, set it to whatever you like, which stores the number of times it's been triggered also in eeprom so you can check how many times the beast has locked up. Watchdog resets each time a command is received, I've added the HEARTBEAT command if you don't want to send relay settings.

Oh, and it does two relays.

By the way, my relays are ON when HIGH so watch your highs and lows.

It's all a bit clunky as it's really a technology prover. tidy as you see fit.

Code below:-
=============================================
/*
V1.0a added fixed IP address.
V1.0b / c added flash to retain the relay state over a reset.
V1.0d added the second relay.
V1.0e added the watchdog and HEARTBEAT command
*/
#include <ESP8266WiFi.h>
#include <ESP_EEPROM.h>
#include <Ticker.h>
// Set up for the WiFi server
IPAddress ip(192, 168, 12, 50);
IPAddress gateway(192, 168, 12, 254);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, 12, 254);
//const char* ssid = "CrossyFTTX2.4"; // fill in here your router or wifi SSID
const char* ssid = "CrossyVPN 2.4G"; // fill in here your router or wifi SSID
const char* password = "BaanFarang"; // fill in here your router or wifi password
#define RELAY 0 // relay connected to GPIO 0
#define RELAY1 2 // relay connected to GPIO 2
WiFiServer server(80);
Ticker secondTick;
// Set up for FLASH storage
byte eepromVar1 = 0;
int dogTimes = 0;
unsigned long oldmillis = 0;
volatile int watchdogCount = 0;
int value1 = LOW;
int value = LOW;
void setup()
{
Serial.begin(9600); // must be same baudrate with the Serial Monitor
EEPROM.begin(64); // 64 Bytes (minimum is 16)
EEPROM.get(0, eepromVar1); // Read the relay state from eeprom
EEPROM.get(1, dogTimes); // read the watchdog counter from eeprom
Serial.println ("");
Serial.print ("Getting relay value from eeprom ");
Serial.println (eepromVar1);
Serial.print ("Getting watchdog counter from eeprom ");
Serial.println (dogTimes);
pinMode(RELAY,OUTPUT);
pinMode(RELAY1,OUTPUT);
if (bitRead(eepromVar1,0) == 0) { digitalWrite(RELAY,LOW);} // and write it to the relay pins
else { digitalWrite(RELAY,HIGH);}
if (bitRead(eepromVar1,1) == 0) { digitalWrite(RELAY1,LOW);} // and write it to the relay pins
else { digitalWrite(RELAY1,HIGH);}
secondTick.attach(1, ISRwatchdog);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.config(ip, gateway, subnet, DNS);
delay (100);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
// ********** Watchdog ISR *******************
void ISRwatchdog () {
watchdogCount++;
if (watchdogCount == 60) {
Serial.println ("Watchdog triggered");
dogTimes ++;
EEPROM.put(1, dogTimes);
EEPROM.commit();
ESP.restart();
}
}
// ********** Main Loop Below *******************
void loop()
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client)
{
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available())
{
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
if (request.indexOf("/HEARTBEAT") != -1)
{
Serial.println("HEARTBEAT");
watchdogCount = 0; // watchdog fed
}
if (request.indexOf("/RELAY=ON") != -1)
{
Serial.println("RELAY=ON");
watchdogCount = 0; // watchdog fed
bitSet(eepromVar1 , 0);
digitalWrite(RELAY,HIGH);
value = LOW;
}
if (request.indexOf("/RELAY=OFF") != -1)
{
Serial.println("RELAY=OFF");
watchdogCount = 0; // watchdog fed
bitClear(eepromVar1 , 0);
digitalWrite(RELAY,LOW);
value = HIGH;
}
if (request.indexOf("/RELAY1=ON") != -1)
{
Serial.println("RELAY1=ON");
watchdogCount = 0; // watchdog fed
bitSet(eepromVar1 , 1);
digitalWrite(RELAY1,HIGH);
value1 = LOW;
}
if (request.indexOf("/RELAY1=OFF") != -1)
{
Serial.println("RELAY1=OFF");
watchdogCount = 0; // watchdog fed
bitClear(eepromVar1 , 1);
digitalWrite(RELAY1,LOW);
value1 = HIGH;
}
// Write the eeprom
EEPROM.put(0, eepromVar1);
// EEPROM.put(1, eepromVar1);
EEPROM.commit();
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // this is a must
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<head><title>ESP8266 RELAY Control</title></head>");
client.print("Relay 0 is now: ");
if(value == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("<br>");
client.print("Relay 1 is now: ");
if(value1 == HIGH)
{
client.print("OFF");
}
else
{
client.print("ON");
}
client.println("<br><br>");
client.println("Turn <a href=\"/RELAY=OFF\">OFF</a> RELAY 0<br>");
client.println("Turn <a href=\"/RELAY=ON\">ON</a> RELAY 0<br>");
client.println("</html>");
client.println("<br>");
client.println("Turn <a href=\"/RELAY1=OFF\">OFF</a> RELAY 1<br>");
client.println("Turn <a href=\"/RELAY1=ON\">ON</a> RELAY 1<br>");
client.println("</html>");
client.println("<br><br>");
client.println("Send a <a href=\"/HEARTBEAT\">HEARTBEAT</a><br>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
}
=============================================

0
caste manizales
caste manizales

6 months ago

after check an rechek this code didn´t work (after a few minutes the server hang). if you do a example "helloserver" from examples from 8266 the server never hang. I only use the esp8266

0
crossy7980
crossy7980

Reply 5 months ago

I had the same issue.

After some Googling, it seems to have been solved by using an older version of the ESP8266 core for the Arduino IDE.

I'm using version 2.5.2.

0
obiora
obiora

6 months ago

I have tried but it not working maybe am not getting it right

0
lotus49
lotus49

Reply 6 months ago

Have a look at my comment above. These modules have fundamental design flaws that can be fixed fairly easily but only if you are handy with a soldering iron.