Find all your DIY electronics in the MakerShed. 3D Printing, Kits, Arduino, Raspberry Pi, Books & more!

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.

Collin Cunningham

Born, drew a lot, made video, made music on 4-track, then computer, more songwriting, met future wife, went to art school for video major, made websites, toured in a band, worked as web media tech, discovered electronics, taught myself electronics, blogged about DIY electronics, made web videos about electronics and made music for them … and I still do!


Related

Comments

  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. [...] Connecting a Relay to Arduino [...]

  21. [...] (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. [...] (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. [...] (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. [...] 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. […] 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. […] Schematics can be found and reproduced looking to any tutorial on how to connect relays to Arduino. For example: http://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: http://makezine.com/2009/02/02/conne…ay-to-arduino/ […]