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.
This tutorial will be requiring a few common parts:
We manufacture a specific Molex Cable for this board, but you can also use standard jumper wires as well. We recommend using the Molex cable as it is keyed to prevent reverse insertion and has a locking ramp to prevent accidental removal. Â
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!Â
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”.
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. If you are using our 6 Pin Molex Cable with this board, this is already taken care of! 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:
}
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!Â
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!
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);
}
12 thoughts on “Getting Started With The 4 Channel Relay Breakout”
James Boag
By adding a very cheap Ethernet shield you can have the basis of your Internet of things, I am currently controlling my 12 volt garden lights and pond lights from a very similar set up. The Arduino runs the web server and these can be controlled from my phone or anything with a web browser.
Chris @ BCR
Very true, with a WiFi controlled relay a lot can be done. We have been working on a couple small, low cost, IoT boards for that specific purpose that should be rolling out very soon!
Albert
Hi James, im currently working on the same project using arduino, bluetooth module and relay board to turn off and lights using smart phone, i need some assistance in this, i will appreciate.
Patrice Boudreault
Here’s the holy grail of IoT:
1- Adafruit ESP8266 Huzzah (13$ wi-fi arduino microcontroller!)
2- RaspberryPi running:
– Mosquitto MQTT server, to handle messages between unlimited IoT devices in your house
– Apache web server, to provide user interface for all devices, with JavaScript websockets MQTT library.
Seriously, this setup makes adding new devices a breeze.
Marvin Pegg
when will you have part II, I want to program a controller for greenhouse temp. including exhaust fans and zone valves etc.
thanks
Marvin
Satish yadav
Arduino Bluetooth for chann
el code
Andries Roode
Hi Marvin,
I developed a Arduino system with a 128×64 screen that reads temp and displays on screen. It regulates 4 different fans, heaters and cooling pads according to the temp. It also has a clock module that gives me time and date to regulate valves and pumps for the plants in the greenhouse.
You can contact me on andries.roode@gmail.com
Mick
I’m using this relay board from switches on a 5v rail. The board is fed from 5v. Is this ok or do I need to add pull down resistors as the switches are push to make and floating when not closed? Thanks
William @ BC Robotics
Hi Mick,
No need to add anything
MJ
By connecting the same as above nothing happens. Why is that happening?
Chris @ BCR
Hi MJ,
If it is connected exactly as above and nothing is happening, feel free to get in touch with our support team – they can help diagnose if it may be a warranty issue. Just have your order number handy!
Cheers,
Chris
D Butler
Great tutorial. I want write a simple program that the next relay doesn’t trigger until a limit switch is triggered and loops through 4 sequences of doing this. Output would run a different motor until next limit switch is hit. Any suggestions of other tutorials that may help with this?