ESP32 - 4-Channel Relay Module

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

When it comes to managing four high-voltage devices such as pumps, fans, or actuators, we have two choices. We can either go with multiple relay modules or opt for a simpler approach. The simpler option is to use a 4-channel relay module, which is a single board equipped with four integrated relays. This streamlines the setup process, making it more convenient to control all the devices effectively.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×4-Channel Relay Module
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for ESP32
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter For ESP32

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 in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables.

Introduction to 4-Channel Relay Module

The 4-Channel Relay Module Pinout

4-Channel Relay Module pinout

A 4-channel relay module has the following pins:

  • Power pins for relay boards
    • DC+: connect this pin to the 5V pin of a power supply
    • DC-: connect this pin to the GND pin of the power supply and also to the GND pin of an ESP32
  • Signal pins:
    • IN1: this pin receives the control signal from an ESP32 to control relay 1 on the module
    • IN2: this pin receives the control signal from an ESP32 to control relay 2 on the module
    • IN3: this pin receives the control signal from an ESP32 to control relay 3 on the module
    • IN4: this pin receives the control signal from an ESP32 to control relay 4 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
    • NC3, NO3, COM3: These pins connect to a high-voltage device that is controlled by relay 3
    • NC4, NO4, COM4: These pins connect to a high-voltage device that is controlled by relay 4

    To learn about connecting a relay to high-voltage devices and understanding the distinctions between normally closed and normally open, check out the ESP32 - Relay tutorial.

    Additionally, the tutorial covers the 4 jumpers available on the relay module, which enable you to individually select between low trigger and high trigger settings for each relay.

Wiring Diagram

It is important to note that the 4-channel relay module consumes a significant amount of power. Therefore, it is strongly advised against powering it directly from the 5V pin of the ESP32. Instead, it is recommended to utilize an external 5V power source specifically for the relay module. This precaution ensures optimal performance and prevents potential issues that may arise from insufficient power supply.

Therefore, we must use three power sources:

  • A 5V power adapter for the ESP32
  • A 5V power adapter for the 4-channel relay module
  • A higher-voltage power adapter (12VDC, 24VDC, 48VDC, 220AC...) for the items that are managed by the 4-channel relay module
  • A wiring diagram with the three power sources. The power supply for the ESP32 (not included in the image) can be either via USB cable or power jack.
ESP32 4-channel relay module wiring diagram

This image is created using Fritzing. Click to enlarge image

  • We can decrease the amount of power adapters by utilizing one 5V power source for both the ESP32 and the 4-channel relay module.
ESP32 4-channel relay module wiring diagram two power source

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

If the four devices that are managed by a 4-channel relay module have the same voltage, then one high-voltage power adapter can be used for all of them. However, if the voltage is different for each device, then separate high-voltage power adapters must be used.

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.

How To Program For 4-Channel Relay Module

  • Sets the ESP32 pin to digital output mode with the pinMode() function.
pinMode(PIN_RELAY_1, OUTPUT); pinMode(PIN_RELAY_2, OUTPUT); pinMode(PIN_RELAY_3, OUTPUT); pinMode(PIN_RELAY_4, OUTPUT);
digitalWrite(PIN_RELAY_1, HIGH); digitalWrite(PIN_RELAY_2, HIGH); digitalWrite(PIN_RELAY_3, HIGH); digitalWrite(PIN_RELAY_4, 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-4-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 #define PIN_RELAY_3 25 // The ESP32 pin GPIO25 connected to the IN3 pin of relay module #define PIN_RELAY_4 33 // The ESP32 pin GPIO33 connected to the IN4 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); pinMode(PIN_RELAY_3, OUTPUT); pinMode(PIN_RELAY_4, OUTPUT); } // the loop function runs over and over again forever void loop() { Serial.println("Turn on all"); digitalWrite(PIN_RELAY_1, HIGH); digitalWrite(PIN_RELAY_2, HIGH); digitalWrite(PIN_RELAY_3, HIGH); digitalWrite(PIN_RELAY_4, HIGH); delay(1000); Serial.println("Turn off all"); digitalWrite(PIN_RELAY_1, LOW); digitalWrite(PIN_RELAY_2, LOW); digitalWrite(PIN_RELAY_3, LOW); digitalWrite(PIN_RELAY_4, LOW); delay(1000); }

Quick Instructions

To get started with ESP32 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP32 on Arduino IDE tutorial if this is your first time using ESP32.
  • Wire the components as shown in the diagram.
  • Connect the ESP32 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP32 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the code and open it with the Arduino IDE.
  • Click the Upload button in the IDE to send the code to the ESP32.
  • Listen for the click sound of the relays.
  • Check the Serial Monitor to observe the result.
COM6
Send
Turn on all Turn off all Turn on all Turn off all Turn on all Turn off all Turn on all Turn off all
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