← Block library

Optocoupler Input

@electrace/optocoupler-input@1.0.0 · CC-BY-4.0
vin 5–24 Vvcc 1.8–5.5 V
PC817
The actual schematic inside this block — every part is explained below.

Optocoupler Input

Reads a signal from a world you don't trust — a 24 V industrial sensor, another machine's output, anything on long wires — with no electrical connection at all between that world and your microcontroller. The only thing that crosses the barrier is light.

  • U1 — the optocoupler. Inside the package: an LED and a phototransistor facing each other across a gap. Signal current lights the LED; the light turns on the transistor; the transistor pulls your output low. Kilovolts of isolation, two cents of silicon, and the two sides can disagree about ground by hundreds of volts without anyone noticing.
  • R1 sets the LED current — (Vin − 1.2 V)/2.2k ≈ 5–10 mA across a 12–24 V input. The datasheet's current transfer ratio (CTR) says how much collector current you get per LED milliamp; everything downstream depends on feeding the LED properly.
  • D1 sits anti-parallel across the LED because opto LEDs die at about 6 V reverse — and field wiring will get connected backwards eventually. One diode makes that a non-event.
  • R2 pulls the output high from your supply, on your side of the barrier. Signal present → output LOW. Yes, it's inverted; see the gotchas.

Exposes: in+/in− (the isolated, dirty side — 5–24 V), vcc/out/gnd (your clean side; out is active-low).

⚠ Isolation is only as good as the layout: the barrier is meaningless if a copper pour sneaks under the package or the two sides share a trace somewhere else. Keep the creepage gap clear — the chip drew the line; the board has to respect it.

Exposed nets

in+in · signal
in-in · signal
vccin · power · 1.8–5.5 V
outout · signal ·
gndin · gnd

Inside this block

R1
2.2k
sets the LED current: (Vin − ~1.2 V)/2.2k ≈ 5–10 mA across a 12–24 V signal — enough for the CTR to pull the output down hard
D1
1N4148
anti-parallel across the LED — opto LEDs die at ~6 V REVERSE, and field wiring gets reversed; this clamps it to a harmless diode drop
U1
optocoupler
the isolation barrier: an LED and a phototransistor facing each other across a gap — the only thing that crosses is light
R2
10k
output pull-up — the phototransistor can only pull LOW; this makes the HIGH, from YOUR logic supply on YOUR side of the barrier

Inside the chip: Optocoupler — an LED staring at a phototransistor

What U1 actually does, drawn out in discrete parts — the same view the editor's “break into discrete” shows.

The simplest teardown in the library, and the most honest: there is NOTHING else in the package. An LED on one side, a phototransistor on the other, and a deliberate gap between them. Current lights the LED; the light — and only the light — crosses the gap; the phototransistor conducts in proportion (the datasheet's CTR says how much). That gap is the entire product: the two halves share no wire, no ground, nothing. The drawing below has no connection between its left and right halves on purpose.

Limits & gotchas

ctr.note 0Size R1 for the LED current the datasheet's CTR curve actually needs (5–10 mA here), then remember CTR FALLS with age and temperature — a design that barely works on day one quietly stops pulling the output low in year three. Design for end-of-life CTR, not the sunny-day number.
speed.note 0A PC817-class opto is SLOW — with a 10k pull-up, edges take tens of microseconds and the phototransistor's storage tail stretches falling edges further. Fine for switches, faults, and mains-zero-cross; not for data. Past a few kHz, use a logic-output opto (6N137-class) instead.
invert.note 0The output is INVERTED: signal present → LED on → transistor on → OUT pulled LOW. Firmware reading this pin wants active-low logic (or flip it in the pin config). 'My input reads backwards' is this, every time.
Use this block in a real design
Drop it on a canvas, wire it up, and watch the live checks — free, no card.
Start designing →
This page is generated from the block's source — the same content powers the editor's explanations and live checks.