Rachana Jain
Published © CC BY

How to use 5V 4-Channel Relay Module with Arduino

In this project you will learn how to control a motor and RGB LED with a HL-52S relay module using Arduino Uno.

BeginnerFull instructions provided2 hours33
How to use 5V 4-Channel Relay Module with Arduino

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
9V battery (generic)
9V battery (generic)
×1
PHPoC Bread Board
PHPoC Bread Board
×1
Multicolour LED
×1
DC motor (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Story

Read more

Code

Code for Interfacing Four-Channel Relay Module with Arduino

Arduino
#define RELAY_ON  0
#define RELAY_OFF 1
#define MOTOR_PIN 2
#define RED_PIN   3
#define BLUE_PIN  4
#define GREEN_PIN 5
void setup() {
  // set the all relay pins as output
  pinMode(MOTOR_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  digitalWrite(MOTOR_PIN, RELAY_OFF);
  digitalWrite(RED_PIN, RELAY_OFF);
  digitalWrite(BLUE_PIN, RELAY_OFF);
  digitalWrite(GREEN_PIN, RELAY_OFF);
}
void loop() {
 // Turn MOTOR_PIN ON
  digitalWrite(MOTOR_PIN, RELAY_ON);
  delay(1000);  // Delay of 1 sec
  // Turn RED_PIN ON
  digitalWrite(RED_PIN, RELAY_ON);
  delay(1000);  // Delay of 1 sec
  // Turn BLUE_PIN ON
  digitalWrite(RED_PIN, RELAY_OFF);
  digitalWrite(BLUE_PIN, RELAY_ON);
  delay(1000);  // Delay of 1 sec
  // Turn GREEN_PIN ON
  digitalWrite(BLUE_PIN, RELAY_OFF);
  digitalWrite(GREEN_PIN, RELAY_ON);
  delay(1000);  // Delay of 1 sec
  delay(1000);  // Delay of 1 sec
  // Turn Off All Relays
  digitalWrite(MOTOR_PIN, RELAY_OFF);
  digitalWrite(RED_PIN, RELAY_OFF);
  digitalWrite(BLUE_PIN, RELAY_OFF);
  digitalWrite(GREEN_PIN, RELAY_OFF);
  delay(1000);  // Delay of 1 sec
  delay(1000);  // Delay of 1 sec
}

Credits

Rachana Jain
13 projects • 8 followers
I'm an avid Arduino and electronics enthusiast with a passion for tinkering, experimenting, and bringing innovative ideas to life.

Comments