How to Use an Optocoupler or Photo Transistor with Arduino

Often you want to switch a load on or off with your Arduino project, but want to make sure its on a different power circuit due to noise, or simply different voltages.

What Are They?

An Optocoupler behaves in a similar way to a relay, in that the circuit you are switching is completely isolated from the circuit used to do the switching.

Functional Diagram of an Opto Coupler

With relays this is done using an electromagnet to physically move the contacts.  In an Opto Coupler / Photo Transistor an LED triggers a photo sensitive transistor.


Often an optocoupler is used to switch a relay on/off, so a microcontroller can switch the larger coil load in an isolated way.

These are shown in the following image, highlighted in the green boxes.

Example of Opto Couplers used on a Relay Board


Why Use Them

Uses less current to switch the device on/off than a relay

No mechanical wear like a relay

Unaffected by surrounding light / fields as built into IC

BUT compared to a relay they cannot switch high currents as it is still a transistor performing the switching


Wiring Them Up

The below is for the 4N25 Transistor to a 3.3v Arduino XXXX

Note IconNote

Check the polarity of IC before connecting, and ensure you add a resistor for the internal LED.

Check your datasheet for maximum ratings and power dissapation (e.g the 4N25 we are using here)

Wiring an Opto Coupler to an Microcontroller