connecting a 12v relay to arduino

7
http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/ Home Sign Up! Explore Community Submit All Art Craft Food Games Green Home Kids Life Music Offbeat Outdoors Pets Photo Ride Science Tech Connecting a 12V Relay to Arduino by gandalfsz on January 16, 2009 Table of Contents License: Attribution Non-commercial (by-nc) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Intro: Connecting a 12V Relay to Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 step 1: Measure the coil resistance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 step 2: Calculate how much current will flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 step 3: Choose your Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 step 4: Calculating R1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 step 5: Choosing your diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 step 6: The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 step 7: Assembling the Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 step 8: The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Upload: juan-arevalo

Post on 26-Oct-2015

17 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

Home   Sign Up!   Explore   Community   Submit

   All     Art     Craft     Food     Games     Green     Home     Kids     Life     Music     Offbeat     Outdoors     Pets     Photo     Ride     Science     Tech

Connecting a 12V Relay to Arduinoby gandalfsz on January 16, 2009

Table of Contents

License:   Attribution Non-commercial (by-nc) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

Intro:   Connecting a 12V Relay to Arduino . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

step 1:   Measure the coil resistance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

step 2:   Calculate how much current will flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

step 3:   Choose your Transistor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

step 4:   Calculating R1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

step 5:   Choosing your diode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

step 6:   The Schematic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

step 7:   Assembling the Circuit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

step 8:   The Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Related Instructables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Advertisements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

Page 2: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

License:   Attribution Non-commercial (by-nc)

Intro:  Connecting a 12V Relay to ArduinoTo 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 US) or BC548 (in Europe)

- 1 Relay for example one with coil voltage 12V and switching voltage 125V AC max 10 A

- 1 Multimeter

Image Notes1. 12V Relay2. Arduino Duemilanove3. 1N4007 Diode4. Transistor5. Wire

Image Notes1. max rating of things you could connect in this case: Not more than 10 Ampere125V AC2. german for coil3. 12V DC thats the Voltage the Relay needs to switch

step 1: Measure the coil resistanceWe 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 notimportant 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). Oneof them is NC and one is COM. To find out which is which let one probe connected and connect the other to the pin thats left over. If you connect the coil to 12V DC itshould make a clicking noise. If your mulimeter now shows a low resistance you have found COM and NO. The one probe you didn't move is COM the other is NO.

Image Notes

Page 3: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

Image Notes1. Pin 1 COM2. Pin 2 Coil A3. Pin 5 Coil B4. Pin 3 NO (= Normally Open)5. Pin 4 NC (= Normally Closed)

1. The measured Resistance 0.41 KOhm means 410 Ohm2. Range 20k3. If this Button is pressed the Value will stay at 0.00

step 2: Calculate how much current will flowThe 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" rigtht ? So just divide through the Resistance "R".

U = R * I / :R

I = U/R

For my relay that would be:

I = 12V / 400OhmI = 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 20 mA. So for 30mA you definitely need one.

step 3: Choose your TransistorFirst find the Datasheet of your transistor. For example search for "2N2222 datasheet".

Your Transistor should achieve 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 R1You 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 / 75Ib = 0.0004 A => 0.4 mA

Due to Ohms Law:

R1 = U / IbR1 = 5V / 0.0004 AR1 = 12500 Ohm

Page 4: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

This is not very accurate to so we use 10kOhm.

step 5: Choosing your diodeThe 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 SchematicFinally here is the Schematic:

Image Notes1. Connect the ground of your 12V source to the ground of your Arduino2. If you forget that Diode you could destroy your Arduino.3. The coil of your relay4. R1 limits Ib5. B6. C7. E

step 7: Assembling the CircuitYour 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 checkyour circuit.

Page 5: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

Image Notes1. Many 12V relays work also at a bit lower voltage (like 9V)2. The relay3. R14. D15. Transistor6. Connection between Arduino and the relay ground7. USB conneection

step 8: The ProgramThe 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);}

Related Instructables

SolderlessBreadboardLayout Sheets(plug and playelectronics) byoomlout

Time-LapsePhotography byrandofo

Powering theArduino Megawith a LithiumBattery byinthebitz

Web ControlledValentine bygschoppe

Properduino -When anEngineerGardens - Part 1- Le PropergatorWateringSystem bygrahamslawson

How to Programthe VexplorerUsing Arduinoby robotkid249

Arduino TutorialBundle.:ArduinoExperimentationKit:. (ARDX) byoomlout

Build a real bell-striking clockfor your PC anda fireextinguisher-striking clock.by 5Volt

Advertisements

Comments18 comments Add Comment

 Decypher4 says:  Jan 19, 2009. 7:44 AM  REPLYWhy are you using a 12V excitation relay with the Arduino? 5V excitation SSRs that can handle a 120V 40A AC load are cheap and readily available. Didyou just have a 12V relay laying around?

 gandalfsz says:  Jan 19, 2009. 12:29 PM  REPLYYes it was the only I had @home, its from an old washing machine.Where do you get cheap 5V SSRs ? In here the are expensive.greetz Daniel

 Doom2099 says:  Sep 9, 2009. 1:56 AM  REPLYSPDT 5v amp realy$5.08

radioshackdirect2U order

Page 6: Connecting a 12V Relay to Arduino

http://www.instructables.com/id/Connecting_a_12V_Relay_to_Arduino/

 Decypher4 says:  Jan 19, 2009. 3:14 PM  REPLYI guess it also depends on your definition of cheap.

This one is $15.00 US

http://www.allelectronics.com/make-a-store/item/SSRLY-42/40-AMP-SOLID-STATE-RELAY/1.html

There are a bunch here too. There's a million different options to sort through, and I've seen them as cheap as $9.00 US.

http://search.digikey.com/scripts/DkSearch/dksus.dll?Cat=1048664&keywords=solid%20state%20relay

Happy hunting.

Cheers

 andrew101 says:  Aug 15, 2009. 5:17 PM  REPLYwell i can get a 12 volt relay with 2 seperate switches for 4 $ canadian at my local ripoff store.

i suppose i could get them cheaper online but its eaiser to buy them there.

 gandalfsz says:  Jan 20, 2009. 6:18 AM  REPLYOk in here you can get one like mine for ~1€ (= 1,2951$)

greetz Daniel

 Decypher4 says:  Jan 21, 2009. 7:25 AM  REPLYThat's cheaper than 9 bucks. Good instructable.

 andrew101 says:  Aug 15, 2009. 5:15 PM  REPLYcool. i made oneawhile ago that i can trigger thru serial to turn on and off some relays. i didn't realise i should add a diode. good tip :)

 jtbz76 says:  Jun 29, 2009. 10:40 AM  REPLYI made this circuit and when the 5v signal from arduino is off the the relay stays on until the 12 volts is disconnected. Its like the transistor is staying on. Anyhints??

 gandalfsz says:  Jun 29, 2009. 11:09 AM  REPLYWhat is the number on your transistor ? Sure that its a transistor not a triac or something ?

 jtbz76 says:  Jun 29, 2009. 9:24 PM  REPLYNope i didnt pay attention that the diagram said view from bottom so i had the emitter and collector reversed. works great thanks!!!

 inane says:  Feb 17, 2009. 2:23 PM  REPLYCan I do without the transistor? I have a reed relay that claims a nominal current of 20mA...shouldnt the Arduino be able to drive it directly?

 gandalfsz says:  Feb 21, 2009. 2:23 PM  REPLYIf it really only needs 20mA it should be ok with Arduino, but it is better for the Controller if you use a transistor.

 russ_hensel says:  Jan 20, 2009. 5:31 PM  REPLYMicrowave ovens ( the control panel ) usually have this type of relay ( more or less ) sometimes 3 of them. Coil voltage typically 12 or 24 volts. Voltage neednot be very exact or regulated. Cost normally 0, priceless if you manage to kill yourself on the high voltage.

 RCURV says:  Jan 19, 2009. 2:02 PM  REPLYsuper useful, I'm planning a sub project an this is just the thing

 joejoerowley says:  Jan 18, 2009. 4:57 PM  REPLYGreat! I was just looking for something like this!Thanks,Joe