Relay is essential for creating DIY home automation projects. Connecting it to a WiFi-enabled ESP32 allows you to control your home appliances from your mobile device, laptop, or remote location.
However, handling mains voltages can be hazardous. If you must use it, do so with extreme caution and for continuous use, it is best to opt for quality relay modules and to confirm its installation by a professional before use.
Using a relay is very simple, mainly when it is sold as a ready-to-use module.
I strongly recommend using a module that incorporates a relay with its minimal circuitry rather than utilizing the relay alone and making the circuit for the logic part yourself (the relay itself does not fit on a breadboard and should not be placed on it anyway).
If you have never used a relay before or if you are curious to understand how it works, its uses and its limits, I recommend you to read the theoretical article on how a relay works .
Relay is a device that can be used with ESP32 to control the power of a DC electrical circuit, such as RGB LED strips or pumps. It can also control appliances connected to the 220V mains, such as fans, lights, and heaters.
If the purpose is to switch a motor on and off frequently, the transistor circuit would be more appropriate (for direct current, of course).
In most Arduino kits, the modules use the relay SRD-05VDC-SL-C from the manufacturer Songle . There are generally two varieties: a module with a single relay and one with several relays simultaneously. If you plan to drive several power circuits separately in a project, choose the multi-relay module instead.
If you’re using an ESP32 powered by a LiPo battery, the available voltage will usually fall in the range of 3.3V to 4.2V. As such, the SRD-03VDC-SL-C model, which runs on 3.3V, is more suitable than the SRD-05VDC-SL-C model, which requires 5V.
When a relay is used, the grounds of the logic part and the power part are entirely separate and must never be connected, primarily if the circuit is supplied with 220V AC.
The model SRD-05VDC-SL-C has three pins to drive the relay:
The signal pin
Sdrives the relay, which is connected to a pin of the ESP32.
The power supply pin is connected to the
5Vmodule of the ESP32 (or the
3V3if it is the SRD- module 03VDC -SL-C).
The final pin, indicated by
-is the ground, is linked to pin
There is also a 3-pin terminal block at the power side:
NC→ Normally Closed contact.
COM→ the middle pin is called common (COM).
NO→ Normally Open: Normally Open contact.
Depending on the application, the power circuit device must be connected to the terminal
COM and either
NC . If you select
NO , the relay will be in an open state by default (the electrical circuit will not be complete). The relay will only close the circuit when a signal is received.
The circuit is closed by default with the
NC terminals selected, meaning the connected device is on. When the relay activates, the circuit opens, and the device no longer receives power.
In the event of a control fault (when the relay is out of service), selecting the safest possible mode is essential. For instance, if a heating resistor needs to be powered, it is best to ensure the circuit remains open if the relay stops working.
I suggest using the combination
NO to operate any device, such as a lamp, motor, or pump.
Here is an example of using a relay to turn on a large computer fan powered by 12V.
The MicroPython script is straightforward, like turning on or off an LED. The code is identical to the script
from machine import Pin import time pin_relay = Pin(32, mode=Pin.OUT) while True: pin_relay.on() time.sleep_ms(200) pin_relay.off() time.sleep_ms(5000)
And here is what it gives with the proposed circuit:
The module has an LED to indicate the status of the relay. It lights up when the relay is activated.
There are a variety of relay modules available, ranging from one to eight channels. Each module can control a corresponding number of outputs and typically contains a blue SRD-05VDC-SL-C relay.
Models with two or more channels usually incorporate an optocoupler to isolate the relay from the ESP32 completely.
An optocoupler is a device that consists of a light-sensitive transmitter and receiver, enabling complete isolation of the logic signal.
For this layer of protection to be effective, the relay circuit must be**supplied with an external voltage source of 5V** on pin
JD-VCC . These relay modules contain additional pins with a jumper
GND to enable or disable this option.
The optocoupler is useless if the jumper is positioned between
VCC . It may be necessary to use an external power supply if the relays draw too much current for the electromagnet.
The operation of a multi-channel relay is similar to that of a single relay. Multiple pins
VIN2 can be used to control the relays independently:
from machine import Pin import time pin_relay_0 = Pin(32, mode=Pin.OUT) pin_relay_1 = Pin(33, mode=Pin.OUT) while True: pin_relay_0.on() pin_relay_1.on() time.sleep_ms(200) pin_relay_0.off() pin_relay_1.off() time.sleep_ms(5000)
For permanent use, please take a quality relay module from a reliable manufacturer, not the cheapest available on Aliexpress. 😉