Sometimes using a direct connection between arduino and some devices like transistors, relays etc can damage your pins(think if you have a shortcircuit and your pin drive more than it’s maximum current, around 40mA). So using a optocoupler may be a good idea because this device separate the arduino’s pin and the rest of the circuit, using light. The most common construction type is formed by an LED and phototransistor. For example the model PC 123 used by us contains an IRED(infrared led) optically coupled to a phototransistor.
Because the voltage of the pin is 5 V we must choose a resistor between pin and LED to have the exact value of IF(20mA), current supported by the pin.
On the transistor side if we have a device which consume less than 20mA we can connect it between source and collector, because the emitter must be connected to ground(npn transistor).
But most devices exceed 20mA, so we will make a circuit like in the picture below:
RF is used to limit the IF current to a 20mA value(we have used a 220 ohms resistor). The OUT part work like a voltage divider when the transistor is block we have 5V on OUT pin and when it is polarized is equivalent with an closed contact so the voltage on the OUT pin is almost zero.
When the transistor(from the optocoupler) is polarized through it passes an Ic current that must not exceed a 20mA value(limit from datasheet)(for that is R).
As you can see this circuit is reversed, when we have 5V on arduino pin(HIGH state) the OUT pin is LOW. In examples 1 and 2(picture above) we have eliminated that inconvenience using a pnp bipolar transistor and a p-channel MOSFET. This transistors are blocked when they have 5V on base and gate and polarized when they have 0V on base and gate.
When you have a voltage different from 5V on the OUT part you need to recalculate the R value.
The program from videos is the “blink” program from examples with a larger delay.
Please let us in the comment zone any suggestions that you think will improve the article!
If you like the article click the follow button from social media to stay in touch with us!
Below you have two short videos one with bipolar transistor and one with MOSFET transistor: