Arduino Technology
Connecting a Relay to Arduino

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. 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. It’s been on the Arduino website for ages

    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. 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…


  4. @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. 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. @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. @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. @ 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. @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. 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. 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. thanks for the responses – interesting stuff.

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

  13. 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. you got it – just don’t feed the Arduino itself a positive voltage above it’s max input value (usually ~12V)

  14. 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

  15. 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.

  16. 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

  17. 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…

  18. Pingback: Relays
  19. 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?

Comments are closed.