Connecting a 12V Relay to Arduino

582,234

230

76

Posted

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!

  • Hi. I love your arti...-slr1337

    slr1337 made it!

  • I upload my version....-SanH8

    SanH8 made it!

  • I made the circuit w...-amajhi

    amajhi made it!

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • First Time Author Contest 2018

    First Time Author Contest 2018
  • Sew Warm Contest 2018

    Sew Warm Contest 2018
user

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

Tips

Questions

What about the Vbe drop usually around 0.7 ?

65 Comments

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

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?

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