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



  • The picture above shows the circuit you will need to build..THE PARTS YOU WILL NEED:

1. 2 X 220Ω resistors.
2. 1 X 2N2222 transistor (or any similar one that will handle the current of the device 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 tutorial 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 device 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 device..




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

I hope this tutorial helped you out and if you have any questions or need some help just leave a comment on our Facebook page…