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:
Understanding the pinout configuration of a 4-channel relay module
Establishing the connections between an ESP32 and the 4-channel relay module
Programming the ESP32 to effectively control the 4-channel relay module
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) ESP32 Screw Terminal Adapter |
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 these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.
Introduction to 4-Channel Relay Module
The 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.
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.
We can decrease the amount of power adapters by utilizing one 5V power source for both the ESP32 and the 4-channel relay module.
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 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);
Manipulate the relay's state with the digitalWrite() function.
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 boardvoid 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 forevervoid 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 allTurn off allTurn on allTurn off allTurn on allTurn off allTurn on allTurn off all
AutoscrollShow 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
Learn More
ESP32 - Relay
ESP32 - 2-Channel Relay Module
ESP32 - Controls Pump
ESP32 - Controls Fan
ESP32 - Controls Heating Element
ESP32 - Button - Relay
ESP32 - Button Toggle Relay
ESP32 - Potentiometer Triggers Relay
ESP32 - Ultrasonic Sensor - Relay
ESP32 - Light Sensor Triggers Relay
ESP32 - Motion Sensor - Relay
ESP32 - DHT11 - Relay
ESP32 - DHT22 - Relay
ESP32 - Cooling System using DHT Sensor
ESP32 - Cooling System using DS18B20 Temperature Sensor
ESP32 - Heating System
ESP32 - Door Sensor - Relay
ESP32 - Door Sensor Toggle Relay
ESP32 - RFID/NFC - Relay
ESP32 - RFID - Servo Motor
ESP32 - Touch Sensor - Relay
ESP32 - Touch Sensor Toggle Relay
ESP32 - Rain Sensor - Relay
ESP32 - Gas Sensor Relay
ESP32 - Sound Sensor - Relay
ESP32 - Controls Relay via Web
※ OUR MESSAGES
As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
If this tutorial is useful for you, please support us to make more tutorials.
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!
PREVIOUS
NEXT
DISCLOSURE
ESP32IO.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com, Amazon.it, Amazon.fr, Amazon.co.uk, Amazon.ca, Amazon.de, Amazon.es, Amazon.nl, Amazon.pl and Amazon.se
Copyright © 2018 - 2024 ESP32IO.com. All rights reserved.
Terms and Conditions | Privacy Policy
Email: ArduinoGetStarted@gmail.com