Connecting a 12V Relay to Arduino

659K24781

Intro: 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);
}

71 Comments

i am using 2n2222a transistor base is connected to digital pin my 4.2v battery negative terminal is connected to collector and positive terminal is connected to analog pin .
when i make digital write high its showing correct voltage of battery but when i making low it is showing some jiter value but it should shoe zero pls tell me the solution
Because when transistor is OFF, analog pin is left floating as no current is drawn from the battery. So it will pickup stray emf around you. Connect a resistor (any value from 10k to 1M) from analog pin to ground to fix that. The downside is you drain battery a little bit more when checking battery, but nothing to worry since it drain only 4.2V/10k = 0.42mA or 17mW.

i found this and i am confused

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

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

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.

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

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.

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

More Comments