8 September 2014

Relay Module

WARNING: Mishandling or incorrect or improper use of relays could result in

  • serious personal injury or DEATH
  • possible physical damage of the product
  • faulty operation
  • or create serious/dangerous hazards.

Please make sure that you read and understand how your relay/relay module board works, the voltage and current it is rated for, and the risks involved in your project BEFORE you even attempt to start putting it together. Seek professional and qualified assistance BEFORE you undertake ANY high power projects.

If you choose to follow the instructions in this tutorial, you do so at your own risk. I am not an electrician, and am not a qualified electrical engineer - so please do your research and seek advice BEFORE undertaking a project using a relay. Please check your connections and test them BEFORE turning the power on.

I accept no responsibility for your project, or the risk/damage/fire/shock/injury/death/loss that it causes. You take full responsibility for your actions/project/creation, and do so at YOUR OWN RISK !!!

Please note: It is illegal in some countries to wire up a high power project without an electrician. Please check your country's rules/laws/regulations before you undertake your project. If you have any doubts - don't do it.


 

What is a relay

A Relay is an electrically operated switch. Many relays use an electromagnet to mechanically operate the switch and provide electrical isolation between two circuits. In this project there is no real need to isolate one circuit from the other, but we will use an Arduino UNO to control the relay. We will develop a simple circuit to demonstrate and distinguish between the NO (Normally open) and NC (Normally closed) terminals of the relay. We will then use the information gained in this tutorial to make a much more exciting circuit. But we have to start somewhere. So let's get on with it.

Parts Required:

Fritzing Sketch


 


 
 

Table of Connections



 
 

Arduino Sketch


 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
    
    
/* ===============================================================
      Project: 4 Channel 5V Relay Module
       Author: Scott C
      Created: 7th Sept 2014
  Arduino IDE: 1.0.5
      Website: http://arduinobasics.blogspot.com.au
  Description: Explore the difference between NC and NO terminals.
================================================================== */

 /* 
  Connect 5V on Arduino to VCC on Relay Module
  Connect GND on Arduino to GND on Relay Module 
  Connect GND on Arduino to the Common Terminal (middle terminal) on Relay Module. */
 
 #define CH1 8   // Connect Digital Pin 8 on Arduino to CH1 on Relay Module
 #define CH3 7   // Connect Digital Pin 7 on Arduino to CH3 on Relay Module
 #define LEDgreen 4 //Connect Digital Pin 4 on Arduino to Green LED (+ 330 ohm resistor) and then to "NO" terminal on relay module
 #define LEDyellow 12 //Connect Digital Pin 12 on Arduino to Yellow LED (+ 330 ohm resistor) and then to "NC" terminal on relay module
 
 void setup(){
   //Setup all the Arduino Pins
   pinMode(CH1, OUTPUT);
   pinMode(CH3, OUTPUT);
   pinMode(LEDgreen, OUTPUT);
   pinMode(LEDyellow, OUTPUT);
   
   //Provide power to both LEDs
   digitalWrite(LEDgreen, HIGH);
   digitalWrite(LEDyellow, HIGH);
   
   //Turn OFF any power to the Relay channels
   digitalWrite(CH1,LOW);
   digitalWrite(CH3,LOW);
   delay(2000); //Wait 2 seconds before starting sequence
 }
 
 void loop(){
   digitalWrite(CH1, HIGH);  //Green LED on, Yellow LED off
   delay(1000);
   digitalWrite(CH1, LOW);   //Yellow LED on, Green LED off
   delay(1000);
   digitalWrite(CH3, HIGH);  //Relay 3 switches to NO
   delay(1000);
   digitalWrite(CH3,LOW);    //Relay 3 switches to NC
   delay(1000);
 }
    
    


 

The Red light on the Relay board turns on when power is applied (via the VCC pin). When power is applied to one of the Channel pins, the respective green light goes on, plus the relevant relay will switch from NC to NO. When power is removed from the channel pin, the relay will switch back to NC from NO. In this sketch we see that power is applied to both LEDs in the setup() method. When there is no power applied to the CH1 pin, the yellow LED will be on, and the Green LED will be off. This is because there is a break in the circuit for the green LED. When power is applied to CH1, the relay switches from NC to NO, thus closing the circuit for the green LED and opening the circuit for the yellow LED. The green LED turns on, and the yellow LED turns off.

I also show what happens when you apply power to a channel (eg. CH3) when there is nothing connected to the relay terminals. The respective onboard LED illuminates. This is useful for troubleshooting the relays, and knowing what state the relay is in (NC or NO). NC stands for Normally closed (or normally connected) NO stands for Normally open (or normally disconnected)

Here is a circuit diagram for two of the relays on the relay module (CH1 and CH2).
This was taken from the iteadstudio site.

 


 
 

The Video


 



 

This tutorial will become very useful in the future. I now have an easy way of switching a circuit electronically. Yes, I could do this with a transistor, but sometimes it is nice to hear that mechanical click. I am not sure why I like relays, but I find them to be quite fun !!

If you liked this tutorial - please show your support :


 
 


 
 

54 comments:

  1. Thanks for an awesome blog! One thing that is killing me is the music in your videos. The crappiest music ever! Keep it rather silent than putting those lame tunes on.

    ReplyDelete
    Replies
    1. Haha! The music is pretty lame. Unfortunately there is not much to choose from when you are trying to keep it free :). If you know somebody that will allow me to use their music for free. Please let me know.
      While I know you have a volume option within YouTube, I thank you for your honest feedback.

      Delete
    2. Try taking music from Internet Archive where they have some music which has been put in public domain.

      Delete
    3. That is where I get all my music from. But there have been some improvements in the quality of music.

      Delete
  2. hye, i have follow your work, but i dont put any resistor to my LED..and the result is,yeah,LED is not blinking...but my CH is keep blinking and keep changing alternately just like you are...it is because of resistor?and i use Arduino Uno ethernet, so i changed a little pin in the sketch

    ReplyDelete
    Replies
    1. You need to connect a resistor in series with the LED.
      It is possible that you could have damaged your LED or Arduino Pin (by skipping the resistor).
      Try running the blink sketch, and test each pin with an LED + resistor. Once you know your Arduino and/or your LEDs are fine, then try incorporating the relay module. But you MUST use a resistor as described in the sketch above.

      Delete
    2. ok..how can i know how much the ohm of resistor that i must use?

      Delete
    3. Nurul... I cannot answer that question. The reason I cannot answer that question is because I do not know what LEDs you are using. I usually use 330 ohm resistors for the LEDs that I have, but that may or may not be suitable for your LEDs. Here is a better explanation.

      Delete
  3. Hi, your blog is very nice and useful..By the way i have some projects in mind that i want to use your relay module information. If its ok for you? im planning to expand the use of arduino by using some inputs to control the individual actions of the relay. i have this input per relay and every time there an input, delay timer starts for example 12 mins before the relay activate. Hope you have some suggestions in mind. jaomadn@gmail.com

    ReplyDelete
    Replies
    1. I have no problem with you using my code in your project.
      If you would like me to make some suggestions on your project, then it would be best to create a thread within the forum: http://arduinobasics-forum.1116184.n5.nabble.com/Help-me-with-my-project-f2.html

      Delete
    2. Thanks.

      I've already created a thread

      Delete
  4. So, I am missing something. Why do you have a connection to Channel 3 on the relay module? Relay 3 doesn't have anything on it, I only see the NC and NO on Channel 1 being used. It seems that part was un-necessary, unless it's just for future expansion or something. Not criticizing, just asking. Great project BTW

    ReplyDelete
    Replies
    1. Hi Dax, you are not really missing anything.
      I was mainly trying to emphasize and demonstrate how the relay module LEDs light up when their respective relay is activated. It was mainly for demonstration purposes.

      And you have great insight. The next project I did was this one:
      Arduino Selfie. This project actually used both channel 1 and channel 3. And yes - I know, a relay was overkill for that project... :)



      Delete
    2. After I re-read your post, I see that my question was not a very good one. You did explain it, I just missed that detail about you saying it demonstrates the little LED and how it's good for troubleshooting. Thanks for the fast reply, and keep up the good work!

      Delete
    3. No worries Dax. Thank you for the feedback... and keeping me on my toes :)

      Delete
  5. hi have just used this with ssr instead of normal relays but the relays dont seem to be switching off is their something iam missing to turn them off please help if you can thanks .

    ReplyDelete
    Replies
    1. Hi Dave,

      I think we will need to take your query to the ArduinoBasics forum.
      Follow this link to get to the forum.
      Post your question, and show your setup and code. Also provide a datasheet for your ssr (if you can find one)...or a link to where you got it maybe??

      Delete
  6. Hey, I want to ask a question about the relay module. Is an optocoupler relay module better than the on e used here?

    ReplyDelete
    Replies
    1. I guess it depends on what you plan to do with the relay module.
      There is a good discussion on this topic here.

      Delete
  7. Hello, can you help me I need full optical isolation and I connected Vcc to Gnd but it doesn't work. I bought module relay here http://pc24.de/weidmuller but even on this web site I couldn't find any tips what to do(

    ReplyDelete
    Replies
    1. Hi Scott,

      Not sure what you mean "it doesn't work". Plus I have no idea what relay module you bought, because the link you provided was too generic.
      Please post queries related to your own projects here:
      http://arduinobasics-forum.1116184.n5.nabble.com/Help-me-with-my-project-f2.html
      Please provide a bit more detail - so that I can understand what it is you have, what you are trying to do, and what you have done so far.

      Thanks.

      Delete
  8. Can 1 arduino uno cater 5 - 4 channel relay modules ?? equivalent to 20 relays???

    tnx.

    ReplyDelete
    Replies
    1. Possibly. But it is limited by the number of available pins (you could possibly use the 13 digital pins and the 5 analog pins - which only gives 18 relays) but italso depends on whether you need all relays on at the same time or not....If you don't need them all on at the same time, you can get creative with how you utilise the available pins, otherwise if you do need all relays on at the same time, then I would recommend using something like the Arduino MEGA...

      Delete
  9. hi great tutorial I see you answer your comment section so I need some help I dont know arduino I cant seem to understand the programing logic as its nothing like the relay logic I know. would it be possible for you to design a circut for me im building a pellet gun target that will actuate with a micro switch ( button) when hit will turn on relay for 5 seconds then reset and wait for next shot. if you can help great. thanks for your time

    ReplyDelete
    Replies
    1. As this is a query about your own project - I think it would be best served in the ArduinoBasics forum. Just copy and paste this into a new topic, and we can discuss further.

      Delete
  10. Hi,
    Can you also put 230V AC on the relays and control them with the 5V of the Arduino?
    When it is possible it doesn't matter which cable is put on the ground and on the NC/NO, right?

    ReplyDelete
    Replies
    1. Hi Anonymous,

      First let me say that in some countries, you need to an electrician to hook up high voltage AC circuits. Please research whether this is true for your country.

      Next - let me say that "playing" with these voltages can be fatal... so you should know what you are doing before attempting a project using high voltages.

      Lastly I would recommend that you read the datasheet for the relays on your board, and make sure they can handle the AC circuit you are trying to connect.

      One more lastly - I would test it with a low voltage circuit first. Make sure it works how you expect it to work.

      There are some relay boards that can control high powered circuits from 5V coming from the Arduino. A relay is an electrical switch, and isolates the Arduino from the high powered circuit.
      When 5V is sent to the relay, the switch activates and the current flows through the NO line through to common (or vice versa)... Whether you hook up to the NO or NC will depend on whether you want to turn the device ON or OFF by sending 5V from the Arduino.

      So just to clarify... when you send 5V from the Arduino, it essentially connects the "NO" wire to the "Common" wire. And when you stop sending 5V, this connection between NO and Common is broken.

      If you follow my tutorial, you will get a better understanding of how relays work.

      Delete
  11. do you know how to use a relay without an arduino ?

    ReplyDelete
  12. Hi,please make me understand how to connect a ac submersible pump to a 4 channel relay
    to control water level of a tank using arduino?

    ReplyDelete
    Replies
    1. Hi Unknown,

      Well - start with this tutorial, and perhaps my water sensor tutorial may also help. Just put these together.
      http://arduinobasics.blogspot.com.au/2014/08/grove-water-sensor.html

      It depends on how you plan to monitor the water level. I know some people use these water sensors, but others use the ultrasonic sensors. In which case this tutorial may be useful: http://arduinobasics.blogspot.com.au/2012/11/arduinobasics-hc-sr04-ultrasonic-sensor.html

      When you detect water above/below a certain point, you can switch on/off the relay.

      Make sure your relay can cope with the voltage/current you plan to use. Look up the datasheet for your relay to get better insight.
      Playing with AC current can kill you (or others). Perhaps seek advice from an electrician before you move forward with this project.

      Delete
  13. found page helpful. was messing with arduino wires damn stiff things. finally got relay to start clicking like a trooper. https://youtu.be/BcSrf1VW3-w
    now need more wires , trying to play ipad game using arduino , hmmm

    ReplyDelete
    Replies
    1. Hi Terry - I am glad this tutorial helped you. I am not sure how your relay board is all mapped out. But I would have put the "blue ground wire" from the Arduino to the GND pin on your relay board (next to the VCC pin or yellow wire in your video).

      Delete
  14. Thanks for this tutorial! I've started experimenting with Arduino and I've run through all the projects that were in my "Learn Arduino Basics" pack. So now I'm adding small items a bit at a time and trying them out. Last week was the Hall Effect sensor and this week the relay. I'm glad your tutorial didn't require kit I don't have.

    ReplyDelete
    Replies
    1. Excellent Amy - keep it up... you'll be an expert in no time.

      Delete
  15. Does this work without the relay module?

    ReplyDelete
    Replies
    1. Sorry - that question is too vague... does what work ?? What are you trying to do ?

      Delete
  16. Hello sir I am working with AC 220V could it controlled by arduino digital output?

    ReplyDelete
    Replies
    1. It is always worth asking the seller of the product or looking at the datasheet for the specific product you choose to buy. If you cannot find this information easily, then I would recommend that you look for a site or product where the information IS available, and meets your needs... Any projects you choose to undertake that involves high voltage should be installed by a licensed electrician, and is at your own risk. Incorrect connections could cause serious harm or death. If you do find the correct relay shield, yes technically, it can be controlled by an Arduino Digital output.

      Delete
  17. How did you get the relay into fritzing? Is that just an image or did you find a 4 channel relay fritzing component somewhere that someone created? I have been looking for one or an 8 channel component.

    ReplyDelete
    Replies
    1. Just an image I took with my camera.

      Delete
  18. Hi,
    Great post. Unfortunately, I have a different kind of a relay board. It has 2 pins for input on one side and 3 on the other. Can you please help me out with its connections?
    Here's the link for its image if you want:
    http://tutorsvilla.com/arduino/relay_board.jpg
    Thanks

    ReplyDelete
    Replies
    1. I would say that the side with 2 connections is the side that controls the relay, and the side with 3 pins is the relay side (similar to my relay board).

      First try to light up the LEDs on the board. Not sure which is VCC and which is ground, but I am guessing that you cannot really get it wrong... the LEDs and diodes look like they will only allow one particular configuration.
      As for the relay side - you can test the connections using a multimeter. This will help test the normally open and normally closed sides.

      Delete
  19. In the NC position connects and the yellow LED glows(LOW signal) but when the NO position is supposed to be connected when I provide the HIGH signal the green LED does not glow.Why is it so?

    ReplyDelete
    Replies
    1. Check the orientation of the green LED. Does the channel LED illuminate when you provide a HIGH signal? Are you using this relay board ? If not, does it support a 5V signal ?
      Do you hear the relay click when you provide a high signal.
      Also make sure that you are providing 5V to VCC and have not accidentally provided 3.3V

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Vyushti - glad you manage to work it out. Shame that the other board did not work for you.

      Delete
  20. hi scott,
    am working on a project where am thinking of using a 1 channel relay between 2 parts of a circuit such that when it is open the output is taken only from the first part of circuit, if closed the output is taken from the second part of the circuit.i need to control where the output is taken using arduino. how should i go about it. thanks

    ReplyDelete
    Replies
    1. Hi Anonymous,

      I have a couple of ideas - but questions about your own project are best served in the ArduinoBasics Forum (link at the top of this page) - where I will respond to your query. Thank you
      Scott

      Delete
  21. hi..
    i had made connection from relay to motor, as NO is connected to vcc 5v, C is connected to motor(water pump) of one side and motor(water pump) of other side is connected to gnd, where no connection for NC,well the problem is when ardiuno switch on the relay for motor(water pump) operation, water pump turn on few sec and gets to off. but i need continuous to turn motor until the power supply is off.

    ReplyDelete
    Replies
    1. Hi Arjun,

      I am a bit confused, why are you connecting VCC to NO?
      Are you trying to power the motor using the Arduino 5V pin ?

      Don't connect the motor to the common line, connect it to the NO or the NC side of the relay (depending on whether you want it to be "ON" until you switch it off, or "OFF" until you switch it on. You should be powering your motor using an external power supply, unless it is a small hobby servo motor, in which case you can exclude the use a relay altogether.

      Delete
    2. hye..how can i turn off the arduino by coding or relay?

      Delete
  22. Thanks For this!.
    I like the music :-)

    ReplyDelete
  23. About two years ago I found a schematic that showed how to wire an Arduino 4 channel relay with another 4 channel relay shield to allow the Arduino to control all 8 relays. Does anyone know where I can find it. I have many Seeed 4 relay shields (v1.2) and I need to make a few 8 relay projects. I cannot find that info anywhere now. Any help would be greatly appreciated.

    ReplyDelete

Feel free to leave a comment about this tutorial below.
Any questions about your particular project should be asked in the ArduinoBasics forum.

Comments are moderated due to large amount of spam.