Arduino Code and Video for a 4-Channel Relay Shield

Arduino Code and Video for a 4-Channel Relay Shield

In this tutorial, we will learn how to use an Arduino to control a 4-channel relay shield. This shield allows you to turn on and off various AC or DC loads, such as lights or motors. By the end of this project, you will understand how to wire the relay shield and how to write code to control the relays using an Arduino.

Arduino 4 channel relay shild

To begin, we will need to set up the hardware components correctly. The relay shield connects to the Arduino using digital pins, which will be used to send signals to turn the relays on and off. Each relay can control a separate load, allowing for versatile applications. The associated video provides a visual guide to the entire setup and code implementation (in video at 00:00).

Hardware Explained

The primary component of this project is the 4-channel relay shield. Each relay on the shield has three terminals: normally open (NO), normally closed (NC), and common (COM). When the relay is activated, it connects the common terminal to the normally open terminal, allowing current to flow through the connected load.

Each relay requires a digital output from the Arduino, specifically using pins 4 to 7. These pins are labeled on the shield, making it easy to connect your devices. Additionally, there are indicator LEDs on the shield that light up when the corresponding relay is activated, providing a visual status of the relays.

Wiring Instructions

To wire the 4-channel relay shield to your Arduino, start by connecting the shield to the Arduino board. Ensure that the pins on the shield align with the Arduino headers. The relay shield uses digital pins 4, 5, 6, and 7 for relay control. Connect the following:

Arduino 4 channel relay shild
  • Pin 7 to Relay 1
  • Pin 6 to Relay 2
  • Pin 5 to Relay 3
  • Pin 4 to Relay 4

Next, connect your load to the relay terminals. For each relay, connect the common terminal to your power source, and then connect the load to either the normally open (NO) or normally closed (NC) terminal based on your requirements. Make sure to observe the correct polarity and ratings for your devices. The video also covers alternative wiring setups (in video at 02:30).

Code Examples & Walkthrough

The following code snippet demonstrates how to define the relay pins and set them as outputs in the Arduino setup.

#define relay1 7
#define relay2 6
#define relay3 5
#define relay4 4

void setup() {
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
}

In this excerpt, we define each relay's corresponding pin number using preprocessor directives. The pinMode function is used to configure these pins as outputs, allowing the Arduino to control them.

Next, we can turn on Relay 3 and keep it activated for three seconds, as shown in the following code block.

void loop() {
  digitalWrite(relay3, HIGH); // turn relay 3 ON
  Serial.println("relay 3 ON");
  delay(3000); // keep relay 3 ON for 3 seconds
  digitalWrite(relay3, LOW); // turn relay 3 OFF
  Serial.println("relay 3 OFF");
  delay(3000); // keep relay 3 OFF for 3 seconds
}

This loop continuously toggles Relay 3 on and off every three seconds. The digitalWrite function is used to set the relay state, and the Serial.println function outputs the current state to the Serial Monitor.

Demonstration / What to Expect

Upon completion of the setup and code upload, you should observe the relay activating as expected. Relay 3 will turn on for three seconds and then turn off, as indicated by the LED on the relay shield. To control other relays, you can modify the code by changing the relay identifiers (in video at 05:00).

Be mindful of potential issues like reversed polarity or incorrect wiring, which can lead to malfunctioning of the relays or even damage to your components.

Video Timestamps

  • 00:00 - Introduction
  • 02:30 - Wiring Setup
  • 05:00 - Code Explanation

Images

Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild
Arduino 4 channel relay shild-bottom
Arduino 4 channel relay shild-bottom
Arduino 4 channel relay shild
Arduino 4 channel relay shild
49-This is the Arduino code and video for a 4-channel relay shield.
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

No files available.