Arduino Nano ESP32 - 4-Channel Relay Module

This tutorial provides step-by-step instructions on utilizing an Arduino Nano 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 Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×4-Channel Relay Module
1×Breadboard
1×Jumper Wires
1×(Optional) 5V Power Adapter for Arduino Nano ESP32
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano 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 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 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 Arduino Nano ESP32
  • Signal pins:
    • IN1: this pin receives the control signal from an Arduino Nano ESP32 to control relay 1 on the module
    • IN2: this pin receives the control signal from an Arduino Nano ESP32 to control relay 2 on the module
    • IN3: this pin receives the control signal from an Arduino Nano ESP32 to control relay 3 on the module
    • IN4: this pin receives the control signal from an Arduino Nano 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 Arduino Nano 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 Arduino Nano 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 Arduino Nano 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 Arduino Nano ESP32 (not included in the image) can be either via USB cable or power jack.
The wiring diagram between Arduino Nano ESP32 and 4-channel relay module

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 Arduino Nano ESP32 and the 4-channel relay module.
The wiring diagram between Arduino Nano ESP32 and 4-channel relay module  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.

How To Program For 4-Channel Relay Module

  • Sets the Arduino Nano 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);

Arduino Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-4-channel-relay-module */ #define PIN_RELAY_1 D2 // The Arduino Nano ESP32 pin connected to the IN1 pin of relay module #define PIN_RELAY_2 D3 // The Arduino Nano ESP32 pin connected to the IN2 pin of relay module #define PIN_RELAY_3 D4 // The Arduino Nano ESP32 pin connected to the IN3 pin of relay module #define PIN_RELAY_4 D5 // The Arduino Nano ESP32 pin connected to the IN4 pin of relay module // The setup function runs once on reset or power-up 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 repeats indefinitely 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); }

Detailed Instructions

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

  • Check out the how to setup environment for Arduino Nano ESP32 on Arduino IDE tutorial if this is your first time using Arduino Nano ESP32.
  • Wire the components as shown in the diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct Arduino Nano 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 Arduino Nano 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

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!