This post is intended for beginners to interface relay boards with Arduino. Shown below is the connection details to connect 4 Channel relay board with Arduino. The relays are 12V relays.
The details of the 4 channel relay board is given below.
• Connect VCC to 12V.
• Inputs 1,2,3 & 4 are used to control the corresponding relays.
• If input is high (5V), then connection is between C (Common) & NO (Normally Open), else connection is between C (Common) & NC (Normally Closed).
• If Input1 is high, LED1 glows and vice versa.
• This board has a ULN driver which gives better performance compared to transistor drive.
• ULN2003 has seven high current Darlington arrays each containing seven open collector Darlington pairs with common emittors.
PS : Make sure to short the ground of 12V and Arduino.
Given below is a sample code to operate the 4 channel relay board using Arduino.
Depending on the state of the pushbutton connected to pin 7 relays will be turned ON/OFF. If pin 7 is high, relays will be turned ON and vice versa.
// set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int relay1 = 2; // the number of the relay1 pin
const int relay2 = 3;
const int relay3 = 4;
const int relay4 = 5;// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the relay1,2,3,4 pins as an output:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn relays on:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}
else {
// turn relays off:
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
}
}
Likewise the details of Single channel, 2-channel and 8-Channel relay boards are given below.
Single Channel Relay board.
Given below is a sample code to operate the single channel lay board using Arduino.
// set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int relay1 = 13; // the number of the relay1 pin// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the relay pin as an output:
pinMode(relay1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn relays on:
digitalWrite(relay1, HIGH);
}
else {
// turn relays off:
digitalWrite(relay1, LOW);
}
}
Given below is a sample code to operate the 2 channel lay board using Arduino.
// set pin numbers:
const int buttonPin = 7; // the number of the pushbutton pin
const int relay1 = 12; // the number of the relay1 pin
const int relay2 = 13;// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the relay1,2 pins as an output:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn relays on:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}
else {
// turn relays off:
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}
}
Given below is a sample code to operate the 8 channel relay board using Arduino.
// set pin numbers:
const int buttonPin = 8; // the number of the pushbutton pin
const int relay1 = 0; // the number of the relay1 pin
const int relay2 = 1;
const int relay3 = 2;
const int relay4 = 3;
const int relay5 = 4;
const int relay6 = 5;
const int relay7 = 6;
const int relay8 = 7;// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the relay1,2,3,4 pins as an output:
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
pinMode(relay5, OUTPUT);
pinMode(relay6, OUTPUT);
pinMode(relay7, OUTPUT);
pinMode(relay8, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn relays on:
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
digitalWrite(relay5, HIGH);
digitalWrite(relay6, HIGH);
digitalWrite(relay7, HIGH);
digitalWrite(relay8, HIGH);
}
else {
// turn relays off:
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
digitalWrite(relay3, LOW);
digitalWrite(relay4, LOW);
digitalWrite(relay5, LOW);
digitalWrite(relay6, LOW);
digitalWrite(relay7, LOW);
digitalWrite(relay8, LOW);
}
}
• Connect VCC to 12V.
• Inputs 1 to 8 are used to control the corresponding relays.
• If input is high(5V) , then connection is between C (Common) & NO (Normally Open), else connection is between C (Common) & NC (Normally Closed).
• If Input1 is high, LED1 glows and vice versa.
• Provision is left for connecting AC supply.
• This board has a ULN driver which gives better performance compared to transistor drive.
• The ULN2803A device is a high-voltage, high-current Darlington transistor array. The device consists of eight npn Darlington pairs that feature high-voltageoutputs with common-cathode clamp diodes for switching inductive loads.
The above products can be purchased from this link.
Sample codes can be downloaded from this link
Reblogged this on Random Codes – Elementz Tech Blog and commented:
Post updated with Arduino sample codes for test the Relay boards
Reblogged this on Open Hardware Community .
Dear Sir Thank you very much for detailed description. Its too good. But my question is I want to use reed switch / magnetic switch & PIR sensor as input and audio alarm and sms/call alert as output. Someone told me that for such input purpose, I has to use relay module. Is it true? If yes then how to connect 16 relays (8 relay board X 2) as input? How to code it?
Relay modules are used for output controlling. For example if you are using your microcontroller to switch a 230 volt appliance you need relay in between along with the driver circuit to operate the relays. For 16 channel you may need to use a0 to a5 pins of arduino also along with other digital output pin. Sample program can be modified according to your need .
Thank you but now want to know how to connect 15 reed switch and 4 PIR sensors with arduino as input?
For the 4 channel 12 V relay . The relay is switching when digital o/p from arduino is high,but when the digital output is made low the relay isn’t switching back to its normal condition.Why?
NB:Though there is noticeable change in LEDs when digital outputs are changed.
Hello Ajas,
Try connecting 5 V vcc and ground from the arduino board and see if the relay is working fine. It seems that you haven’t defined the pin connected to relay as output. Please see if you have called the pinmode function to define the call as output.
I have bought 4 Channel relay board (above) but it does not seems to be working.. 😦 i have one question though .. can we use a 12v power adapter to power it… I have cut my power adapter tail and using the two wires to power my board through vcc and gnd respectively.. but its not working.. pls help… or do i need a 12v battery to power it on??? does it works only on battery??
Please use adaptor of rating 12V and 1Ampere or more. Relay will work with both power adapter or battery, provided it should be giving sufficient current to relay coils. Use a multimeter to ensure proper contacts is made with the connections.
Thank You for the reply… i have my circuit done properly with right power ratings and stuff.. but somehow it still not working.. maybe my relay board is defective.. what actually happens is that as i complete my circuit and connect the + termimal to vcc the leds slightly lit up and then again go off.. what might be the reason for that?? and when i load the program to arduino to run my board it does nothing… stares me idiotically point blank @ my face… 😦
Relay board seems to be defective. Please see if vcc and gnd pins are short using a multimeter.
Thanks for the after sales support. It was really helpful. You guys rock!
Hi Neelabh,
Glad to hear that it helps. Please see our wiki page also for more tutorials on hardware interfacing
http://elementzonline.com/wiki/Main_Page
can i use 5 volt in vcc.. is it work…??? my this 8 relay board does not working..
You should use only 12 v as vcc for the eight channel relay board. The voltage rating is given on the relay.
hi i am using a 8 ch relay which i am trying to get it working with a arduino mega.i have a 12vdc 1A adapter but the thing wont trigger the relay….i have couple of questions hope that some one can help me here.
Q1. vcc is given 12vdc but the ground is given from board or the adapter negative itself?
Q2 or are all the gnd to be grounded together I.e arduino gnd relay gnd and adapter gnd?
i hope my queries will be answered soon thanking in advance.
You should make all the grounds common. Then only the input is correctly referenced from the Arduino board. See the connection diagram for reference
Thanks for the prompt reply
I have bought 2 Channel relay board but its not working because of uncommon neutral.
Please explain me how to short neutral of 2 different source ,and will it effect the circuit?
I connected arduino with usb cable(which have its own neutral),relay powered by external 12v dc source (which have its own neutral).so if i short all neutral and connected to arduino gnd pin will it damage the circuit.
No you can connect the two grounds together safely. There will not be any damage to the circuit. The circuit shown in the post can also be used for reference.
How to connect 4 channel 12v relay using single adaptor for both relay module and Arduino.
When I contacted the same it’s can’t working…
Relay indicater LED glow during low brightness. What is the mistake in my connections please give me suggestions…
“Hi i’m trying to control the light using arduino uno with two channel relay board for automatic light intensity system using LDR and but the relay was operating reverse functioning but i don’t know how to solve the problem.
“
Hi i’m trying to control the light using arduino uno with two channel relay board for automatic light intensity system using LDR and but the relay was operating reverse functioning but i don’t know how to solve the problem.
Some relay circuits are designed to work in reverse logic. Change the code accordingly. You can turn pin HIGH to turn off relay and vice versa.
Happy hacking.