Sometimes you need to send a signal from one circuit to another with no electrical connection between them. For example, a 3.3 V microcontroller talking to a machine running on 24 V, or a circuit that senses whether mains power is present. The simple solution is the optocoupler.
What's inside?
A small chip with two things facing each other:
- An LED (usually invisible infrared).
- A phototransistor that conducts when light falls on it.
When you turn on the LED, the transistor conducts. The signal passes as light, and the two circuits are completely electrically isolated, up to thousands of volts depending on the chip.
PC817 pins (4 pins): 1 the LED anode, 2 the LED cathode, 3 the transistor emitter, 4 the collector. Pin 1 is marked with a dot on the body.
Why does isolation matter?
- Protection: if a fault or high voltage occurs on the other side, the microcontroller is safe.
- Different grounds: the two circuits don't need a common ground.
- Less noise: noise from motors and large loads doesn't come back into the sensitive circuit.
- Safety: when a circuit deals with mains (220 V), this isolation separates the dangerous part from the part you touch.
PC817: the best known
The LED side
Like any LED, it needs a resistor. The forward voltage is about 1.2 V, and from 3.3 V at a current of about 5 mA:
- R = (3.3 − 1.2) ÷ 0.005 ≈ 420 → 470 Ω (current ≈ 4.5 mA).
The transistor side
CTR (current transfer ratio) tells you the transistor current as a proportion of the LED current. For the PC817 the minimum is about 50% depending on the grade. So with 5 mA in the LED, the transistor can sink at least about 2.5 mA. That's enough for a signal into a microcontroller or PLC input with a pull-up resistor, but not enough to drive a relay directly. Use a transistor after it.
Note: the CTR falls with temperature and with the age of the LED (after years). So design with a margin: if you need 1 mA at the output, don't rely on exactly 50%; give the LED a bit more current.
A complete example: reading a 24 V signal
An industrial sensor or a button in an electrical panel outputs 24 V, and you want an ESP32 to read it:
- The LED resistor: (24 − 1.2) ÷ 0.005 ≈ 4560 → 4.7k. Power: 24 × 24 ÷ 4700 ≈ 0.12 W, so a quarter-watt resistor is enough.
- The output side: the collector to a GPIO pin with a 10k pull-up to 3.3 V, and the emitter to the ESP32's ground.
- The logic is inverted: 24 V present → the transistor conducts → the pin reads LOW.
pinMode(5, INPUT_PULLUP); // you can do without the external 10k
bool on = (digitalRead(5) == LOW);
If the signal could arrive reversed (a wiring mistake), put a reverse diode across the LED to protect it, because the LED can't withstand a reverse voltage of more than about 6 V.
Driving something from the opto
The current of the transistor inside is small (tens of milliamps at most, and better less). So:
- For a signal: connect it directly to a chip or PLC input.
- For a relay or motor: the opto drives a transistor or MOSFET, and that drives the load. That's exactly what's inside most relay modules.
The types
| Type | Example | Use |
|---|---|---|
| Phototransistor | PC817 | Ordinary, slow signals |
| Darlington | 4N32 | Very high CTR but slower |
| Phototriac | MOC3021 | Triggering a triac on 220 V |
| Fast digital | 6N137 | Fast signals up to a megabit |
Uses
- Relay modules: many of them contain an optocoupler isolating the Arduino's signal from the relay coil.
- Reading 12 or 24 V signals in factories (industrial sensors and buttons).
- Detecting mains presence or the zero crossing of the 220 V (with a safely designed circuit and resistors rated for high voltage).
- Switching power supplies: it feeds the voltage information from the output back to the input while staying isolated.
Notes
- An ordinary optocoupler is relatively slow (microseconds). For fast signals there are special fast types.
- For the isolation to be real, don't connect the two circuits' grounds together, and leave a gap on the board between the two sides.
Mistakes that defeat the isolation
| Mistake | Why it's a problem |
|---|---|
| Powering both sides from the same supply | The ground is now shared, and the isolation is only on paper |
| A trace or copper plane passing under the chip | High voltage can jump onto it |
| A small gap on the board between the two sides | With moisture and dust, leakage can occur |
| An LED with no resistor | The LED burns out in a second |
In 220 V circuits, the required gap between the two sides on the board is millimetres (usually 6–8 mm), and sometimes a slot is cut in the board under the chip. And if you're not sure about a mains circuit, read mains safety first.
FAQ
Can I use an opto for analogue signals? Not accurately, because the relationship between input and output isn't linear and changes with temperature. For analogue there are dedicated isolation chips.
Why does my relay module have a JD-VCC jumper? So you can power the relay side from a separate supply, and the opto on it actually isolates. If you leave the jumper on, both sides are on the same supply and the isolation isn't complete.