4: Interfacing Dual Channel Relay with ESP32
1: Introduction to Relays
Power relay module is an electromagnet switch controlled by a low power signal from microcontrollers such as ESP32 or Arduino. Using the control signal from the microcontroller we can turn ON or OFF the appliances that are even working on high voltages such as 120-220V.
A single channel relay module normally contains 6 pins:
The six pins include:
Pin | Pin Name | Description |
---|---|---|
1 | Relay Trigger Pin | Input for relay activation |
2 | GND | Ground Pin |
3 | VCC | Input supply for relay coil |
4 | NO | Normally open terminal |
5 | Common | Common terminal |
6 | NC | Normally Closed terminal |
2: Different Types of Relays
Relay modules come in different variations depending upon the number of channels it has. We can easily find relay modules with 1,2,3,4,8 and even 16 channels relay modules. Each channel determines the number of devices we can control on the output terminal.
Here is a brief comparison of single, dual and 8 channel relay module specifications:
Specification | 1-Channel Relay | 2-Channel Relay | 8-Channel Relay |
---|---|---|---|
Supply Voltage | 3.75V-6V | 3.75V-6V | 3.75V-6V |
Trigger Current | 2mA | 5mA | 5mA |
Current Active Relay | 70mA | Single(70mA) Dual (140mA) | Single(70mA) All 8 (600mA) |
Max Contact Voltage | 250VAC or 30VDC | 250VAC or 30VDC | 250VAC or 30VDC |
Minimum Current | 10A | 10A | 10A |
As we have covered a brief comparison between different channel relays now, we will be using dual channel relay in this article for demonstration purposes.
3: 2-Channel Relay PinOut
Here in this article, we will be using dual channel relay. A dual channel relay pins can be divided into three categories:
- Mains Voltage Connections
- Control Pins
- Power Supply Selection
3.1: Main Voltage Connections
Main connection inside a dual channel relay module includes two different connectors with each connection having three pins NO (Normally Open), NC (Normally Closed) and Common.
Common: Control the main current (Supply voltage of external device)
Normally Closed: Using this configuration relay is set to closed by default. In normal configuration current is flowing between common and NC unless a trigger signal is sent to open the circuit and stop the current flow.
Normally Open: Normally open configuration is opposite to NC. By default, current is not flowing; it only starts to flow when a trigger signal is sent from ESP32.
3.2: Relay Control Pins
The other side of the relay module includes a set of 4 and 3 pins. The first set of low voltage sides contains four pins VCC, GND, IN1 and IN2. The IN pin varies depending upon the number of channels there is a separate IN pin for each channel.
The IN pin receives the control signal for relay from any microcontroller. When the received signal goes below 2V the relay is triggered. Following configuration can be set using the relay module:
Normally Closed Configuration:
- 1 or HIGH current START to flow
- 0 or LOW current STOP flowing
Normally Open Configuration:
- 1 or HIGH current STOP flowing
- 0 or LOW current START to flow
3.3: Power Supply Selection
The second set of pins includes the three pins VCC, GND and JD-VCC. The JD-VCC pins are normally connected to VCC which means the relay is powered using the ESP32 voltage and we don’t need an external power source separately.
If you remove the black cap connector shown in image above, then we have to power the relay module separately.
As of now we have covered all the specifications and working of the dual channel relay module. Now we will interface it with ESP32.
4: Interfacing Dual Channel Relay with ESP32
Now we will use any single channel from the relay module and control a LED using the ESP32 signal. Using the same technique any of the AC appliances can also be controlled but we have to power them separately. We will use the first channel of the relay module.
4.1: Schematic
Now connect the relay module as shown in image below. Here we have used GPIO pin 13 of ESP32 for the trigger signal of the relay module. A LED is connected in NC configuration.
Following pin configuration will be followed:
Relay Pin | ESP32 Pin |
---|---|
IN1 | GPIO 13 |
VCC | Vin |
GND | GND |
Channel 1 NC | LED +ive Terminal |
Common | Vin |
4.2: Code
Open Thonny IDE. Connect ESP32 with PC and upload the given MicroPython script.
from time import sleep
relay = Pin(13, Pin.OUT) # GPIO PIN 13 for RELAY Input Signal
while True:
relay.value(0) # RELAY ON for 10 SEC in Normally Close Mode
#For Normally Open change the wire configuration from RELAY Module
sleep(10)
relay.value(1) # RELAY OFF for 10 SEC in Normal Close Mode
sleep(10)
Here in above code GPIO 13 is defined as a trigger pin connected to IN1 of the relay module. Next, we defined a relay module in NC configuration which turns ON LED unless a HIGH signal is sent at IN1 from ESP32.
In case one wants to set NO configuration send a HIGH signal at IN1 to turn ON LED.
After uploading code in the ESP32 board now observe the output.
4.3: Output
As LED is connected in NC configuration so LED is ON, but the relay module channel 1 LED is OFF.
Now a HIGH signal is sent at IN1 pin the LED turn OFF but now the relay module channel 1 LED is ON.
We have successfully integrated and tested the ESP32 microcontroller board with a dual channel relay module. For demonstration purposes we connected a LED at Common terminal of channel 1.
Conclusion
Using a relay with ESP32 is a great way of controlling multiple AC devices not only using a wired connection but can also be controlled remotely. This article covers all the steps needed to control a relay with ESP32 using the MicroPython script. Here we used the Thonny IDE editor for writing MicroPython code. Using this article any channel relay module can be controlled using MicroPython code.