How to Set Up a 5V Relay on the Arduino (2024)

Posted by Scott Campbell | Arduino | 73

How to Set Up a 5V Relay on the Arduino (1)

One of the most useful things you can do with anArduinois control higher voltage (120-240V) devices like fans, lights, heaters, and other household appliances. Since the Arduino operates at 5V it can’t control these higher voltage devices directly, but you can use a 5V relay to switch the 120-240V current and use the Arduino to control the relay.

How to Set Up a 5V Relay on the Arduino (2)

The Arduino can be programmedto turn onthe relay when acertain event occurs, for example when the temperature ofa thermistor getshigher than 30°C. Or when the resistance of a photoresistor drops below 400 Ohms. Almost any sensor can be used to trigger the relay to turn on or off. The trigger doesn’t even need to be from a sensor. It can occur at set time intervals, it can be triggered from the press of a button, or even when you get an email.

I’ll be using theSRD-05VDC-SL-C 5V relayin this tutorial because it’s very popular among Arduino and DIY electronics hobbyists. Let’s start with seeing how the 5V relay works, then I’ll show you how to set it up on the Arduino and give you some code to get it working.

BONUS: I made a quick start guide for this tutorial that you can download and go back to later if you can’t set this up right now. It covers all of the steps, diagrams, and code you need to get started.

Here’s the datasheet:

SRD-05VDC-SL-C Datasheet

How the 5V Relay Works

TheSRD-05VDC-SL-C relay hasthree high voltage terminals (NC, C, and NO) which connect to the device you want to control. The other side has three low voltage pins (Ground, Vcc, and Signal) which connect to the Arduino.

  • NC: Normally closed 120-240V terminal
  • NO: Normally open 120-240V terminal
  • C: Common terminal
  • Ground: Connects to the ground pin on the Arduino
  • 5V Vcc: Connects the Arduino’s 5V pin
  • Signal: Carries the trigger signal from the Arduino that activates the relay

Inside the relay is a 120-240V switch that’s connected to an electromagnet. When the relay receives a HIGH signal at thesignal pin, the electromagnet becomes charged and moves the contacts of the switch open or closed.

Normally Open vs. Normally Closed

The relay has twodifferent types of electrical contacts inside – normally open (NO) and normally closed (NC). The one you use will depend on whetheryou want the 5V signal to turn the switch on or turn the switch off.The 120-240V supply currententers the relay at the common (C) terminal in both configurations. To use the normally open contacts, use the NO terminal. To use the normally closed contacts, use the NC terminal.

Normally Open

In the normally open configuration, when the relay receives a HIGH signal the 120-240V switchcloses andallows current to flow from the Cterminal to the NO terminal. A LOW signal deactivates the relay and stops the current. So if youwant the HIGH signal to turn ONthe relay,usethe normally open terminal:

Normally Closed

In the normally closed configuration, a HIGH signal opens the switch and interrupts the 120-240V current. A LOW signal closes the switch and allows current to flow from the C terminal to the NC terminal. Therefore, if you want the HIGH signal to turn OFFthe 120-240V current, use the normally closed terminal:

A Temperature ControlledRelay Circuit

To show you how to wire the relay, let’s build a temperature controlled relay circuitthat will turn off a light bulb when the temperature of a thermistor reaches 150°F.Thermistorsare really useful with 5V relays. You can use them to turn off a large motor if gets too hot or turn on a heater if the temperature gets too cold.

WARNING – THIS PROJECT INVOLVES HIGH VOLTAGES THAT CAN CAUSE SERIOUS INJURY OR DEATH. PLEASE TAKE ALL NECESSARY PRECAUTIONS, AND TURN OFF ALL POWER TO A CIRCUIT BEFORE WORKING ON IT.

The setup is fairly simple, just make sure that the high voltage connections to the relay are secure:

Identify the hot power wire (red wire in the diagram above) in the cordleading to the light bulb and make a cut. Connect the side leading to the light bulb to the NO terminal of the relay, and the side leading to the plug to the C terminal. This way the relayis on the hot side, and current is switched before it reaches the light bulb. It’s dangerous to put the relay on the neutral wire, since if the device fails current can still fault to ground when the relay is off.

The thermistor part of the circuit is set up as a voltage divider. The value of the resistor should be the same order of magnitude as the thermistor. For example, I’m using a 10KΩ thermistor, so the resistor should be10KΩ as well. If you use a100KΩ thermistor, use a100KΩ resistor.

If you do use a 100K Ω thermistor, you’ll need to change line 7 in the code belowto Temp = log(100000.0*((1024.0/RawADC-1)));.See our article on Making an Arduino Temperature Sensor for more information.

The Code

After everything is connected, upload this code to the Arduino:

#include <math.h>int pinOut = 10;double Thermistor(int RawADC) { double Temp; Temp = log(10000.0*((1024.0/RawADC-1))); Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp ))* Temp ); Temp = Temp - 273.15; Temp = (Temp * 9.0)/ 5.0 + 32.0; return Temp;}void setup() { Serial.begin(9600); pinMode(10, OUTPUT);}void loop() { int val; double temp; val=analogRead(0); temp=Thermistor(val); Serial.print("Temperature = "); Serial.print(temp); Serial.println(" F"); if (temp >= 150){ digitalWrite(pinOut, LOW); } else { digitalWrite(pinOut, HIGH); } delay(500); }

In this example, the relay will stay activated and let current flow through the light bulb until the temperature ofthe thermistor reaches 150°F. At 150°F the relayshuts off and the current stops. You can change the temperature inline 27 where it says if (temp >= 150){.

Here’s a video you can watch of me wiring up the relay to see it in action:

Anotherreally useful projectis a relay controlledpower outlet box. This will let youplug any applianceinto the outlet andcontrol it with your Arduino without cutting into any power cords. You can also control severaldevices at the same time. See our tutorial “Turn Any Appliance into a Smart Device with an Arduino Controlled Power Outlet” to see how we built it.

Hope this is useful for you! Leave a comment if you have any questions. If you want us to let you know when we publish more tutorials, be sure to subscribe and share it if you know someone that would find it helpful. Thanks!


Related Posts

How to Use Active and Passive Buzzers on the Arduino

September 24, 2021

How to Write Data to Files on an SD Card With the Arduino

May 22, 2020

How to Show Arduino Sensor Data on a Web Page

May 11, 2020

How to Power an Arduino With a Battery

June 13, 2020

73 Comments

  1. Tyaon November 30, 2015 at 5:28 pm

    Sir, How can i display the temperature on LCD and serial monitor at the same time?

    Reply

    • Circuit Basicson August 7, 2016 at 2:32 am

      Check out our article on LCD displays for the Arduino, it should explain what you need to do that:

      https://www.circuitbasics.com/how-to-set-up-an-lcd-display-on-an-arduino/

      Reply

  2. gisdudeon January 14, 2016 at 2:35 pm

    Where did you purchase this relay? I can’t seem to find it. I’ve found similar ones, but not this one.

    Reply

  3. Martin Houseon January 25, 2016 at 12:37 am

    that relay really should be in a box. Mains nips are painful & dangerous.

    Reply

    • Circuit Basicson August 1, 2016 at 2:28 pm

      That’s very true! You might want to check out this other article I wrote about how to make a relay controlled power outlet box: https://www.circuitbasics.com/build-an-arduino-controlled-power-outlet/

      Reply

      • Marioon March 29, 2017 at 1:28 am

        Hi there awesome video!! I was wondering how i can do this project with a light fixture that just has a hot wire and a neutral wire?

        Reply

        • Alexon October 8, 2017 at 10:21 pm

          Mario:
          If you see the diagram, the black wire between the plug and the light fixture is the neutral wire.
          So in this case, like in yours, the light fixture just has a hot wire and a neutral wire.
          So this project is EXACTLY what you are looking for.
          :-)

          Reply

  4. Williamon January 30, 2016 at 12:31 am

    The relay is UR (UL Recognized) but I would love to see the bottom of the PCB to verify proper creepage distances.

    Reply

  5. EVO2GOon February 15, 2016 at 3:37 am

    @iotcentrum

    Reply

    • Circuit Basicson August 1, 2016 at 2:33 pm

      The black thing that looks like a ceramic disc capacitor is actually the thermistor. This is a voltage divider circuit, so the value of the resistor should be of the same magnitude as the resistance of your thermistor. For example, if you have a 100K Ohm thermistor, the resistor should be 100K Ohms also. Thanks for bringing that up, I will update the post…

      Reply

  6. JerryRon April 2, 2016 at 7:35 pm

    Very nice, clear explanation.

    Thank you.

    Reply

  7. JCon April 15, 2016 at 3:10 pm

    How would I attach more than 1 relay (say 3) to control several loads individually?

    Reply

  8. William de Visseron May 24, 2016 at 6:16 pm

    is this sketch also for degrees C, instead of F??

    Reply

    • Circuit Basicson August 1, 2016 at 2:55 pm

      It’s for Fahrenheit, but comment out line 10 to get Celsius…

      Reply

  9. steven c shepardon June 5, 2016 at 5:27 am

    Not today, but really soon! Thanks…

    Reply

  10. Brent Parrishon June 15, 2016 at 5:00 am

    Love to see a tutorial using a MOSFET in place of the relay for voltages higher than 5V.

    Reply

  11. Davidon July 12, 2016 at 2:15 pm

    Would it be right to assume that if this approach is used on a 240V circuit that it can only interrupt one of the two phases, and that as a result the second phase will remain “hot” (and potentially lethal) at the load, even when it’s “off”?

    If so, the modification to use the same idea with a 240V circuit would be to use a DPDT (double-pole/double-throw) relay instead: the poles being used for the two 240V hots and the throws being normally-open (NO) and normally-close (NC).

    At least that’s what I’m setting out to try.

    Thank you for the most excellent pictures.

    And your photos were very helpful too. ;^)

    Reply

  12. alirezaon July 16, 2016 at 11:18 am

    can you explain more about the thermistor and the program written for it in the sketch?

    Reply

    • Circuit Basicson August 1, 2016 at 2:53 pm

      Check out our other article on thermistors and the Arduino, I go a lot more in depth into the program and set up of the thermistor: https://www.circuitbasics.com/arduino-thermistor-temperature-sensor-tutorial/

      Reply

  13. shandon August 7, 2016 at 9:59 pm

    will this still work even when i used the thermistor which is above 100k for recording high temperatures of around 200 celcius and 300 celcius for a 3d printer head

    Reply

    • shandon August 7, 2016 at 10:01 pm

      and what is power rating of a 10k resistor

      Reply

    • Circuit Basicson March 1, 2017 at 3:06 am

      Yes it will work for any size thermistor… You just want the resistor in the voltage divider be around the same resistance as your thermistor. Then you have to change line 7 in the code to the resistance value. So if you use a 200K thermistor, line 7 would be:

      Temp = log(200000.0*((1024.0/RawADC-1)));

      Reply

  14. NormanDunbaron September 19, 2016 at 10:11 pm

    You say, just under the image, to “Connect the power wire of the light bulb cord to the NO (normally open) terminal of the relay, and connect the neutral power wire of the cord to the C (common) terminal of the relay.”

    The image show the power wire is cut and the cut ends are then connected to the relay common and NO intact. Connecting power to neutral should blow a fuse when the relay closes the contacts!

    Cheers,
    Norm.

    Reply

    • NormanDunbaron September 19, 2016 at 10:13 pm

      Sorry, can’t spell.

      Contact, not intact. I hate auto correct sometimes!

      Cheers,
      Norm.

      Reply

    • Circuit Basicson March 1, 2017 at 3:08 am

      Sorry, that was an error… Thanks for commenting about it, I just changed the post.

      Reply

    • Circuit Basicson March 1, 2017 at 3:21 am

      Nice, thanks

      Reply

  15. Igor Andradeon October 11, 2016 at 11:05 am

    And that capacitor between the vcc and gnd is for? decoupling?

    Reply

    • Circuit Basicson March 1, 2017 at 3:10 am

      I think the capacitor you’re talking about is actually a thermistor.

      Reply

  16. antzevilon October 19, 2016 at 10:27 am

    How about using dust sensor ?

    Reply

    • Circuit Basicson March 1, 2017 at 3:15 am

      I just used a thermistor because it’s a simple example, but any sensor that can be connected to the Arduino will work. It’s just a matter of writing the code that will take the input from the sensor and using it to produce a HIGH signal at one of the Arduino’s pins.

      Reply

  17. Victor Peterson October 30, 2016 at 2:48 pm

    Can I use this relay to just make a connection without transferring power, like a makeshift momentary switch?

    Reply

  18. Mad Alexon November 27, 2016 at 9:23 am

    I need to control the relay using a serial monitor commands, is that possible? And how to do it , Thanks

    Reply

  19. Ihsanon January 2, 2017 at 11:59 am

    Sir, if i’m using with DHT11and i want to use it for humidity, what should i change in the coding?

    Reply

    • Circuit Basicson March 1, 2017 at 3:20 am

      Check out the code at the bottom of this post: https://www.circuitbasics.com/build-an-arduino-controlled-power-outlet/

      I’ve built the relay into a power outlet box, but the code takes the humidity reading from a DHT11 and uses it to control the relay.

      Reply

  20. Johnon March 26, 2017 at 11:51 pm

    any pointers on controlling power to ac devices? So say varying heat output of a hairdryer for example?

    Reply

  21. Brandonon April 4, 2017 at 11:52 pm

    Hi I was just wondering if this relay would also work for outputting lower voltages, I am trying to use a push button to activate a relay from my 5v pin on the arduino uno with the output attached to a 6v power wheels motor ( the current is too high for me to want that involved with anything on my arduino). I saw the voltage rating for this is 120v, but was wondering if it will work for such a lesser voltage

    Reply

    • JernejQon July 16, 2018 at 3:29 pm

      Relay is an electromechanical switch and as such “doesn’t care” about the voltage going through – use it freely.
      With high powers /high voltages it is useful because it totally separates both circuits (safety concerns). For lower powers /lower voltages you can also use a FET (“sort of” transistor) as a switch – it is much more simple. Switching a FET / transistor with arduino “out” pins is well documented.

      *For the sake of accuracy: you can also switch bigger loads with transistors / FETs … Relay is a kind of obsolete tech really.

      Reply

      • Daniel Thackerayon July 15, 2020 at 2:42 am

        Not obsolete at all. A relay has the advantages mentioned in your post, namely a very large usable voltage range (from zero up to the maximum rated), AC or DC (either polarity), and very good physical isolation.

        The failure mode of an overstressed relay is a lot like a fuse and very unlikely to cause any other damage, unlike a solid state device which can connect the switched supply to the device. For a solid-state switch to have that level of protection an opto-isolator is required, and that creates limits on the power that can be switched.

        The main advantage of solid state switches is faster operation and cleaner switching.

        Reply

  22. Steveon April 10, 2017 at 1:02 am

    Hi
    I have a question about the Songle relays. The spec sheet says the current draw is about 85mA. Can you tell me the current draw of the input pin only. I presume most of the current comes from Vcc.

    Reply

  23. ALPLon April 26, 2017 at 6:23 pm

    Some basic remarks:

    1) Relays: in DC-control-circuits should have a flyback diode connected in parallel to the coil of the relay (eg 1N4001 – but check the relay data sheet for choosing the right diode). Since an inductor (the relay coil) cannot change it’s current instantly, the flyback diode provides a path for the current when the coil is switched off. Otherwise a large voltage spike will occur causing arcing on the switch contacts which will likely destroy the switching transistor, etc.

    2) since an Arduino-PIN sources max 25mA, the relay cannot be switched directly from the Arduino-pin. Therefore eg a transistor (BC547 or 2N2222, etc) should be used (search in google for more details and sample circuits) for switching the relay.

    3) Powering the Arduino by the means of the USB-connection (which provides appr 100mA) may be not enough to switch relays. Use a separate wall-plug with sufficient power (>500mA, better more).

    4) If the relay should switch AC-loads, the use of an opt-isolator is recommended to separate the control-circuit from the switching-circuit (relay). Also the electrical paths for the AC-lines on the relay-module should be wide enough (min 3mm), as short as possible, separated from any other path on the PCB and not too close to each other.

    Reply

  24. Vaidehi Sonion June 25, 2017 at 2:54 pm

    This is useful to me.. this content have a useful information about basics of relays.

    Reply

  25. Waleed Alrashedon November 9, 2017 at 2:58 pm

    Mohammad Abdulhay this might be useful

    Reply

  26. Abhayon December 8, 2017 at 4:04 pm

    Sir,

    I want to create continuous fast pulses sent to the relay. Please advise on the code to use, and the circuit.

    Reply

    • JernejQon July 16, 2018 at 2:40 pm

      relays are electromechanical devices and as such VERY UNSUITABLE for “continuous fast pulses”. Use “solid state relay” assembly or transistor+optocoupler instead.

      Reply

  27. Anikaon January 9, 2018 at 5:49 am

    How did you draw the relay in Fritzing software? Please help me with this.

    Reply

  28. Rahim SAon July 8, 2018 at 3:22 pm

    Hi. How cn i operate a complete home with adruino and relays… Imagine i have more than 50 Switches to operate in a complete home. Do i have to keep 50 Relays?

    Reply

  29. JernejQon July 16, 2018 at 2:37 pm

    Would be grateful for a schematic of the circuit.

    This post is inaccurate. Is misleading. Leads to overloading Arduino. Is dangerous.

    – A relay is not the same thing as the “Relay module” you are using here. I came to this page looking how to hook up the relay (just the component, so this post doesn’t help me much). No relay (as a component) has a “signal” pin on the coil side, just two pins (which then need additional circuitry for load and spikes regulation). This is a big disinformation – try to change this.
    – I have trouble understanding why did you have to complicate / and obfuscate this post with temp sensing – if the title is about using the relay (or relay module), make a decent post about it … i have spent 5 minute looking at the fritzing pic wandering what is the point of thermistor (it could be inrush current regulation, but then it wouldn’t be in circuit this way) – then I realized it is a part of some other bussiness
    – it is a “circuit basics” page. I strongly advise not to post high voltage designs to such a page – look at the comments and questions in them if you think I am overreacting. Make the relay switch a high current / low voltage load if you want to make an instructable.

    Reply

  30. Angelon July 20, 2018 at 7:02 am

    great! I’m making a circuit for an ice maker, but I have trouble finding the thermistor data sheet, and I saw that in your code you have the coefficient of the thermistor that you use, I want to know how I can get the data I have I just know it’s 10k and nothing else.

    or with the formula of your code can I apply it to this thermistor?

    I would appreciate your help,

    Reply

  31. Mikeon September 24, 2018 at 8:25 pm

    This is the first time I have messed with a relay so I guess there are conventions involved; however, the fact the “C” stands for “common” seems dangerous. In household AC the neutral wire is also sometimes referred to as “common”. But in this circuit the hot wire is connected to “common”. Is this just a convention when dealing with relay’s I am not aware of?

    Reply

  32. Ian d. von October 2, 2018 at 11:01 pm

    I want to do something similar with a DHT 11, but i have a problem with this. If the temperature reaches the vale, and the lamp turns off, the temperature will also go lower, and that is going to make the lamp go on and off in aloop. How do I introduce a range, so it gets more stable?

    Reply

  33. Brenon November 13, 2018 at 3:03 pm

    Dears, i have a doubt.
    You said that we have to identify the power wire! Suppose the three hole of my wall socket are: A – the hot power wire, T- the ground and B – the neutral wire.
    I named the three pin of the plug A1, T1 and B1 ad connected A1 to C on the relay and B1 to the lamp.
    If i insert the pin A1 to A and B1 to B in the wall socket the condition of your diagram is satisfied but i can insert the plug two wais. If i insert A1 to B and B1 to A then power wire goes direc to the lamp and the neutral to the relay.
    This, probably, is a problem. How can it be solved?
    Thanks in advance.

    Reply

  34. Vinay Kumar Jangiron January 17, 2019 at 3:48 am

    Can we control 12V DC circuit using the same 5V relay

    Reply

  35. Matimuon April 13, 2019 at 9:58 pm

    Hello sir, I want to use the 5v relay but controlling it via the computer keyboard chip. when capslock is on the is 3v across it, so i wanted to use this voltage to control my 5v relay.

    Schematic or references will be helpful….THANK YOU

    Reply

  36. JAMon August 26, 2019 at 5:42 pm

    Hi, on fritzing I can’t find the relay image like the one used by you, how did you insert it? where do you find it?

    Reply

  37. Tejaon October 10, 2019 at 1:52 pm

    am working on a system to turn my room fan on if the temp reaches above a certain point. I figured out how to turn the relay on or off depending on temp, but it tries to turn the relay state on or off on every loop cycle. Is there a way to determine if the relay is currently in NO or NC position so I can have the loop cycle only change state if it is not in the correct position?

    Reply

  38. satyaon November 9, 2019 at 12:58 pm

    I’m trying to connect ldr with automatic light intensity change and I used the online code to interface and controll but the ldr was automatically on and off without intensity change and in dark time the led goes to off state and also dark in the automatically light goes to off reduce this please provide the correct solution to solve this problem thank you

    Reply

  39. chandra sekharon November 14, 2019 at 5:10 am

    Hi its very useful to us i tried to control the light using arduino uno with two channel relay board for automatic light ON & OFF system using LDR, i change my option this is very useful application for home applications Thanks a lot…

    Reply

  40. Bobon November 26, 2019 at 12:45 am

    Hi,

    I’m watching your videos on using Arduino and 5v relays…

    I’m trying to figure out some simple code to get a temp reading from DarkSky and if the temp is below, 30, turn on the relay.
    And when it goes back above 30, to turn the relay back off.

    But, I’m a novice and can’t figure it out.

    Can you help or suggest where to go for help?

    Reply

  41. Ron Bookeron February 4, 2020 at 2:38 pm

    I appreciate you explaining to make sure to secure the voltage connections and the relay. when setting it up. My son has a project for school, and he wants to make an electric car. I will be sure to get him the relays he needs and other parts to complete his project.

    Reply

  42. mandyon February 7, 2020 at 6:27 am

    hi , is it possible to drive mosfet switch (h-bridge) using relay as an interface between arduino and h-bridge?

    Reply

  43. Andyon April 11, 2020 at 10:39 am

    Sir, how did you get this specific relay into the Fritzing software

    Reply

  44. boeufon September 28, 2020 at 1:33 pm

    What if I link my stuff on NC but it’s turns on when HIGH and turns of when LOW ???

    Reply

  45. azka dalvion April 16, 2024 at 3:15 pm

    Thank you soooo much for this!!!! We tried every single thing and nothing worked, but this worked!!! Your circuit diagram helped us a lot!!d

    Reply

Leave a reply

How to Set Up a 5V Relay on the Arduino (2024)
Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5927

Rating: 4.1 / 5 (62 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.