Introduction: Connecting a 12V Relay to Arduino

Picture of Connecting a 12V Relay to Arduino

To connect a 12V relay to the Arduino you need the following things:

- 1 Arduino

- 1 diode for example 1N4007

- 1 NPN transistor for example 2N2222 (in the US) or BC548 (in Europe)

- 1 relay for example one with coil voltage 12V and switching voltage 125VAC/10 A

- 1 multimeter

Step 1: Measure the Coil Resistance

Picture of Measure the Coil Resistance

We are going to measure the coil resistance to calculate the current.

First we must find the coil:
On some relays the pins are labeled so you can just measure at pin 2 & 5.

Otherwise you have to measure at every pin:

Between two pins you should have between 100 and 10 000 Ohm. Remember that value. That are the two terminals of the coil. The coil is not polarized so its not important which one goes to V+ or GND.

If you have found those there are only three left. Between two should be a connection (if you measure a few Ohm its okay but everything above 50Ohm is too much). One of them is NC and one is COM. To find out which is which let one probe connected and connect the other to the pin that’s left over. If you connect the coil to 12V DC it should make a clicking noise. If your multimeter now shows a low resistance you have found COM and NO. The one probe you didn't move is COM the other is NO.

Step 2: Calculate How Much Current Will Flow

The formula you need is a simple one:

(maybe people in England or the US know the Voltage as "V" but I will refer to it as "U" as we call it in here)

U = R * I

OK, but we want the current "I" right ? So just divide through the Resistance "R".

U = R * I / :R

I = U/R

For my relay that would be:

I = 12V / 400Ohm
I = 0.03 A => 30 mA (That is Ic)

The Arduino can handle up to 20mA but its better to use a transistor even if your current is only 20mA. So for 30mA you definitely need one.

Step 3: Choose Your Transistor

First find the Datasheet of your transistor. For example search for "2N2222 datasheet".

Your transistor should comply to  the following things:

- It has to be NPN not PNP !!

- Ic should be bigger than the value you calculated in step 2

- Vceo should be bigger than the supply voltage

Step 4: Calculating R1

You can find the value of hfe in your datasheet:
Mine says for BC548 its 75 at 10mA at 10V. Its not very precise cause its very difficult to build transistor with a accurate hfe.

hfe = Ic / Ib

We know hfe and Ic so lets calculate Ib:

Ib = Ic / hfe

For BC548:

Ib = 0.03 A / 75
Ib = 0.0004 A => 0.4 mA

Due to Ohms Law:

R1 = U / Ib
R1 = 5V / 0.0004 A
R1 = 12500 Ohm
This is not very accurate to so we use 10kOhm.

Step 5: Choosing Your Diode

The diode is needed cause the voltage will rise high if you suddenly change the voltage at the inductor. The formula for the voltage is:

U_L = - L * delta i/delta t

So theoretically if delta t equals zero U will be infinite.

But due to the minus in front you can add a diode in the "false direction" parallel to the relay. So the current can flow till its zero so the voltage is also zero.

Step 6: The Schematic

Picture of The Schematic

Finally here is the schematic:

Step 7: Assembling the Circuit

Picture of Assembling the Circuit

Your datasheet says which pins are E, B and C.
Before you connect your Arduino connect a 4.5V Batteries negative terminal to GND and its positive terminal to R1. The relay should make a clicking noise if not, check your circuit.

Step 8: The Program

The test program is just an edited version of the "Blink" example:

  • relaytest |
  • Author: gandalfsz |
  • Date: 18 Jan 2009 |
  • Function: Toggles Pin 13 every 10 Seconds |

int outPin = 13;

void setup()
     pinMode(outPin, OUTPUT);

void loop()
     digitalWrite(outPin, HIGH);
     digitalWrite(outPin, LOW);


OsvaldoR13 (author)2017-02-24

I used the nte123a transistor (NTE equivalent of 2n2222a) and it would not work for me. Datasheet for it gives the same hfe value of 75 @ 10ma @ 10v. But what did work was using the hfe value on the package (200) instead and recalculating. So I ended up using a 33k resistor

ranjan56 (author)2016-12-10

why is the 2n2222 transistor used? can we connect the output pin directly to relay?

StevenB38 (author)ranjan562017-02-02

An arduino can only supply so much current, usually around 20mA, enough to drive an LED. Any circuit that needs more than 20mA will need some sort of current amplifier or buffer hence the transistor.

taiphungha (author)StevenB382017-02-17

you need more resistor for down current, why you choose transistor instead of add more resistor?

chemgreec. (author)2017-01-16

Hello everyone, I am ne wto electronics and I puzzled by somethign at the moment. I want to use my arduino to latch on a relay. I use a 9v battery to power the relay and I want to control it with a 5v arduino mini pro. So WHile i was building the circuit(similar to this but with a latch on addition) the trigger from the arduino was not working. I realized that I have not connected the arduino ground to the battery ground. But then I was really confused. What would the concequence be if you connect two seperate arbitrary grounds? What if their relative state is different and there is flow of current from one to the other? I made an hydraulic parallel to understand that and I concluded that we should not be doing that at all. Let me explain: Imagine having two water tanks and we know that one has 9 metres of water in it and the other one has 3 meters of water in it. BUT we dont know at which height the tanks if you just connect their bottoms together we could potentialy blow them up imagine if one tank with 3m in it is lets say 1km up on a mountain and the other tank with 9m of water in it is at sea level. if you join their bottoms the flow would be so high that would crash the bottom tank!! How do we avoid this with circuits that have different voltages? PLease help it really bugs me!!

StevenB38 (author)chemgreec.2017-02-02

Think about the voltages that you are using as potential differences i.e the arduino is outputting 5V, which is a potential difference between its output pin and its ground line. Your relay needs a potential difference of 5V on its input line relative to its ground line in order to trigger.

If the grounds are not connected then these differences are effectively floating, so 5V from the arduino will not be enough to drive the relay as the potential differences are wrong.

Using your tank analogy the 9m tank cannot flow into the 3m tank if the bases are not on the same level.

hkfanatic (author)2016-12-08

Hi! Thanks for posting this--it's a huge help for beginners like me.

I've been researching on connecting relays to the Arduino and all of the examples I found either used a separate power supply for the 12-Volt relay (using a 9-volt battery) and the Arduino (using a USB cable) OR, the Arduino board itself supplies the power but the relay is only 5 Volts.

If I may ask, would it be possible to use only 1 power supply for the Arduino (via the DC crown jack) and three (3) 12-Volt relays (only 1 relay will operate at a given time)?

hkfanatic (author)2016-12-08

Hi. Thanks for posting this--it's a huge help for beginners like me.

I've been researching on connecting relays with the Arduino and the examples I found either uses a separate power supply for the 12-Volt relay (e.g. 9-Volt battery) and the Arduino (USB cable) OR the Arduino board itself supplies the power but the relay is a 5-Volt one.

If I may ask, would it be possible to use only 1 power supply (e.g. 12-Volts) to power both the Arduino (via the DC Crown Socket) and the 12-Volt relay (e.g. parallel mode)?

chintans (author)2015-09-08

Can some1 post diagram on how to use the relay with arduino with 12V 1A power supply to Arduino and no different power source for the relay.

Also note the protection diode is needed.
I have made the relay work but not with the protection diode.
And engineers, please, in the diagram please display from with pin to which pin the diode is used for newbies like me.

Thank you.

FrancescP2 (author)chintans2016-11-13

The PIN's are correctly detailed on the diagram, your problem aren't the PIN's, your problem is that you don't know how to read de diagram to do the correct connections.

Only 3 PIN's to be connected, and only 1 goes to Arduino, the Digital Port 13, the rest of the PIN's was connected like the blue connectors on this video:

exzibit29 (author)2016-07-27

I have a relay that has 12V DC spule, but its 6A/250VAC, can use it this circuit without changing other components? Thanks in advance.

mayur.phadte (author)2016-05-31

I used a 2N4401 and the transistor blew up. Checked all connections, could the transistor be at fault here ? Was an old one.

skumar231 (author)2016-03-24

Hi i make a video how to connect and use relay module with arduino uno.

codes and diagram is in link of video

ams31 (author)2016-02-10

The most commonly used 12 Volt cube relay has 400ohm coil resistance. So the current required is 12V/400 = 30ma . But this is the holding current the pull in current is a bit larger than that so you normally design with assumption that current required atleast 1.5 times of calculated current.

You should use 1N4148 diode instead of 1N4007 as 4148 is a fast switching diode with maximum forward current of 300ma.

If you have relays with lesser coil resistance so the current required for relay is more then you can add a pull up resistor to the arduino pin. A pull up resistor is basically a resistor between the controller pin and the Vcc. If you use pullup resistor you can use the above relay driver circuit for interfacing with any microcontroller

JohnN77 (author)2015-11-09

I have 5V relay with 70 OHM coil resistance,and i'm using 2N2222A transistor and it doesn't work with Arduino ???!!!!! any help please???

ConnorN3 (author)2015-07-08

Built it today, and it worked just as the author said it would. Only difference was that I substituted a different diode (1N4001) instead of the one suggested.

Thanks for the build!

SIM-Tech (author)2015-06-30

Ok can someone tell me why da hell do we need a transistor or a mosfet if your using a relay? the both work the same way only that one is for Ac power "relay" and the other is for Dc power "transistor/mosfet" GATE/ latch on the transistor just needs 5v to trigger it and the same can be said about the relay it needs 5v to trigger the gate /SIGNAL to open/close the circuit so whats the point? BUT even a some relay's can be used with Batteries for DC 12-30V @ 10-30A not just AC so again why use a transistor? i can see why the diode or even a resistor but a NPN or PNP com'on someone break it down just as i have.

1413 (author)SIM-Tech2015-07-02

As the author stated: he use a normal 12v relay (maybe the only one he had at hand)

and it take 30mA from 12v control source.

If you apply 5v directly to the coil of 12v relay using IO pin of arduino maybe the current is not enough to switch the latch since 5v / 400 ohm = 0.0125 A or ~13mA compare to typical 30mA of 12v relay. So you have to use a transistor as a switch so that you can use 5v source of arduino to switch on 12v source to power the relay. Any 12v source would be fine if it is able to provide current larger than 30mA.

If you use 5v relay and use arduino IO pin to drive the relay directly, the coil resistor is around 70 ohm which needs ~71mA to work. This time 71mA excess the absolute 40mA current limit of arduino IO pin. It will likely damage your arduino. So again, you have to use transistor as a switch to amplify the current apply to the relay coil.

These are typical relay you will find on ebay.

Yes, when using a single mosfet or transistor, you need less components but it is more complicate with calculation and less flexible in general applications. Say, you wish to turn on a small LED stripe or a garrage door motor, the easiest way is using a relay.

Sorry for my bad english

SIM-Tech (author)14132015-07-05

ok so i just have to use a transistor or in my case a mosfet since im playing with DC motors who are higher then spec of both the MCU & Relay? and the mosfet i choose will have to have a output current of more then 71mOhms ? so that the current of the mosfet can pass thru the coil and make it to the gate of the triggering pin which activates the relay function ? on the relay it self?

SIM-Tech (author)14132015-07-02

ok i can take that i do see your logic, thanks for that @1413

tho now hear my point of view'

you said:
5v / i40mA = 0.0125 ohms or ~13 ohms compare to typical 30mA of 12v relay.

So if arduino has 40mA and the relay needs 30mA doing your math i see no reason for the over kill, the arduino looks like supplying 40mA of which the relay only needs 30mA as you said, so what is it im not understanding? theirs a extra 10mA of a overkill so if anything a resistor should be used to not fry the relay from excess mA's

on your 2nd statement your saying the coil of the relay is 70 ohms
so RxI=V (70 ohms x 30 mA = 2100)
whats going on?

1413 (author)SIM-Tech2015-07-04

ah I see, you misunderstood some concept.

Ohm laws: R*I = V or I = V / R.

and we work with Ampere and Volt and Ohm, if it is mA or mV or kOhm, you have to convert it to standard unit to work with the equation.

First, the recommended current of each IO pin of arduino is 20mA. The absolute Maximum Ratings - the point where damage will start to happen: DC Current per I/O Pin ........... 40.0 mA

So anything that draw more than 20mA on a single IO pin is not recommended for a long time.

Second, you misunderstood my equation:

400 ohm is the resistor of the coil of the 12v-relay.

- If you apply 12v on the coil the, equation is:

I = V / R = 12 / 400 = 0.03 Ampere (30 mili Ampere)

This 30mA is the normal working condition of the relay under 12v control source

- But if you apply 5v on the coil, the equation would be:

I = V / R = 5 / 400 = 0.0125 Ampere (~13mili Ampere)

This maybe not enough to switch the latch.

And your equation : 5v / i40mA = 0.0125 ohms is not correct.

The correct equation is

R = V / I = 5v / 40mA = 5v / 0.04 A = 125 Ohm.

This means, if you can find a relay with the coil resistor is about 125 Ohm. It might work.

And this one is wrong too: RxI=V (70 ohms x 30 mA = 2100)

It should be 70 ohm x 30mA = 2100mV = 2.1v

It means if you have a 70 ohm coil, you want a current of 30mA goes through it, you will need to apply a voltage of 2.1v

OK. For a eqation like I*R = V , you should find which variable is the constant and which is not or what is known and what is not known so that you find the unknown.

Resistor, voltage = constant. You likely can't change them but you CAN mesure them using volt-ohm-meter BEFORE connect the coil to arduino. That leave Current is unknown. You haven't connect them so you can't mesure the actual current flow through the coil. And 30mA we are talking about is the ideal value for the coil to work, not the real current flow through the coil in real circuit.

Hope this will clear your mind.

JesseR5 (author)2015-04-30

Why do you show a 9V battery source when 12V is specified?

The specs for Duemilanove say pins can provide 40mA, not 20mA. If the relay coil was 5V 30mA you could power it with the 5V pin correct?

I don't understand transistors very well. A base current of .4mA with 75hfe will allow 30mA to pass from the 12V source and switch the relay, correct?

akshat gupta (author)2015-04-20

Kindly tell me how much oms resistance do we have to use to connect ardino and circuit please tell me as you connected please tell me as soon as possible

amajhi made it! (author)2015-03-07

I made the circuit with 6v relay which I really needed. The instructions were really easy to follow.

I controlled a white led and a yellow led with the relay. The red led shows the status of digital pin.

It was fun to make!
Thank you!

mabedin1 (author)2015-02-16

handymandavebritain (author)2015-02-16

The Ground from 12 Volt that is used to drive your Motor or other device is shared by the Arduino Ground and the Negative - 12Volt, this way Pin 13 is isolated & receives no +12 Volts which would obviously damage it. The relays trigger Current must be within Transistors power range, if well below milliamp (200Ma) should be OK, but a large one may be no good buy one off Bay is that's the case.

Eloisa CamelM (author)2015-02-15

thank you for this info..
but can i use 12v 120vac/10A RELAY? instead of 125vac?

Eloisa CamelM (author)2015-02-15

thank you for this info..
but can i use 12v 120vac/10A RELAY? instead of 125vac?

SasonoG (author)2015-01-11

5v arduino pin goes to transistor then goes to relay (which is need 12v), then where do relay get its 12v?? correct me if im wrong..

Anuj FALCON (author)2014-12-30

Very good

reedict (author)2014-07-26

it doest work ... your negative from the battery goes were???

ianni (author)reedict2014-08-21

Ground. This is the most simple control of a relay with a uC and it works. Have fun!

SamiulI (author)2014-08-10

First of all, I am new at this. So, if you start from the very basic, it would be helpful for me.
I have an 8pin 12v relay [exactly same as the given pic] and i don't have the pin configuration & datasheet. I want to make an water alarm system. Based on the water level I can control an LED without any hassle as arduino serves 5v which is enough for that LED.

Now I want to turn on a 12v speaker instead of the LED. Arduino cannot provide that desired 12v. Someone asked me to buy a relay and N-mosfet and these will do the job. But, i cannot understand a line. Could you please help me to build my circuit? I will be really thankful.

bogd (author)2014-07-03

Just one question (and please forgive me if you already mentioned this somewhere else): shouldn't it be R1=(U-Ube)/Ib?

With BC548 having a typical Ube ov around 0.7V, this would make it (5-0.7)/0.0004 = 10.75 KOhm.

Which makes your chosen value (10K) even more accurate :)

feedel (author)2014-04-29

I have 4 relays and I want to control it by Arduino form the following:

1 - if first relay work by Button No. 1 even if we pressed on any Switches last three relays will not work

2 - If the first switch off the second is work and it control on the third and fourth

3 - If the first and second switch off , third relay is working after pressing button 3

4 - If the first and the second and the third switch off ,forth relay is works after pressing button 4

5 -If the first switch working another time ,first relay is working and all another relays is off.

please how it work and sketch.

slr1337 made it! (author)2014-03-14

Hi. I love your article here it is very informative. I tried to make this and was somewhat successful. I say somewhat because it sorta works. When connected to my arduino uno it pulses like it is supposed to. I know because I checked the continuity between the emitter and the collector. It is clearly only open when the base has voltage to it. The problem I am having is even with no voltage to the base I connect my 12vdc power source (a car battery), and it seemingly ignores the transistor in the circuit and powers the relay coil. The coil does not lose power until the power source is disconnected. I am using the same transistor as you and the same diode. My relay is a 12v/12v and the resistance of the coil is 83 ohms making my resistor a 2.2k (tried a 2.7k it was to high). If you could help me out in understanding this it would be much appreciated.

fryddog (author)slr13372014-03-18

Hello, I have a similar problem. I've successfully made your circuit using a 2N2222A transistor. I couldn't get the 10k resistor to work with it, so I tried a 1k which didn't work either. I then tried a 330 ohm resistor which seems to work. Only now the transistor pulls in the relay when it should but then latches, keeping the relay on until I remove the 12V supply to the coil completely. Any suggestions?

slr1337 (author)fryddog2014-03-19

What are the ohms on the coil of your relay.

homunkoloss (author)slr13372014-03-19


can you post a picture of the relay where one can see the label?



slr1337 (author)homunkoloss2014-03-19

Thank you for getting back to me. I actually had a suggestion from a forum I posted on suggesting that I had the transistor's emitter and collector switched around. This was the case is working good now. Thank you.

Umair.saeed5 (author)2013-07-28

Nice Thanks man

KJ4ZVQ (author)2013-02-08

Nice! I had one in the making. Good point of showing the power formula to keep from burning up your board(that makes for a VERY bad day)!

Del Carmen (author)2013-02-04

Cool ! Now I know how to interpret the relay. I finally understood. very nice, thank you. Del Carmen

renefabri (author)2012-11-27

1. The photo does not show where are connected the green wires at the top, and the yellow wires at the bottom. Am I right to guess at the top (green) is a circuit with a lot of voltages and current (maybe 5V to 220V and 1A to 10A), and at the bottom (yellow) is a 4.5V battery?
2. The R1 resitor, is it what is called a pull-up resistor? I am puzzled because I don't see any connection to the 5V of the Arduino.

palestinian-warrior (author)2012-09-14

best tutorial ever
thank you so much
i searched the net alot
and that is the best one on the net
thanks alottt :)

salvocanna (author)2012-06-08

I made this:
It's almost the same... but the relay is 5V and is a shield.. take a look :)

jashan510 (author)2012-05-02

my coil resistance is just 71ohm so Ic comes really high
if i put a resistor in series wid d coil would it work?

dcutler1958 (author)2012-02-28

I'm a bit lost on the step of choosing a diode. How do you pick a diode knowing the current that passes across the relay coil?

gamingman (author)2011-11-06

Major point: Make sure you connect the 12V ground to the Arduino ground, or it will not work.

gabriel.hahmann (author)2011-02-23

I'm using a 5v relay and It is not working. By mistake I bought a 2N2222A, wich I saw on the datasheet had a Vebo of 6V, could you tell me if these is the problem? Congratulations for the great howto!

About This Instructable




More by homunkoloss:Connecting a 12V relay to ArduinoHow to make your GNOME Desktop even cooler
Add instructable to: