Relay modules paired with miniature boards ESP8266 ESP-01/01S Smart home control devices have become one of the most popular solutions for creating smart home systems. These devices allow remote control of household appliances via Wi-Fi, saving energy and increasing convenience. However, novice users often encounter connection difficulties: incorrect power supply circuits, firmware errors, or unstable relay operation due to improper contact selection.
In this article we will look at Step-by-step connection of the relay module to the ESP-01/01S at 5V, including component selection, circuit assembly, firmware and configuration through popular platforms such as Blynk or Home AssistantWe'll pay special attention to typical errors that lead to module burnout or incorrect operation, as well as energy optimization for off-grid projects.
1. Selecting a Relay Module and ESP8266 ESP-01/01S: Key Differences
Before you start connecting, it's important to understand which modules you need. ESP8266 ESP-01 And ESP-01S they look similar, but have critical differences:
- 🔹 ESP-01: Classic version with 1 MB of memory, without a built-in voltage regulator. Requires an external power source. 3.3B.
- 🔹 ESP-01S: an improved modification with 4 MB of memory and a built-in stabilizer, which simplifies connection to 5V (but with nuances!).
- 🔹 Relay module: can be 5V, 12V or 24V. Only the following is suitable for the ESP-01/01S module with opto-isolator and logic level 3.3V–5V, otherwise the board will burn out.
Pay attention to the relay marking: if the module says "5V Relay", this means that the relay coil is rated for 5V, but control signal (from ESP) can be either 3.3V or 5V. Check the datasheet for the parameter "Trigger Voltage"For reliability, use modules with opto-isolators—they protect the ESP from voltage surges when switching loads.
⚠️ Caution: Relay modules without opto-coupling may interfere with the ESP power supply, causing random board reboots. If your project is stability-critical (e.g., heating control), choose relays with galvanic isolation.
| Component | ESP-01 | ESP-01S | 5V relay module |
|---|---|---|---|
| Supply voltage | 3.3V (strictly!) | 3.3V or 5V* (via VCC) | 5V (coil) |
| Logical level | 3.3B | 3.3B | 3.3V–5V (depending on model) |
| Max. relay load current | — | — | 10A (250V) or 15A (120V) |
| Opto-isolator | No | No | Recommended |
*ESP-01S can be powered from 5V via contact VCC, but logical inputs (GPIO) 3.3V remains!
2. Necessary components and tools
To assemble a working circuit you will need:
- 🛠️ ESP8266 ESP-01/01S — 1 pc.
- 🔌 5V relay module with 1-4 channels (for example, HL-52S or Songle SRD-05VDC-SL-C).
- 🔋 Power supply 5V/2A (for example, a smartphone charger or a module LM2596).
- 🔄 Logical level converter (optional if the relay requires 5V at the input).
- 🔌 Terminal blocks or soldering for connecting wires.
- 🖥️ USB-UART adapter (For example, CP2102 or FT232RL) for firmware.
If you plan to control high-power appliances (such as a heater or pump), add a circuit breaker to the appropriate current. A 220V LED lamp can be used for testing.
3. Wiring diagram of the relay module to the ESP8266 ESP-01/01S
The most common mistake is connecting the ESP directly to the relay without taking into account the voltage levels. Below is safe scheme for ESP-01S with 5V relay:
1. Nutrition:
- Connect VCC relay to 5V power source.
- VCC Connect ESP-01S to 3.3V (if you use an external stabilizer) or to 5V (if you supply power through the built-in regulator).
- GND Combine all components together.
2. Control signal:
- Choose a free one GPIO on ESP (for example, GPIO0 or GPIO2).
- Connect it to the contact IN relay via resistor 1 kOhm (for reverse current protection).
- If the relay requires 5V at the input, use level converter (For example, TXB0104).
3. Load:
- To contacts COM (general) and NO (normally open) relay connect the controlled device (for example, a 220V lamp through a socket).
5V Relay → ESP-01S
VCC → 5V (source)
GND → GND
IN → GPIO2 (via 1kOhm resistor)
COM/NO → Load 220V
⚠️ Caution: Never connect a 220V load directly to the ESP contacts! The relay must break the circuit. only from the phase side (not neutral!). For safety, use a residual-current circuit breaker in the panel.
Why can't I control the relay directly from GPIO without a resistor?
Without a resistor, current surges during switching may occur, which could damage the GPIO port. The resistor limits the current and protects against back EMF in the relay coil.
4. ESP8266 Firmware: Selecting Firmware and Example Code
To control the relay via Wi-Fi you can use:
- 📱 Ready-made firmware: Tasmota, ESPHome (integration with Home Assistant), Blynk.
- 💻 Self-written code: in language Arduino IDE with the library
ESP8266WiFi.
An example of minimal code for the Arduino IDE (controlling a relay via a web interface):
#include <ESP8266WiFi.h>
const char* ssid = "Your_WiFi";
const char* password = "password";
int relayPin = 2; // GPIO2
WiFiServer server(80);
void setup() {
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Relay is off at startup
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
String request = client.readStringUntil('\r');
if (request.indexOf("/on") != -1) digitalWrite(relayPin, LOW);
if (request.indexOf("/off") != -1) digitalWrite(relayPin, HIGH);
client.println("HTTP/1.1 200 OK");
client.println("Relay: " + String(digitalRead(relayPin) == LOW ? "ON" : "OFF"));
client.stop();
}
}
After downloading the code:
- Open
Serial Monitorin the Arduino IDE to see the ESP's IP address. - In your browser, go to the address
http://[IP_ESP]/onto turn on the relay or/offto turn off.
Check USB-UART adapter drivers|Install ESP8266 library in Arduino IDE|Select correct board (Generic ESP8266 Module)|Disconnect GPIO0 from GND before flashing|Connect TX/RX of adapter to RX/TX of ESP (crosswise)
-->
5. Setup via Blynk or Home Assistant
For convenient control of the relay from a mobile application, this is suitable Blynk:
- Download the app Blynk and create a new project.
- Add a widget
Buttonand set it toDigitalpin (for example,D2). - Copy Auth Token from Blynk's letter.
- Upload the code with the library to the ESP
BlynkSimpleEsp8266.h, specifying the token and Wi-Fi data.
To integrate with Home Assistant use ESPHome:
- Install ESPHome Dashboard (add-on to Home Assistant).
- Create a config:
esphome:
name: esp_relay
platform: ESP8266
board: esp01_1m
wifi:
ssid: "Your_WiFi"
password: "password"
switch:
- platform: gpio
pin: GPIO2
name: "Relay 1"
After compiling and loading the firmware, the relay will appear in the Home Assistant interface as a switch.
6. Typical problems and their solutions
If the relay does not switch or the ESP constantly resets, check:
- 🔋 Malnutrition: The ESP-01/01S are sensitive to voltage drops. Use a power source with a current of at least 500 mA.
- 🔌 GPIO conflict: Pin
GPIO0It is used for the firmware mode. If you connect a relay to it, the ESP may not start. UseGPIO2. - 📶 Wi-Fi interference: If the ESP loses connection, add a capacitor 1000 μF between
VCCAndGNDrelay. - ⚡ Back EMF: When driving an inductive load (such as a motor), connect diode 1N4007 parallel to the relay coil.
If ESP does not connect to Wi-Fi:
- Please check that you entered the correct password (case sensitive!).
- Make sure that your router does not block the device by MAC address.
- Update your ESP firmware to the latest version via
Arduino IDE.
⚠️ Caution: When using a relay to control lighting, keep in mind that some LED lamps have built-in drivers that may conflict with mechanical relays. In this case, use solid state relays (SSR).
7. Optimizing energy consumption for autonomous projects
If your project is powered by a battery or solar panel, reduce ESP consumption:
- 🔋 Deep Sleep Mode: ESP-01/01S can wake up by a timer or an external signal (for example, from a motion sensor).
- 📡 Disabling Wi-Fi: Use the command
WiFi.mode(WIFI_OFF)in the code when the connection is not needed. - ⚡ Undervoltage: Powering the ESP from 3.3V (not 5V) reduces heating and consumption.
Sample code for the mode Deep Sleep with awakening every 5 minutes:
#include <ESP8266WiFi.h>
void setup() {
// Your relay control code
ESP.deepSleep(300e6); // 300 seconds = 5 minutes
}
void loop() {} // Not used
To power the 18650 battery, use a step-up converter module. MT3608to get stable 5V for the relay and 3.3V for the ESP.
Frequently Asked Questions
Is it possible to control a 220V relay directly from an ESP8266?
No! The ESP8266 is not designed to handle high voltage. The relay must be intermediate link, breaking the 220V circuit. The ESP board itself controls only relay coil (5V or 3.3V).
Why does the relay click, but the load does not turn on?
Possible reasons:
- Incorrect connection of the load (for example, to
NCinstead ofNO). - Weak contact in the 220V circuit (check the tightness of the terminals).
- The relay contacts are burnt out (check with a multimeter in continuity mode).
How to connect multiple relays to one ESP-01?
ESP-01/01S has only 2 free GPIOs (GPIO0 And GPIO2). To control 4 relays use:
- 🔌 Port expander (For example, PCF8574 via I2C).
- 🔄 Shift register (For example, 74HC595).
- 🔧 ESP-12E with a large number of GPIOs.
Which firmware is best for a smart home?
Depends on your ecosystem:
- 🏠 Home Assistant → ESPHome (maximum integration).
- 📱 Blynk → official firmware Blynk.
- 🔧 Universal solution → Tasmota (supports MQTT, HTTP, automation rules).
Is it possible to use ESP-01 without USB-UART adapter?
Technically yes, but it's inconvenient. Flashing requires an adapter. After flashing, you can control the ESP via Wi-Fi, but debugging or updating will still require the adapter. An alternative is to use OTA firmware (over the air), but it requires initial setup via UART.