ESP32 - 2-Channel Relay Module

This tutorial provides step-by-step instructions on utilizing an ESP32 to control a 2-channel relay module. It covers the following aspects in detail:

When we need to control two high-voltage devices like pumps, fans, or actuators, we have two options. We can either use multiple relay modules or go for a more straightforward solution. The easier way is to use a 2-channel relay module, which is a single board with two relays built-in. This makes the setup simpler and more convenient for controlling both devices.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×2-channel Relay Module
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP32
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to 2-Channel Relay Module

Pinout

2-channel Relay Module Pinout

A 2-channel relay module has the following pins:

  • Power pins for relay boards
    • DC+: connect this pin to 5V pin of power supply
    • DC-: connect this pin to the GND pin of the power supply and also to the GND pin of the ESP32
  • Signal pins:
    • IN1: this pin receives the control signal from ESP32 to control relay 1 on the module
    • IN2: this pin receives the control signal from ESP32 to control relay 2 on the module
  • Output pins: NCx (normally closed pin), NOx (normally open pin), COMx (common pin),
    • NC1, NO1, COM1: These pins connect to a high-voltage device that is controlled by relay 1
    • NC2, NO2, COM2: These pins connect to a high-voltage device that is controlled by relay 2

    Moreover, the 2-channel relay module features two jumpers that grant you the flexibility to select either a low-level trigger or high-level trigger for each relay independently.

    If you're keen on understanding the basics of relays, I highly recommend checking out the ESP32 - Relay tutorial. This tutorial offers comprehensive insights into:

    • Connecting relays to high-voltage devices
    • Explaining the concepts of normally closed and normally open
    • Describing the differences between low-level trigger and high-level trigger
    • Demonstrating how to effectively control relays using ESP32

Wiring Diagram

ESP32 2-channel relay module wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

If you plan to use the 5V pins to power additional components, there's a chance that the relay module won't receive enough power. Therefore, it's essential to use a separate 5V power source specifically for the module.

So, we need to use three types of power sources:

  • A 5V power adapter for ESP32
  • A 5V power adapter for the 2-channel relay module
  • One or several higher-voltage power adapters (12VDC, 24VDC, 48VDC, 220AC...) for things that are controlled by the 2-channel relay module

Below is the wiring diagram using three power sources. The power supply for the ESP32 (not shown in the image) can be connected either through a USB cable or a power jack.

ESP32 2-channel relay module external power source wiring diagram

This image is created using Fritzing. Click to enlarge image

To reduce the number of power adapters required, we can simplify things by using a single 5V power supply for both the ESP32 and the 2-channel relay module.

ESP32 2-channel relay module wiring diagram two power source

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If the two devices controlled by a 2-channel relay module operate at the same voltage, we can utilize a single high-voltage power adapter to supply power to both devices. However, if the devices require different voltages, we can independently use separate high-voltage power adapters for each device.

How To Program For 2-Channel Relay Module

  • Initializes the ESP32 pin to the digital output mode by using pinMode() function.
pinMode(PIN_RELAY_1, OUTPUT); pinMode(PIN_RELAY_2, OUTPUT);
digitalWrite(PIN_RELAY_1, HIGH); digitalWrite(PIN_RELAY_2, HIGH);

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-2-channel-relay-module */ #define PIN_RELAY_1 27 // The ESP32 pin GPIO27 connected to the IN1 pin of relay module #define PIN_RELAY_2 26 // The ESP32 pin GPIO26 connected to the IN2 pin of relay module // the setup function runs once when you press reset or power the board void setup() { Serial.begin(9600); // initialize digital pin as an output. pinMode(PIN_RELAY_1, OUTPUT); pinMode(PIN_RELAY_2, OUTPUT); } // the loop function runs over and over again forever void loop() { Serial.println("Turn on both relays"); digitalWrite(PIN_RELAY_1, HIGH); digitalWrite(PIN_RELAY_2, HIGH); delay(2000); Serial.println("Turn off both relays"); digitalWrite(PIN_RELAY_1, LOW); digitalWrite(PIN_RELAY_2, LOW); delay(2000); }

Quick Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP32
  • Listen the click sound on relays.
  • See the result on Serial Monitor.
COM6
Send
Turned on both relays Turned off both relays Turned on both relays Turned off both relays Turned on both relays Turned off both relays Turned on both relays Turned off both relays
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

Function References

※ OUR MESSAGES