How to Use Any Relay With Your Arduino the Safe Way




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

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

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() {
void loop() {


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

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

Be the First to Share


    • Made with Math Contest

      Made with Math Contest
    • Multi-Discipline Contest

      Multi-Discipline Contest
    • Robotics Contest

      Robotics Contest

    10 Discussions


    5 months ago

    I have had lots of problems using ardruino's to control high power devices like compressors and fans through relays. Powering those devices can cause electrical interference which may crash or reset the arduino. These problems can be resolved by using a snubber (google it) of the electrical contacts of the relay-switch. Also ferrite core's between the arduino and the relays did make an improvement.

    If you powering critical systems trough a arduino, it's also a good habit to use the watchdog timer.


    Question 1 year ago on Step 2

    If I use 5v VCC do I have to change R2 resistor of Q1?


    4 years ago

    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?

    4 replies

    Reply 4 years ago

    First of all, a 12v relay (as shown in this instructable) can't be driven by 5v arduino output.

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

    Even a 5v relay can't be connected directly to an arduino I/O pin. (exception with micro reed relays)

    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.

    Isolation is a different topic. In this particular situation it's to "float" the relay's power supply from the arduino power supply.

    other reasons to do so can be:

    - to protect arduido from electromagnetic noise (specially with long wires connected to the load that can acts as antennas.

    - to protect the arduino in case something goes wrong with the load (short between the relay coil and mains...)


    Reply 3 years ago

    Not sure if all of these points are valid:

    1.) not enough voltage = use external supply or 5V relay

    2.) not enough current = almost any transistor will do

    3.) inductive transients = use flyback diode across relay

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

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


    Reply 4 years ago

    Thanks, very informative response. You know, I didn't even read the relay to see that it was 12v!


    Reply 4 years ago

    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.


    4 years ago

    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.

    2 replies

    Reply 4 years ago

    thanks for catching that, I cant believe I forgot to add the diode...


    Reply 4 years ago

    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.