Language: EN


Isolated inputs and outputs with Arduino and optocoupler

As our projects grow and, especially, as we interact with systems of a higher power level, such as a 24V LED strip, a 15V motor controller, or even the electrical grid at 230V, we will need a way to isolate and protect the electronics from the control layer.

Otherwise, any connection error or malfunction of a component, or even the noise of the signals or temporary spikes, for example from motor starts, could fry our electronics.

Throughout the blog, we have already seen ways to acquire information through digital inputs, analog inputs, and sensors, and to act on them with digital outputs and analog PWM outputs. We have also learned how to communicate with other devices, for example, through the serial port, I2C, SPI, Bluetooth, among others.

We have also seen how to act on devices of another power level, for example, by taking actions using a BJT transistor, MOSFET, or a relay output. We also know how to read in these circuits using voltage dividers, voltage transformers, or current transformers.

Destroying a $1.60 Arduino may not be a big loss, but imagine that it is connected via USB to your laptop and we fry it as well. Or imagine that, as a result of a current spike, the control electronics fuse and prevent a protection from acting, causing an actuator to move uncontrollably, damaging everything in its path, or causing a vehicle to have an accident.

There is even a real danger to people’s safety, both from the transmission of high-power current and from the action or omission of a system when the controlling electronics are damaged.

Industrially, the need to isolate two circuits while maintaining communication between them has existed for some time, both for the safety of the equipment and for the people themselves.

Another common use that requires isolation is data transmission. Communication lines can accumulate spikes, noise, or interference, which need to be isolated to prevent them from damaging the transmitting and receiving systems.

There are several ways to achieve this isolation between circuits, with one of the most common being the use of an optocoupler, devices widely used in electronics and to which we are going to dedicate this post today.

What is an Optocoupler?

An optocoupler is a device that uses light to connect two electrical circuits while maintaining galvanic isolation between them, that is, the electric charge does not have a path to pass between both circuits.

The only communication between both circuits is through light, without any electrical contact. A dielectric barrier is frequently incorporated between emitter and receiver to increase the isolation to the kV order.


By having galvanic isolation, we protect the control electronics from any type of noise, overvoltage, spikes, harmonics, induced current, etc… that could damage it. In case of a catastrophic failure, the optocoupler could be destroyed, but we would only have to replace it, and it is an inexpensive component.

Optocouplers are usually provided in DIP and SMD integrated circuits, with 4 to 16 pins. There are different types of optocouplers, and each one requires a different number of pins. Additionally, there are integrated circuits with one or several optocouplers inside (single, double, and quad). The combination of these determines the number of pins of the integrated circuit.


There are many variations of the standard optocoupler. There are optocouplers in which a single emitter activates several receivers. Some optocouplers are designed so that the output is linear to the input, so they can transmit signals with minimal distortion. Others incorporate signal amplification or offset functions. There are also bidirectional optocouplers, formed by two LEDs. In short, it’s a whole world.

As you can see, there can be a lot of difference from one optocoupler to another. Therefore, check the datasheet of your component before making any assembly

How does an optocoupler work?

Optocouplers incorporate an emitter and a receiver in a single compact package. In most cases, the emitter is a GaAs IR LED diode, while the photoreceptor can be a phototransistor, a photodiode + transistor, a phototriac, among others.


The characteristics of the optocoupler depend on the type of receiver used, and the particular model of the optocoupler. Some of the main characteristics that intervene in choosing an optocoupler are:

  • Nominal voltage and currents for both the primary and secondary circuits
  • CTR (Current transfer ratio), the relationship between the energy provided by the receiver in the primary and the energy absorbed by the emitter in the primary.
  • Switching speed (bandwidth), with switching times of 1 to 20μs being common
PhotodiodeVery fastLow
Photodiode + transistorFastMedium
Photodiode + darlingtonFastHigh
PhototriacSlowVery high

Optocouplers with Arduino

There are many optocouplers that we can use with Arduino. In general, they are inexpensive devices. We can find them at a cost, depending on the models, from 0.05€ to 0.30€, searching in international sellers on eBay or AliExpress.

Some common models are the 817c (PC817, EL817…), optocouplers with a phototransistor receiver, for a cost of about 0.05€.

The 4N2X (4N25, 4N26) and 4N3X (4N35, 4N38) series are also common, all of them with a phototransistor receiver and a cost of between 0.10€ to 0.20€.

Within the optocouplers with a photodiode + transistor (high speed), we have the 6N13X (6N135 and 6N137), with a cost of about 0.30€.

Finally, the MOC3063 and similar ones, are optocouplers with a phototriac receiver, with a cost of about 0.20€.

How to use an optocoupler with Arduino?

In this post, we will focus on the optocouplers with a phototransistor (normal, or Darlington) and a photodiode + transistor. In this type of optocouplers, the secondary behaves in a similar way to a switch, controlled by the primary circuit.

Arduino optocoupled signal emitter

If we want to transmit an optocoupled signal, that is, so that Arduino is part of the primary circuit, the connection is simple. On the one hand, we power the primary circuit that powers the primary circuit, which is nothing more than a simple LED. Therefore, we only have to put a series resistor to limit the current, as we saw in the entry [Turn on an LED with Arduino].(/2015/11/encender-un-led-con-arduino/)

The value of the resistor will vary depending on the nominal current (forward current) of the optocoupler diode, its voltage drop, and the supply voltage, but a range of 220-500 Ohms is reasonable.

The secondary behaves as a switch. If we only want to operate a load, we simply connect one of the phases so that the optocoupler controls the load.


If we want a digital output (LOW or HIGH), we should use a Pull-Up or Pull-Down resistor, respectively.


In some setups, you will see a protection diode added to the optocoupler. This diode is optional and only makes sense if there is a possibility of feeding the primary circuit with a reverse current greater than the diode’s breakdown voltage.


If you are powering from Arduino and, since the breakdown voltage in many optocouplers is 6V, in general, this diode will not be necessary because the optocoupler is protected even if we accidentally reverse the cables.

Arduino optocoupler signal receiver

If what we want is for Arduino to receive an optocoupled signal emitted from another device, we simply have to read the secondary as if it were a button, as we saw in the entry Read a button with Arduino.

If we use the internal Pull-Up resistors of Arduino, we will not need to add any additional components.


Download the code

All the code in this post is available for download on Github. github-full