Language: EN

arduino-optoacoplador

Isolated inputs and outputs with Arduino and optocoupler

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

Otherwise, any connection error or malfunctioning component, and even noise from signals or temporary spikes, for example from motor starts, could fry our electronics.

Destroying a €1.60 Arduino may not be a great loss, but imagine it being connected via USB to your laptop and we fry that too. Or imagine that due to a current spike, the control electronics melt and prevent a protection from acting, causing an actuator to move uncontrollably and damage everything in its path, or cause 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 electronics that control it are damaged.

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

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

There are several ways to achieve this isolation between circuits, one of the most common being the use of an optocoupler, devices widely used in electronics that we will dedicate this entry to today.

What is an Optocoupler?

An optocoupler is a device that uses light to connect two electrical circuits while maintaining galvanic isolation between them, meaning the electric charge has no path to pass between the two circuits.

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

arduino-optoacoplador-interior

By having galvanic isolation, we protect the control electronics from any kind of noise, overvoltage, spike, harmonic, induced current, etc., that could damage it. In the event of a catastrophic failure, the optocoupler may be destroyed, but we would only need to replace it and it is a cheap component.

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

arduino-optoacoplador-dip

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

As you can see, there can be a lot of differences from one optocoupler to another. Therefore, review 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 integrated circuit. In most cases, the emitter is a GaAs IR LED, while the photoreceptor can be a phototransistor, a photodiode + transistor, a phototriac, among others.

arduino-optoacoplador-tipos

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 come into play when choosing an optocoupler are:

  • Nominal voltages and currents for both the primary and secondary circuits
  • CTR (Current transfer ratio), the ratio 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 typically ranging from 1 to 20μs
ReceiverSpeedCTR
PhotodiodeVery fastLow
PhototransistorMediumMedium
PhotodarlingtonMediumHigh
Photodiode + transistorFastMedium
Photodiode + darlingtonFastHigh
PhototriacSlowHigh
PhototriacSlowVery high

Optocouplers with Arduino

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

Some common models are the 817c (PC817, EL817…), optocouplers with a phototransistor receiver, costing around €0.05.

The 4N2X series (4N25, 4N26) and 4N3X (4N35, 4N38) are also common, all with phototransistor receivers and costs ranging from €0.10 to €0.20.

Among the photodiode + transistor (high speed) optocouplers, we have the 6N13X (6N135 and 6N137), costing around €0.30.

Finally, the MOC3063 and similar, are optocouplers with a phototriac receiver, costing around €0.20.

How to use an optocoupler with Arduino?

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

Arduino emitting an optocoupled signal

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

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

The secondary behaves like a switch. If we only want to activate a load, we simply count one of the phases so that the optocoupler controls the load.

arduino-optoacoplador-salida

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

arduino-optoacoplador-salida-digital

In some assemblies, you will see that a protection diode is added to the optocoupler. This diode is optional and only makes sense if there is a possibility of causing the primary circuit to experience a reverse current higher than the breakdown voltage of the diode.

arduino-optoacoplador-diodo-proteccion

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

Arduino receiving an optocoupled signal

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

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

arduino-optoacoplador-entrada