loading

This instructable will show you how to use any relay new or used with your ARDUINO the safe and proper way, If you purchases a preasymbled relay board for your arduino with all the components on it this is not the tutorial you are looking for. This instructable will show you how to use optocouplers and transistors to safley connect a stand alone relay to your arduino board..

SO lets get started!..

Step 1: Build the Circuit

The picture above shows the circuit you need to build..

The parts you will need:

2 x 220Ω resistors.

1 X TIP120 DARLINGTON transistor is recommended but any one that will handle your relay will work .

1 X SHARP PC817 optocoupler.

1 X relay.

1 X 1N4001 diode.

ARDUINO board, jumper wires & breadboard.

The Optocoupler is the most important part, it isolates the circuit from the relay using light. For more info about optocouplers please check out my instructable about them at this link (OPTOCOUPLERS). The 220Ω resistor cuts the voltage down from 5v to 1.2v on the led of the optocoupler, The max voltage on the led of the sharp PC817 is 1.4v.

The transistor is a darlington tip120, I chose this because it will handle almost all the small to medium sized relays you could be using. This transistor will handle up to a 60v 5amp load. The base voltage of this transistor is 5v max and that's what the 220Ω resistor is for, it brings the voltage down to 1.5v well below 5v (try to never run components to there max, they will have short lives and get very hot).

Now for the relay I'm using a 12v relay, if you are using a relay that requires a different voltage please use the appropriate power supply. Also remember NOT to connect your grounds between your arduino and power supply, that's why we are using the optocoupler to isolate them from each other.

If your relay does not have a diode built in make sure you add one, A 1N4001 or any similar will work.

Now you have the circuit built lets try it out!..

Step 2: Testing

To test your new relay circuit out just copy and paste this code into your ARDUINO IDE... Make sure you are using pin 9 on your arduino for the relay trigger!!..

int rel=9;
void setup() {
pinMode(rel,OUTPUT);
}
void loop() {
digitalWrite(rel,HIGH);
delay(1000);
digitalWrite(rel,LOW);
delay(1000);
}

---------------------------------------------------------------------------------------

This code will make your relay turn on and off every 1 second.

If you have any questions or problems please leave a comment.........

So by this I guess your saying that an arduino can't safely power relay on it's own? Just wondering why the need for isolation?
<p>First of all, a 12v relay (as shown in this instructable) can't be driven by 5v arduino output.</p><p>In addition, this relay coil resistance is 185 Ohm. That means the current is about 12/185 = 65mA (after inductive transients). Arduino can drive up to 40mA per pin (recommended 20mA per pin, max 100mA per pin group, max 200mA for entire arduino). </p><p>Even a 5v relay can't be connected directly to an arduino I/O pin. (exception with micro reed relays)</p><p>Finally, inductive loads generates high voltage on current transients (when the current changes rapidly) and capacitive loads drives high current on voltage transient: Interfacing these loads to a microcontroler or any logical circuit requires special attention, or you'll flame our arduino.</p><p>Isolation is a different topic. In this particular situation it's to &quot;float&quot; the relay's power supply from the arduino power supply. </p><p>other reasons to do so can be:</p><p>- to protect arduido from electromagnetic noise (specially with long wires connected to the load that can acts as antennas.</p><p>- to protect the arduino in case something goes wrong with the load (short between the relay coil and mains...)</p>
<p>Not sure if all of these points are valid:</p><p>1.) not enough voltage = use external supply or 5V relay</p><p>2.) not enough current = almost any transistor will do</p><p>3.) inductive transients = use flyback diode across relay</p><p>4.) electromagnetic noise... was that ever problem in reality? i only had problem with noise on INPUT pins without properly set pullups. not on output...</p><p>5.) safety in case of relay isolation breakdown... this is true... but i am not sure how dangerous relays really are... if you couldn't trust relay for isolation, how could you trust optocoupler? both are made for isolation... but well... both uses different techniques. i am not very sure about this...</p>
Thanks, very informative response. You know, I didn't even read the relay to see that it was 12v!
<p>I was wondering the same thing myself. They forgot the most important protection component too, the flyback diode. You should put a reverse biased diode across the coil to direct the inductive kickback back from the relay coil into the power supply, and away from your control electronics.</p>
I would also suggest adding a diode, across the relay coil. the anode of the diode connected to the side of the coil that is directly connected to the supply. The relay coil is an inductor and when it is switched off a reverse voltage spike can be generated. The diode would be forward biased to this spike and thus shorted safely across the coil.
<p>thanks for catching that, I cant believe I forgot to add the diode...</p>
<p>Hey I missed your comment. Yes the flyback diode is pretty important to shunt back EMF from the collapsing magnetic field of the coil away from electronics. </p>

About This Instructable

9,079views

47favorites

License:

More by Z-HUT:How to Add a Audio Alarm to Your Mouse Trap ANDROID Bluetooth Controlled ARDUINO Universal Infrared Remote Control ARDUINO how to build your own universal IR remote control 
Add instructable to: