Skip to content

uPesy now ships to the entire European Union

Livraison à partir de seulement 2.50€ !

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Control electrical devices with a Relay and an ESP32 in MicroPython

(Updated at 01/09/2023)

relay SRD-03VDC-SL-C Songle

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.

Warning

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.

Getting started with a Relay

Using a relay is very simple, mainly when it is sold as a ready-to-use module.

Note

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).

Presentation and functioning of a Relay

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.

Note

If the purpose is to switch a motor on and off frequently, the transistor circuit would be more appropriate (for direct current, of course).

Connection of the SRD-05VDC-SL-C Relay

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.

Note

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.

relay circuit diagram esp32 Arduino code

Electrical circuit to be made

Warning

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.

Pin Wiring

Relay Module

ESP32

S

32

VCC

5V

GND

GND

The model SRD-05VDC-SL-C has three pins to drive the relay:

  • The signal pin S drives the relay, which is connected to a pin of the ESP32.

  • The power supply pin is connected to the 5V module of the ESP32 (or the 3V3 if it is the SRD- module 03VDC -SL-C).

  • The final pin, indicated by - is the ground, is linked to pin GND .

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.

internal relay schematic

Output terminals of the relay

Depending on the application, the power circuit device must be connected to the terminal COM and either NO or 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 COM and 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.

Note

I suggest using the combination COM and 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.

control of a fan via a relay from an esp32

The NO and COM terminals are used for this configuration

Controlling the Relay with Python on the ESP32

The MicroPython script is straightforward, like turning on or off an LED. The code is identical to the script blink .

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:

control of a fan via a relay from an esp32

Note

The module has an LED to indicate the status of the relay. It lights up when the relay is activated.

Des modules relais avancés multicanaux

multi-channel relay module

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.

Separate the ESP32 from the Relay with the Optocouplers

Models with two or more channels usually incorporate an optocoupler to isolate the relay from the ESP32 completely.

Note

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 JD-VCC , VCC and GND to enable or disable this option.

internal relay diagram with galvanic isolation

The optocoupler is useless if the jumper is positioned between JD-VCC and VCC . It may be necessary to use an external power supply if the relays draw too much current for the electromagnet.

Use multi-channel Relay modules

The operation of a multi-channel relay is similar to that of a single relay. Multiple pins VIN VIN1 , 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)

Warning

For permanent use, please take a quality relay module from a reliable manufacturer, not the cheapest available on Aliexpress. 😉

Fabriquer son propre module Sonoff