Connecting a 12V Relay to Arduino

591,720

230

66

Published

Introduction: 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

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

Finally here is the schematic:

Step 7: 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);
     delay(10000);
     digitalWrite(outPin, LOW);
     delay(10000);
}

3 People Made This Project!

Recommendations

  • Oil Contest

    Oil Contest
  • Planter Challenge

    Planter Challenge
  • Make it Move Contest

    Make it Move Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

1 Questions

What about the Vbe drop usually around 0.7 ?

You are correct to point out that Vbe theoretically should be accounted for so that his equation for R1 would be:

R1 = (5V-0.7V) / 0.0004 A

R1 = 10750 Ohm

As homunkoloss points out, though, hfe is not very precise, so we are dealing with an approximation. The 0.7V doesn't make much difference. I usually throw it in, though. Note that including the 0.7V actually brings down R1 closer to the value homunkoloss chose (10k). I expect he went down to that value knowing that a lower value would be closer to the correct choice had he included Vbe.

66 Comments

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

3 replies

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.

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

The relay requires 30mA to pull-in. If you drove the relay directly from the Arduino pin and simply added resistors to limit the current down to 20mA, you would likely not have reliable operation (or no operation) of the relay. We use the transistor as a switch to switch the 12V supply to the relay coil with as much current as needed (limited by the relay coil resistance).

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

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 are..so 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!!

1 reply

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.

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)?

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)?

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.

1 reply

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: https://www.youtube.com/watch?v=S-QddCWt5l4

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.

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

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

codes and diagram is in link of video

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

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???

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!

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.

2 replies
user

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

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?