Arduino code for a 4-to-16 channel 5V relay module

Arduino code for a 4-to-16 channel 5V relay module

In this tutorial, we will learn how to control a 4-channel relay module using an Arduino. This will allow you to manage AC loads such as lights, fans, or heaters safely and effectively. The code provided can be adapted for modules with more channels, making it versatile for various applications.

Before we dive into the details, it's essential to understand the components involved in this project. The relay module serves as a switch that can control high voltage devices while being controlled by low voltage signals from the Arduino. Each relay can either be normally open or normally closed, allowing for flexibility in how the connected devices operate. Please refer to the video for visual guidance (in video at 02:15).

Hardware Explained

The main components of this project include the Arduino board and the relay module. The Arduino acts as the controller, sending signals to the relay module based on the code we write. The relay module contains multiple relays that can switch AC loads on and off. Each relay has three terminals: normally closed (NC), normally open (NO), and a common terminal (COM).

When the relay is activated, the common terminal connects to the normally open terminal, allowing current to flow through the load. This is a low-trigger relay, meaning it activates when it receives a low voltage signal (0V) and deactivates with a high voltage signal (5V). Understanding this mechanism is crucial for safely controlling AC devices.

Datasheet Details

ManufacturerGeneric
Part number4-channel relay module
Logic/IO voltage5 V
Supply voltage5 V
Output current (per channel)10 A max
Peak current (per channel)15 A
PWM frequency guidanceN/A
Input logic thresholdsLow: 0 V; High: 5 V
Voltage drop / RDS(on) / saturationN/A
Thermal limits85 °C
PackageStandard relay module
Notes / variantsAvailable in 2, 4, 8, and 16 channel variants

  • Ensure the relay's current rating is not exceeded to prevent damage.
  • Use separate power supplies for high-current applications to isolate the Arduino.
  • Maintain proper heat dissipation for the relays during operation.
  • Check the wiring carefully to avoid short circuits.
  • Use opto-isolators if necessary for added protection.

Wiring Instructions

Arduino wirign for 4 channel relay to control 4 AC load
Arduino wirign for 4 channel relay to control 4 AC load

To wire the relay module to your Arduino, start by connecting the ground pin of the relay module to the ground pin on the Arduino. Next, connect the VCC pin on the relay module to the 5V pin on the Arduino. For the control pins, connect the IN1, IN2, IN3, and IN4 pins of the relay module to digital pins 2, 3, 4, and 5 on the Arduino, respectively. This setup allows the Arduino to control the state of each relay.

Make sure to position the relay module on an insulated surface, as it deals with AC loads. If you're using an external power supply for the relays, connect its ground to the Arduino ground to ensure a common reference. This is crucial for proper operation and safety.

Code Examples & Walkthrough

The following code snippet initializes the number of relays and sets the control pins:

int ch = 4; // number of relays you have
int relay[]={2,3,4,5}; // Arduino pin numbers for relays

This defines the number of relays and their corresponding Arduino pin connections. The array relay holds the pin numbers used to control each relay.

Wirig relay module to AC load

Next, we set up the pins in the setup() function:

void setup() {
    Serial.begin(9600); // prepare Serial monitor
    for(i=0; i < ch; i++) {
        pinMode(relay[i], OUTPUT); // set i(th) pin as output
        digitalWrite(relay[i], HIGH); // Turn the relay OFF  
    }
    Serial.println("Robojax 4 Relay Test");
}

This code initializes the serial monitor and sets each relay pin as an output, turning them off by default. The serial print statement confirms that the setup is complete.

The main loop of the program controls the relays:

void loop() {
    for(int i=0; i < ch; i++) {
        Serial.print("Relay "); Serial.print(i+1); Serial.println(" ON");
        digitalWrite(relay[i], LOW); // Turn the relay ON    
        delay(wait);  
    }
    for(int i=0; i < ch; i++) {
        Serial.print("Relay "); Serial.print(i+1); Serial.println(" OFF");
        digitalWrite(relay[i], HIGH); // Turn the relay OFF    
        delay(wait);  
    }
    Serial.println("====== loop done ==");
}

This loop first turns each relay on one by one, waits for a specified time, and then turns them off in the same sequence. The serial output helps monitor the relay status in real-time.

Demonstration / What to Expect

When you run the program, you should see the relays activate in sequence, turning on and off every two seconds. If everything is wired correctly, the AC loads connected to the relays will respond accordingly. Be cautious of the current draw from the Arduino; if too many relays are activated simultaneously, it may exceed the board's capacity (in video at 13:45).

Video Timestamps

  • 00:00 Introduction
  • 02:15 Hardware Explanation
  • 05:30 Wiring Instructions
  • 08:45 Code Walkthrough
  • 13:45 Demonstration

Images

Wirig relay module to AC load
Wirig relay module to AC load
Arduino wirign for 4 channel relay to control 4 AC load
Arduino wirign for 4 channel relay to control 4 AC load
88-This is code for a 4- to 16-channel 5V relay module for Arduino.
Language: C++
89-If you want to replace the loop inside setup(), then use this as shown in the video.
Language: C++
Copied!

Things you might need

Resources & references

No resources yet.

Files📁

Fritzing File