ESP8266 - 2-Channel Relay Module

This tutorial instructs you how to use ESP8266 to control a 2-channel relay module. In detail, we will learn:

When we want to control two high-voltage devices such as pumps, fans, or actuators, we have two choices. We can either use multiple relay modules or opt for a simpler solution. The simpler way is to use a 2-channel relay module, which is a single board that already has two relays integrated into it. This approach makes the setup easier and more convenient for controlling both devices.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×2-channel Relay Module
1×Jumper Wires
1×Breadboard
1×(Optional) 5V Power Adapter for ESP8266
1×(Optional) Screw Terminal Expansion Board for ESP8266

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of 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 ESP8266
  • Signal pins:
    • IN1: this pin receives the control signal from ESP8266 to control relay 1 on the module
    • IN2: this pin receives the control signal from ESP8266 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

    Additionally, the 2-channel relay module includes 2 jumpers that allow you to choose between the low-level trigger and high-level trigger for each relay individually.

    If you're interested in learning the fundamentals of relays, I recommend checking out the ESP8266 - Relay tutorial. The tutorial provides detailed information on:

    • How to connect relay to high-voltage devices
    • The terms normally closed and normally open
    • The terms low-level trigger and high-level trigger
    • How to control relay using ESP8266

Wiring Diagram

The wiring diagram between ESP8266 NodeMCU and 2-channel relay module

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

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 ESP8266
  • 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 ESP8266 (not shown in the image) can be connected either through a USB cable or a power jack.

The wiring diagram between ESP8266 NodeMCU and 2-channel relay module external power source

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 ESP8266 and the 2-channel relay module.

The wiring diagram between ESP8266 NodeMCU and 2-channel relay module  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 ESP8266 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);

ESP8266 Code

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-2-channel-relay-module */ #define PIN_RELAY_1 D6 // The ESP8266 pin connected to the IN1 pin of relay module #define PIN_RELAY_2 D7 // The ESP8266 pin connected to the IN2 pin of relay module // The setup function runs once on reset or power-up void setup() { Serial.begin(9600); // Configure the ESP8266 pin as an digital output. pinMode(PIN_RELAY_1, OUTPUT); pinMode(PIN_RELAY_2, OUTPUT); } // The loop function repeats indefinitely void loop() { Serial.println("Turned on both relays"); digitalWrite(PIN_RELAY_1, HIGH); digitalWrite(PIN_RELAY_2, HIGH); delay(2000); Serial.println("Turned off both relays"); digitalWrite(PIN_RELAY_1, LOW); digitalWrite(PIN_RELAY_2, LOW); delay(2000); }

Detailed Instructions

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Listen the click sound on relays.
  • Check out the result on the 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

Function References

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!