HomeTutorials › Getting Started With The 4 Channel Relay Breakout

Getting Started With The 4 Channel Relay Breakout

Share With:
google

The 4 Channel Relay Breakout is an easy way to use your Arduino, Raspberry Pi, or other microcontroller to switch high voltages and high current loads. The board is both 3.3V and 5V logic compatible and uses 4 digital outputs to control 4 individual relays. Each relay has the common, normally open, and normally closed pin broken out to a convenient 5.0mm pitch screw terminal. If this all sounds over your head, don’t worry, we will be going into this in great detail!

A Few Considerations:
Before we jump into getting this relay breakout board hooked up there are a few points to consider.

• While this board can be used to switch mains power, it should be done so using extreme caution. This should not be done without the aid of an experienced electrician.
• This board uses through hole components that could short if the board is placed on a conductive surface. We recommend this board be mounted using the four corner mounting holes before use.

How It Works:
This breakout takes care of all the extra components required to use a relay with a microcontroller. Each relay can be individually driven on this board; when one of the input pins is set high it will trigger a transistor that will trip the corresponding relay and illuminate an indicator light. Each input can be directly connected to an Arduino (or other microcontroller / single board computer) without worry of damage.

The Parts Needed:

This tutorial will be requiring a few common parts:

We have also used one of our full sized Arduino Mounting Plates in this tutorial. The mounting plate keeps your Arduino fixed in place, allowing for much more organized prototyping!
relay-2
relay-3

Step 1 – Power To The Breakout Board

Start by orienting the Arduino and 4 Channel Breakout Board next to one another. This breakout board operates on 5VDC, since the Arduino also runs on 5VDC we can take power off of the 5VDC pin on the Arduino.

Start by finding the “5V” pin on the Arduino and running a jumper wire over to the “VCC” pin on the breakout board. The VCC is the positive (power) input for the circuit. Next, run a wire from the “GND” (ground) pin on the Arduino to the “GND” pin on the Breakout Board.

The breakout board now has power!

Step 2 – Connecting The Trigger Wires

Each relay on the breakout board has an individual trigger wire meaning they can all be individually switched on and off. When each trigger wire has between 3 and 5VDC applied a transistor triggers the relay coil, in turn, making the relay switch. We can connect a digital pin from the Arduino to each of the trigger pins on the Arduino Uno to control the relays.

Let’s go ahead and connect Arduino to the breakout board. We will start with digital pin 2 as pins 0 and 1 are reserved for the Arduino’s Serial Port. Arduino Digital Pin 2 can be connected to the pin marked “1” on the relay breakout board, Arduino pin 3 to “2”, Arduino pin 4, to “3”, and Arduino pin 5 to “4”.

relay-4
relay-5

Step 3 – Double Check And Plug It In!

Before we give the Arduino power it is always a good idea to go over all of the connections to make sure there are no wires in the wrong spot – sometimes that can make for a very expensive mistake!

One way to avoid this problem is good wire color discipline. In other words, decide on a purpose for each color of wire and stick to them! In this example all 5V power are red wires, all grounds are black wires, and other colors are signal wires. This way, if you ever see a red wire going to a black wire you will know right away that something isn’t quite right!


void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}

Step 4 – Starting The Code

Now that we have finished with the hookup we need to start writing some code. We will be using the Arduino IDE, this is available from https://www.arduino.cc/en/Main/Software

We will start with the “BareMinimum” sketch found by clicking “File” and selecting Examples / Basic / BareMinimum. This sketch is a great starting point as it includes the Setup and Loop functions – we will write the rest!

Step 5 – Understanding How To Control The Breakout Board

This breakout board is expecting to see a digital output signal on each of its 4 control pins so the code will be fairly simple to set up. When each Arduino pin is set “high” the connected relay will switch. When the Arduino pin is set “low” the relay will return to its off position.

Lets write a few lines of code and get this board up and running!

Step 6 – Writing The Code

We are starting with the BareMinimum Sketch found in the IDE, it should look something like this:

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:  
}

So first we will need some variables for the Arduino pins we plan to use:

int relayPin1 = 2;  //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3;  //This is the Arduino Pin that will control Relay #2
int relayPin3 = 4;  //This is the Arduino Pin that will control Relay #3
int relayPin4 = 5;  //This is the Arduino Pin that will control Relay #4

void setup() {
  // put your setup code here, to run once:
}
void loop() {
  // put your main code here, to run repeatedly:  
}

Basically we are going to reduce the opportunity for confusion later down the road by assigning names to each of the Arduino pins. Now, instead of trying to remember that Arduino Pin 2 is Relay number 1, we will simply just refer to the variable “relayPin1” instead – much easier!

Next we will set all of the pins we are using to be digital outputs:

int relayPin1 = 2;  //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3;  //This is the Arduino Pin that will control Relay #2
int relayPin3 = 4;  //This is the Arduino Pin that will control Relay #3
int relayPin4 = 5;  //This is the Arduino Pin that will control Relay #4

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  
}

Ok, now all of the pins are set to outputs. Next we can write some code in the loop to switch a relay on and off:

int relayPin1 = 2;  //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3;  //This is the Arduino Pin that will control Relay #2
int relayPin3 = 4;  //This is the Arduino Pin that will control Relay #3
int relayPin4 = 5;  //This is the Arduino Pin that will control Relay #4

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin1, OUTPUT);     
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(relayPin1, HIGH);    //Switch Relay #1 ON
  delay(1000);                      //Wait 1 Second
  digitalWrite(relayPin1, LOW);     //Switch Relay #1 OFF
  delay(1000);                      //Wait 1 Second
}

If we were to upload this code to the Arduino it will turn Relay #1 on for 1 second and then switch it off for 1 second and continue through this loop forever.

So before we do upload the code let’s use of all 4 relays to make a lot of clicking noise:

int relayPin1 = 2;  //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3;  //This is the Arduino Pin that will control Relay #2
int relayPin3 = 4;  //This is the Arduino Pin that will control Relay #3
int relayPin4 = 5;  //This is the Arduino Pin that will control Relay #4

void setup() {
  // put your setup code here, to run once:
  pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly: 
  digitalWrite(relayPin1, HIGH);
  delay(1000);
  digitalWrite(relayPin1, LOW);
  digitalWrite(relayPin2, HIGH);
  delay(1000);
  digitalWrite(relayPin2, LOW);
  digitalWrite(relayPin3, HIGH);
  delay(1000);
  digitalWrite(relayPin3, LOW);
  digitalWrite(relayPin4, HIGH);
  delay(1000);
  digitalWrite(relayPin4, LOW);
}

Step 7 – Upload The Code And Test

Now that all of the code has been written it can be uploaded to your Arduino! Click “Upload” button in the top left corner of the Arduino IDE and it should upload without any issues. After a few seconds the relays should start turning on and off in order one after another at a 1 second interval.

Stay tuned for Part II of this tutorial – we will look at using digital and analog inputs to trigger relays!

finalcode1

Have A Question?
If you have any questions, or need further clarification please post in the comments section below; this way future users of this tutorial can see the questions and answers!

Parts Used In This Tutorial:

5 comments

  1. James Boag
    • Albert
  2. Patrice Boudreault
  3. Marvin Pegg

Leave a Reply