Connecting a Relay to Arduino

Arduino Technology
Connecting a Relay to Arduino

arduinorelay.jpg
From the MAKE Flickr pool

After fielding a few questions about connecting relays to an Arduino, Anachrocomputer drew up this basic schematic for proper wiring.

Why use a relay with an Arduino board?
Individual applications will vary, but in short – a relay allows our relatively low voltage Arduino to easily control higher power circuits. A relay accomplishes this by using the 5V outputted from an Arduino pin to energize an electromagnet which in turn closes an internal, physical switch attached to the aforementioned higher power circuit. You can actually hear the switch *click* closed on even small relays – just like the big ones on street corners used for traffic signals.

Sparkfun recently posted a nice tutorial on using relays to control mains supply power – strongly recommended for those interested in controlling ‘wall powered’ devices – Controlling Big, Mean, Devices

Update: Anachrocomputer adds – Another reason to use a relay is for electrical isolation. The switching contacts of a relay are completely isolated from the coil, and hence from the Arduino. The only link is by the magnetic field, represented in the diagram by a dotted line.

37 thoughts on “Connecting a Relay to Arduino

  1. anachrocomputer says:

    Thanks for posting my diagram on Make! I grew up with the late 1970s British electronics magazines, like Practical Electronics, and they always showed very clear, well-drawn diagrams. I think the clarity of a diagram like this makes a big difference. I drew it with Inkscape, incidentally, and the spiral is a sequence of Bezier curves.

    Another reason to use a relay is for electrical isolation. The switching contacts of a relay are completely isolated from the coil, and hence from the Arduino. The only link is by the magnetic field, represented in the diagram by a dotted line.

  2. The Oracle says:

    It’s been on the Arduino website for ages http://www.arduino.cc/playground/uploads/Main/relays.pdf

    It is just silly to duplicate information that’s already been made available the the community and scatter it around the web. All it does is increase the noise to signal ratio making it harder to find what you want, and it’s a waste of effort that could be expended adding a tutorial that is not already there.

    Also, this is the 21st century. There’s a lot to be said for using solid state relays, which are very cheap at sources like All Electronics.

  3. arduinonaut says:

    I was messing with this exact thing this past Friday… I had some hang ups though, so here’s a few questions for anyone really:

    1. what is the purpose of R1? same reasoning as lighting an led?

    2. what are the proper specs for the relay? 5v but what range of coil resistance will work… never did get mine to click even with direct 9v applied. (surplus relay so i dont know…)

    3. will any ‘generic’ npn transistor work for TR1? i think i had a 2n3704.

    4. also for switching small dc signals what is the difference between using a relay or a 4066 (bilateral switch) I had better luck with the bilateral switch. but other than isolation what are the benefits/uses of each…

    Thanks.

  4. Collin Cunningham says:

    @anachrocomputer – thnx, updated post

    @oracle – no, this is not noise, it is signal with increased fidelity. please keep it constructive or keep it to yourself.

  5. Chris says:

    Here’s what I usually use:

    There is this series of really cheap darlington transistor arrays: ULN2XXX
    Perfect for motors and relays, I usually buy the ULN2003A It has 7 darlington tarnsistors, build-in free-wheeling diodes and even resistors on it’s inputs.
    Which means: just directly connect your microcontroller to the ULN2003-IC, connect the ground-pin of the IC and connect the relay on the output of the IC.

  6. The Oracle says:

    @arduinonaut – R1 serves the same basic function as in an LED. Without it, you will effectively have a short circuit from the microcontroller IO to ground through the the base-emiter junction of the transistor. 2) depends on the relay. 3) generally yes, but it may depend on the relay as well.

    @Collin – Is that a new make policy, please “keep it constructive or keep it to yourself”? Because that certainly seems to contradict the general philosphy along the lines of if you can’t mess with it, you don’t own it (sorry for paraphrasing). I am doing exactly what Phillip requested of me and not directing personal attacks at anyone. Would you please clarify exactly how this adds any fidelity to the post I linked it’s the same exact information, maybe drawn slightly neater.

    I guess I was wrong to point out that the 7805-based iPod charger would not work. I’m sorry, I should have left your loyal readers to waste their time and money building it because saying it won’t work is not very constructive. Though I thought I was improving this blog because if people build things that don’t work they’ll lose respect for and faith in Make.

  7. anachrocomputer says:

    @Oracle: that PDF file you refer to is exactly the one that was causing confusion for novice users. The symbols and labelling used in it are technically correct, but are not clear to the people who I drew this for, i.e. novice Arduino users.

  8. arduinonaut says:

    @ Chris –
    if you use the uln2003a do you need a protection diode on the relay, or is that the purpose of the diode after the darlington pair in the IC? i like the looks of that thing…

  9. anachrocomputer says:

    @arduinaut: Yes, a 2N3704 will be OK for this application; there’s nothing critical, so a generic NPN part will do. The relay needs to have a 5V rated coil, and must not draw more than about 100mA. The contacts should be rated to suit the load. A 4066 CMOS switch will be fine for many types of circuits in place of a relay. What you don’t get with a 4066 is electrical isolation and high current capacity. A 4066 is, of course, silent in operation and smaller than a relay. It’s also much lower power consumption and can be driven directly from a logic level output.

  10. anachrocomputer says:

    Yes, the ULN2003A has built-in protection diodes. Don’t forget to connect the pin that links the diodes to the positive power supply! Note that a ULN2803 has eight drivers, while a ULN2003 has only seven.

  11. Sean says:

    Just ordered up a pair of 5v logic control optically isolated 25A – 125VAC solid state relays for a project. Don’t even need a coil kickback diode or transistor circuit, straight 5V TTL in. Plenty available on any of the electronics surplus sales sites for $7-10 apiece if you’re looking for higher power control.

    Thanks for posting, I did have a smaller project and needed the transistor driver schematic to run a 5v relay.

  12. xlevus says:

    For total isolation, shouldn’t you use an Optoisolator?

  13. arduinonaut says:

    thanks for the responses – interesting stuff.

    found out that uln2803 = net2018 (if that helps anyone)

    1. correctonaut says:

      should read NTE2018 not net2018

  14. billy says:

    If you wanted to use a different dc voltage relay say 12DC or 24DC, how would you hook that up? Would you just connect the second power supply ground to the Arduino ground and run the 12v or 24v where the 5v is? Will TR1 isolate the voltage from back-feeding into the Arduino?

    1. Collin Cunningham says:

      you got it – just don’t feed the Arduino itself a positive voltage above it’s max input value (usually ~12V)

  15. billy says:

    I tried doing this with an Opto22 Solid state relay Model DC60S3. I keep getting continuity across the N/O contacts, even if its not connected to the arduino. I was told by someone that I needed a bigger load an it. It didn’t seem to mater what I put on the contacts, and I don’t think its a bad relay I used two different ones and it did the same thing. When the arduino output was off the resistance did change but it didn’t close all the way. This is a link to the specs
    http://www.opto22.com/site/pr_details.aspx?cid=4&item=DC60S3

    1. Collin Cunningham says:

      hmm – they may be right. I don’t have much experience with these but the specs do mention a 3 amp nominal current rating (@ the input?). Well that’s huge in terms of arduino pins.

      I’ve only used lil plastic case guys like these –

      http://www.allelectronics.com/make-a-store/item/RLY-625/5VDC-DPDT-DIP-RELAY/-/1.html

      the continuity across N/O pins would make me think I had them mixed up – strange indeedly

  16. Billy says:

    Thanks for the input. I wasn’t sure about the 3 amp nominal current rating, I thought it was the maximum allowable current throw the coil contacts. I think I’m going to stick with standard mechanical relays until I learn a bit more about the logic of the solid state ones.

  17. F. Terence King says:

    LOts of information on this here:
    http://arduino-info.wikispaces.com/ArduinoPower

  18. F. Terence King says:

    Lots of information on how to do these things here:
    http://arduino-info.wikispaces.com/ArduinoPower

  19. David says:

    Great article.

    For those wanting in detail information about hooking up relays in general, a good resource is:

    http://www.learningaboutelectronics.com/Articles/How-to-connect-a-relay-to-a-circuit

    From this, you can know better how it would be used with an arduino board.

  20. Arduino ile Röle Kontrolü | Arduino Türkiye says:

    […] Connecting a Relay to Arduino […]

  21. MAKE | A Micro Relay at Work says:

    […] (NC) to normally-open (NO), I needed a novel circuit. Low and behold, a Flickr user posted this simple circuit to the MAKE Flickr pool in 2009 for driving a relay with the Arduino’s 5V power supply. For […]

  22. ○DIY/Tip▶ A Micro Relay at Work « dot dot dob tob tob says:

    […] (NC) to normally-open (NO), I needed a novel circuit. Low and behold, a Flickr user posted this simple circuit to the MAKE Flickr pool in 2009 for driving a relay with the Arduino’s 5V power supply. For R1 I […]

  23. A Micro Relay at Work says:

    […] (NC) to normally-open (NO), I needed a novel circuit. Low and behold, a Flickr user posted this simple circuit to the MAKE Flickr pool in 2009 for driving a relay with the Arduino’s 5V power supply. For R1 I […]

  24. 6.3 How a Micro Relay Works | Poly SteamED says:

    […] simply wanted to actuate the relay, I needed a novel circuit. Low and behold, a Flickr user postedthis simple circuit to the MAKE Flickr pool in 2009 for driving a relay with the Arduino’s 5V power supply. For R1 […]

  25. Charlyv says:

    Note that an open collector circuit is great for raw relay connections, but in the new 4 and 8 channel relay boards, it is not required and all the circuitry to drive the relay direct from an arduino digital output is on the board already. Note that some relay boards are active low, so the relay turns on with a low signal. This requires the following sequence before controlling the relay:

    #define RELAY_ON 0
    #define RELAY_OFF 1
    //prime the relay variable off before setting the pinmode
    digitalWrite(Relay_1, RELAY_OFF);
    pinMode(Relay_1, OUTPUT);
    //good to go

  26. Connected space heater says:

    […] is turned off. If you google "arduino relay" you'll find as many examples as you need. Here is just the first one I clicked on. In a practical sense, it will be easier to send a signal to […]

  27. ngalia says:

    hai,I’m using a one channel Relay module which is connected to my arduino and bulb.I connected my Bulb’s connection to the COM and NO of my Relay Module.The bulb is an AC.Then Relay Module To arduino.when I power up the bulb as well as the arduino(with blinking codes) the Bulb didnt Blink,only a steady light as if not being controlled.Can you help me with this problem?..please I need Help…

  28. Doorman Arduino - Doctor Bit's Projects | Doctor Bit's Projects says:

    […] Schematics can be found and reproduced looking to any tutorial on how to connect relays to Arduino. For example: https://makezine.com/2009/02/02/connecting-a-relay-to-arduino […]

  29. Relays says:

    […] Every hit seemed relevant to me. This was just one of them: https://makezine.com/2009/02/02/conne…ay-to-arduino/ […]

  30. Buster says:

    Can I use the 5v from the arduino to power the upper line?
    And what is the value of R1, what kind of diode do i need, and what kind of transistor do i need?

  31. James Bryant says:

    If you replace the bipolar junction transistor (BJT) with an n-channel MOSFET you can eliminate R1 – just connect the Arduino dgital pin to the gate of the MOSFET. Any small N-channel MOSFET designed for switching with 5V logic will do – see my articles at http://www.analog.com/library/analogDialogue/archives/48-05/RAQ_105.html and http://www.edn.com/design/analog/4429969/Choosing-Transistors for advice on choosing transistors for projects.

  32. Basics About Photography Pdf | Quality Photos For You And Me says:

    […] Connecting a relay to Arduino – After fielding a few questions about connecting relays to an Arduino, Anachrocomputer drew up this basic schematic for proper wiring. Why use a relay with an Arduino board? Individual applications will vary, but in short – a relay allows our relatively … […]

  33. Arduino ile Röle Kontrolü | Teknobayt says:

    […] Connecting a Relay to Arduino […]

  34. 控制繼電器的4種方法 - Make 國際中文版 > DIY Projects, Inspiration, How-tos, Hacks, Mods & More @ Makezine.com.tw - Tweak Technology to Your Will says:

    […] 如果照前面所述把繼電器接到電路,並且從Raspberry Pi或Arduino等裝置跨控制電路施加電壓(可以用任何電壓來源),可以觸發繼電器,但不會穩定運作,因為受鎖存的考量和其他問題影響。因此,我們要在電路加裝一些元件。我第一次使用繼電器時發現這個問題,並且在Stack Overflow(程式問答網站)找到很多不同的做法。這篇《MAKE》雜誌文章有搭配電路圖的說明。 ​ 我平常會先研究清楚然後寫在這裡給你看,但使用繼電器還有更簡單的方法,而且有好幾種。上述將在接下來的段落為你介紹,同時也會告訴你如何從專題的程式碼觸發繼電器。 […]

Comments are closed.

Discuss this article with the rest of the community on our Discord server!
Tagged

ADVERTISEMENT

Maker Faire Bay Area 2023 - Mare Island, CA

Escape to an island of imagination + innovation as Maker Faire Bay Area returns for its 15th iteration!

Buy Tickets today! SAVE 15% and lock-in your preferred date(s).

FEEDBACK