Isolating Circuits From Your Arduino With Optocouplers




Introduction: Isolating Circuits From Your Arduino With Optocouplers

A Optocoupler also called a photocoupler, optical isolator or opto-isolator is a small chip that transfers signals between two isolated circuits using light. A basic optocoupler uses a led and a phototransistor, the brighter the led the more current is allowed to pass through the phototransistor. For more info on how optocouplers work check out this link ( OPTOCOUPLER )...

In this instructable I will show you how to isolate and control the speed of a 12v PC fan with your ARDUINO board, This circuit could be used to control many things with minor modifications to the circuit.

So lets get started!

Step 1: Build the Circuit.

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


1. 2 X 220Ω resistors.
2. 1 X 2N2222 transistor (or any similar one that will handle the current of the devise you want to power).
3. 1 X SHARP PC817 optocoupler (or any similar one will work).
4. 1 X PCfan or motor you want to control (make sure it will handle the power supply your using) .
5. 1 X ARDUINO board.
6. jumper wires.
7. a 12 volt power source or a 9 volt battery will work if you don't have one.

Building the circuit:

The first 220Ω resistor is connected between pin 9 on the arduino board and the positive side of the optocouplers led in the chip, this resistor cuts down the voltage from the board so it does not over power the led and burn it out. If you use a different optocoupler than the SHARP PC817 you may need to adjust this resistor to compensate, the PC817 has a Max led input voltage of 1.4 volts and the resistor brings the arduino 5 volts down to 1.25 volts..

The second 220Ω resistor limits the current flow through the phototransistor in the chip, the SHARP PC817 will handle 35 volts at 50mA but it will get very hot and not last long under those conditions so I put a current limiting resistor in the circuit to keep it way under those conditions.

The transistor I recommend for this instructable is a 2N2222 but any transistor that will handle the voltage and amps of your fan or motor will work fine, If you are going to be using a motor or devise that has high current pull I suggest using a Darlington transistor like the TIP120.

If you are using a big fan or motor you might want to place a diode between the positive and negative of your devise so if the power gets cut the power generated by the fan or motor does not exceed the reverse voltage of your transistor (if the power is cut and your motor is still spinning it will act like a generator until it stops spinning). Just use a rectifier diode with the positive end connected to the negative of the fan or motor and the negative side of the diode to the positive side of your devise..

Step 2: The Code

Now that you have the circuit built lets upload the code and try it out, just copy the code below and paste it into your Arduino ide.

int fan = 9; //fan on pin 9
int spe; //fan speed
void setup() {
pinMode(fan, OUTPUT);
void loop() {
if (Serial.available()) //check for input
spe = Serial.parseInt();
spe = constrain(spe, 0, 255); //sets min and max for fan speed
analogWrite(fan, spe);
Serial.print("fan speed is - ");


Now that you have the code load it onto your arduino then open the serial monitor, Type in a number between 0 & 255 and hit enter. If everything is working properly your fan or motor should be spinning, just remember if your starting from zero (fan off) you will probably need to enter a number of 20 or higher to get the devise spinning.
I hope this instructable helped you out and if you have any questions or need some help just leave a comment and ill get back to you ASAP!!!.............

Be the First to Share


    • Reuse Challenge

      Reuse Challenge
    • Puzzles Challenge

      Puzzles Challenge
    • Metal Contest

      Metal Contest



    1 year ago

    I guess this circuit is relevant to show the potential use, but I few things crossed my mind:
    - the base of the transistor could be directly connected to an arbitrary arduino pin (through a 1kOhm resistor). Reducing the need of an optocoupler all together.
    - is the transistor really necessary in this circuit?


    3 years ago on Step 2

    Hello everyone, it's great as a solution, and in line with the editing I want to do. for room cooling. on the other hand I would also like to do it in both directions (aspirations and extraction of air)

    Rafael Karosuo
    Rafael Karosuo

    6 years ago

    I have a doubt, the Arduino Uno uses an ATmega 328p, that says in it's datasheet that has a max of 40mA per I/O pin.
    Also the TLP621 (Toshiba version) says that a typical current to it's LED activator is of 16mA and a max recommended of 20mA.
    But the thing is, that also says that the TLP621 could have an absolute max rating of 60mA, which could damage the uC.

    How often could this abs. max condition occur?

    or in which conditions?

    or what protection measures could we use to avoid uC damage if that case happens?

    Thanks a lot for your instructable, very straight forward and hands on.

    Take care.


    Reply 5 years ago

    I know this is an old post.
    The current ratings is for the optocoupler, meaning it can lead a max ampere of 60mA through the led inside. This is why you put a current limiting resistance in series with the led, to limit it's current to a nice level for the optocoupler and the arduino. This will also increase the lifetime of the optocoupler.

    So, you shouldn't run neither the opocoupler or an arduino output at 60mA at all, if you want them to last for a while.



    5 years ago

    Nice tutorial explaining optocoupler.

    Keep sharing


    6 years ago

    Thank you for posting this instructable! It has provided some very useful information!!

    I plan on using your circuit to drive Multiple LED display from an Arduino Nano v3,

    Other than using different code ( I don't need PWM, just a logic High "ON" or Low "OFF") do I need to modify the circuit to drive a 7 segnent LED display? ALL i will be doing on the output side is flashing a pre-hardwired number on the display. Thank you again!!


    7 years ago

    I like this instructable as a (nearly) standard circuit diagram is used instead of a diagram showing bread board connections only. In this way you learn more about components and understand the circuit better.


    7 years ago

    An extra layer of protection is always a good investment, thanks for sharing.