Optocoupler Basics: Let a 5 V Microcontroller Switch 12 V Safely

Author: Abloom Instruction Date: November 13, 2026

An optocoupler lets a 5 V microcontroller control a 12 V circuit without any wire connecting the two sides. Inside one small chip, an LED shines on a phototransistor: light carries the signal across the gap, and up to 5000 V of isolation stands between the logic side and the power side. This post designs a real PC817 circuit — a microcontroller pin driving a 12 V logic output — and checks that it saturates even in the worst case.

Optocoupler circuit with a 390 ohm resistor driving the LED from a 5 volt microcontroller pin and a phototransistor with a 4.7 kiloohm pull-up to 12 volts, beside a chart of collector current versus LED current for current transfer ratios of 50, 100 and 200 percent

Figure 1: The optocoupler circuit (a) and its transfer curves (b). The LED and the phototransistor share no ground — only light crosses the dashed barrier. (Schematic, not to scale.)

How the optocoupler works

The PC817 contains an infrared LED and an NPN phototransistor in one package. When the LED is on, its light turns the phototransistor on; when the LED is off, the transistor is off. There is no electrical connection between the two sides at all, so a voltage spike or a fault on the 12 V side cannot reach the microcontroller — the isolation is rated at 5000 Vrms.

The key number is the current transfer ratio (CTR): CTR = IC / IF, the collector current you get out for the LED current you put in. For the PC817 the datasheet guarantees at least 50%, with typical values around 100% and up to 600% depending on the rank. CTR varies with temperature and age, so a good design always uses the minimum value, never the typical one.

Worked example — the LED side

Drive the LED from a 5 V microcontroller pin. The PC817 LED drops about VF = 1.2 V (datasheet value at 20 mA). Aim for IF = 10 mA:

R = (5 − 1.2) / 0.01 = 380 Ω

The nearest standard value is 390 Ω, which gives:

IF = (5 − 1.2) / 390 = 9.74 mA

The resistor dissipates (5 − 1.2) × 9.74 mA = 37 mW, so an ordinary ¼ W resistor is fine. A microcontroller pin can source 10 mA easily — but check your chip's per-pin limit before using a smaller resistor for more LED current.

Worked example — the output side

On the 12 V side, a 4.7 kΩ pull-up resistor connects the collector to +12 V, and the output is taken from the collector. When the LED is off, the transistor is off, no current flows in the pull-up, and Vout = 12 V. When the LED is on, the transistor saturates and pulls the output low. The current the transistor must pass to saturate is:

IC(sat) = 12 / 4700 = 2.55 mA

Now check the CTR. At IF = 9.74 mA the transistor can pass up to CTR × IF:

CTR 100% (typical): IC = 9.74 mA  ·  CTR 50% (worst case): IC = 4.87 mA

Both are above the 2.55 mA needed, so the transistor saturates even in the worst case — with a margin of 4.87 / 2.55 = 1.9× at minimum CTR. In saturation VCE(sat) is about 0.2 V, so Vout ≈ 0.2 V when the LED is on. Notice the inversion: LED on → output LOW (0.2 V); LED off → output HIGH (12 V). Your code must allow for that.

The power in the phototransistor is tiny: 0.2 V × 2.55 mA = 0.51 mW. The pull-up resistor dissipates (12 − 0.2)2 / 4700 = 29.6 mW while the output is low.

What it can and cannot drive

This circuit gives you a 12 V logic signal — it is not a power switch. The PC817 phototransistor is rated for a maximum collector current of 50 mA, so it cannot drive a relay coil or a motor directly. To switch a real load, use the optocoupler output to drive a transistor or a MOSFET, which then handles the load current. Typical uses are a microcontroller driving a relay driver, sensing mains voltage safely, or breaking a ground loop between two pieces of equipment that must not share a ground.

Key points to remember

  • An optocoupler carries the signal as light: an LED on one side, a phototransistor on the other, with 5000 Vrms isolation and no shared ground.
  • LED side: 5 V, VF = 1.2 V, 390 Ω gives IF = 9.74 mA; the resistor dissipates 37 mW — a ¼ W resistor is fine.
  • Output side: a 4.7 kΩ pull-up to 12 V needs IC(sat) = 2.55 mA; the transistor saturates and Vout ≈ 0.2 V.
  • CTR check: even at the worst-case CTR of 50%, the transistor can pass 4.87 mA — more than the 2.55 mA needed, a 1.9× margin. Always design with the minimum CTR, not the typical 100%.
  • The output is inverted: LED on → 0.2 V, LED off → 12 V.
  • The PC817 can pass at most 50 mA — it gives a logic signal, not power; add a transistor or MOSFET to switch a relay or motor.