172а. An optocoupler at a microcontroller input: CTR with margin for heat and ageing, the LED resistor, the pull-up and speed
A signal from another device, from a 24 V sensor or from a MIDI line must not go straight to a microcontroller pin: a different ground, transients, hundreds of volts in a fault. The cheapest barrier is an optocoupler: an LED on one side, a phototransistor on the other, only light between them. Section 172 covered switches at the output; here is an input through an optocoupler: what current to give the LED so the circuit still works in ten years, what pull-up to fit and why extra current makes the input slower.
Grounds: alg.c (the LED resistor, the smallest current with margin,
a CTR estimate from the ADC, the speed limit) was built with gcc 12.2.0
on a computer (stm-sys:1) and run by host.c, where the
phototransistor is a model: the collector current follows CTR·If with a
time constant that grows with the pull-up and is limited by the
saturation current. The input thresholds are 0.3 and 0.7 of the 3.3 V
supply, as on STM32 inputs. The same alg.c was built with
arm-none-eabi-gcc 12.2.1 (stm-arm:1) for Cortex-M4F and Cortex-M0.
Terms used in the documentation
Term Meaning optocoupler, photocoupler an LED and a photodetector in one package CTR at a given LED current collector current divided by LED current CTR rank, CTR bin optocouplers sorted by CTR range CTR degradation CTR falling over time: the LED dims collector-emitter saturation voltage the voltage across the switched-on optocoupler transistor propagation delay tPHL, tPLH the delay of the output falling and rising pulse width distortion the difference between the two delays isolation voltage, creepage the insulation test voltage and the leakage path over the surface digital isolator, capacitive or magnetic transfer through a capacitor or a transformer on the die In plain words. Current through the optocoupler LED makes light, the light opens the transistor on the other side, the transistor pulls the microcontroller input to ground. How much current the transistor passes depends on how "good" the optocoupler is; with time and heat it weakens, so the current is chosen with margin. But an overfed transistor then releases the input slowly.
172а.1 Why an optocoupler at the input
An optocoupler breaks the electrical connection: the grounds of the two devices are not joined, a transient on the 24 V line stays on the LED side, and common-mode noise does not flow through the board. Typical places are inputs from limit switches and 24 V sensors (section 208б), the MIDI port (section 198п), the feedback of a mains power supply (section 208). The price is spread, slowness and ageing, covered below.
172а.2 CTR: what the datasheet promises
The main parameter is CTR, the ratio of collector current to LED current. In common single-transistor optocouplers it lies from 50 to 600 %, and the manufacturer sorts them into ranks: for example, 80–160 % or 130–260 %. The design uses the lower bound of the rank and then two corrections: when the package heats to 70–85 °C CTR falls, and over the years the LED dims — the faster, the higher its current and temperature. This section takes corrections of 0.8 for heat and 0.5 for ageing; then the 80–160 % rank gives at worst 0.8 · 0.8 · 0.5 = 0.32 — a CTR of 32 %.
172а.3 Worst-case design
For the output to go firmly to "0", the transistor must pass the whole pull-up current: (3.3 − 0.2) / RL. The optocoupler gives CTR · If, hence the smallest LED current:
float ic = (vcc - vsat) / rl;
float ctr = ctr_min * k_temp * k_age;
return ic / ctr * 1e3f;
For three pull-ups and a CTR of 32 %:
| Pull-up RL | required collector current, mA | LED current with margin, mA | resistor from 3.3 V, Ω | resistor from 24 V, Ω | resistor power from 24 V, mW |
|---|---|---|---|---|---|
| 1.0 kΩ | 3.10 | 9.69 | 217 | 2354 | 220.9 |
| 4.7 kΩ | 0.66 | 2.06 | 1019 | 11062 | 47.0 |
| 10.0 kΩ | 0.31 | 0.97 | 2168 | 23535 | 22.1 |
A 1 kΩ pull-up needs almost 10 mA — at the limit of a GPIO pin and 220 mW of heat in the resistor from 24 V: a 0.5 W resistor is needed. With a 4.7 kΩ pull-up 2 mA is enough.
172а.4 The LED resistor
The forward drop of an optocoupler LED is about 1.2 V, the resistor follows from Ohm's law:
return (vsup - vf) / (if_ma * 1e-3f);
From 24 V almost all the voltage drops across the resistor, and the power grows with current: 47 mW at 2 mA, 221 mW at 9.7 mA. A 24 V input also gets a reverse diode across the LED (its reverse voltage is only 5–6 V) and, if the line is long, a transient suppressor (section 173).
172а.5 Why a phototransistor is slow
The photocurrent is small, nothing discharges the phototransistor base, and the collector–base capacitance is multiplied by the gain — the Miller effect. The larger the pull-up, the slower this capacitance recharges: in the bench model the time constant is 1.8 µs plus 6 ns for every ohm of pull-up.
The second effect is saturation. If CTR · If is much larger than the pull-up current, the transistor holds extra charge in the base. After the LED switches off, the collector current must first fall to the pull-up current, and only then does the output start to rise: the more the transistor is overfed, the longer the input takes to release.
172а.6 Experiment: pull-up and current against speed
An optocoupler with a CTR of 100 %, a 3.3 V input; "fall" is from the LED switching on to 0.3 VDD, "rise" from switching off to 0.7 VDD. The speed limit follows the rule: each edge no longer than half a bit, the difference of delays no more than a quarter of a bit:
float b1 = 0.5e6f / tmax;
float b2 = d > 0.0f ? 0.25e6f / d : b1;
return b1 < b2 ? b1 : b2;
| Pull-up RL | LED current, mA | fall, µs | rise, µs | limit, bit/s |
|---|---|---|---|---|
| 1.0 kΩ | 1 | does not go low | — | — |
| 1.0 kΩ | 2 | does not go low | — | — |
| 1.0 kΩ | 5 | 4.8 | 12.6 | 32092 |
| 1.0 kΩ | 10 | 2.1 | 18.0 | 15645 |
| 4.7 kΩ | 1 | 20.3 | 46.7 | 9455 |
| 4.7 kΩ | 2 | 8.5 | 67.5 | 4233 |
| 4.7 kΩ | 5 | 3.1 | 95.0 | 2721 |
| 4.7 kΩ | 10 | 1.5 | 115.8 | 2188 |
| 10.0 kΩ | 1 | 16.2 | 142.9 | 1974 |
| 10.0 kΩ | 2 | 7.6 | 185.7 | 1403 |
| 10.0 kΩ | 5 | 2.9 | 242.4 | 1044 |
| 10.0 kΩ | 10 | 1.5 | 285.2 | 881 |
Three conclusions. The pull-up matters most: 1 kΩ gives tens of kilobits, 10 kΩ about one. Too little current does not pull the output low at all (1 kΩ at 1 and 2 mA). And extra current hurts: at 1 kΩ going from 5 to 10 mA cut the limit from 32092 to 15645 bit/s — the rise grew from 12.6 to 18.0 µs.
172а.7 Example: a MIDI input at 31 250 bit/s
MIDI needs 31 250 bit/s. By the table a single-transistor optocoupler fits only with a 1 kΩ pull-up and 5 mA — 32092 bit/s, barely. But that is at a CTR of 100 %. The same circuit with the worst CTR of 32 % does not pull the output low at all: 5 mA · 0.32 = 1.6 mA, less than the 3.1 mA needed. That is why a MIDI port uses fast optocouplers with a separate output stage or a built-in Schmitt trigger (6N138, H11L1, section 198п) rather than a general-purpose one. The rule is wider than MIDI: above a few kilobits take an optocoupler with a fast output or a digital isolator.
172а.8 Self-test of ageing through the ADC
If the optocoupler output also goes to an ADC input, the device can watch its ageing itself. The LED is driven with a small current at which the transistor does not saturate, and CTR is computed from the drop across the pull-up:
if (vout < vsat_lim)
return -1.0f;
float ic = (vcc - vout) / rl;
return ic / (if_ma * 1e-3f);
On the bench at 0.2 mA and a 4.7 kΩ pull-up a new optocoupler (CTR 100 %) gives 2.360 V at the output — code 2929 and an estimate of 1.000; one aged by half gives 2.830 V, code 3512 and 0.500. A saturated output (0.15 V) is rejected by the function. If the estimate falls below the margin built in at 172а.3, the device warns in advance instead of failing one frosty day.
172а.9 When not an optocoupler
- A digital isolator — for SPI, UART and CAN at hundreds of kilobits and megabits: transfer through a capacitor or a transformer on the die, no CTR, no LED ageing, delays of tens of nanoseconds.
- An optocoupler with a fast output — when LED isolation is exactly what is needed (the MIDI current loop, an input from another device).
- A general-purpose optocoupler — for slow signals: limit switches, buttons, mains presence, power supply feedback.
172а.10 Pitfalls
Designing with the typical CTR. The circuit works on the bench with a new optocoupler and fails years later or in a hot cabinet. Use the lower bound of the rank with corrections (172а.3).
An overfed LED. Extra current speeds up ageing and slows the output release: at 1 kΩ 10 mA is twice as bad as 5 mA (172а.6).
A huge pull-up to save current. 10 kΩ and more is about one kilobit; fine for a button, not for a UART.
A 0.125 W resistor from 24 V. At 10 mA it dissipates 0.22 W (172а.3).
The LED reverse voltage. 5–6 V: a swapped 24 V wire breaks it down without a reverse diode.
Ground through the shield. Isolation is pointless if the common wire is joined through a cable shield or the enclosure.
172а.11 Summary
- CTR is taken at the lower bound of the rank with corrections for heat and ageing: the 80–160 % rank is 32 % at worst.
- The smallest LED current is the pull-up current divided by the worst CTR: 9.69 mA for 1 kΩ, 2.06 mA for 4.7 kΩ.
- The pull-up sets the speed: 1 kΩ — tens of kilobits, 10 kΩ — about one; extra LED current slows the rise.
- MIDI and fast lines need optocouplers with a fast output or digital isolators, not general-purpose ones.
- CTR loss can be caught by the device itself: a small current, an ADC at the output, an estimate from the drop across the pull-up.
Check yourself
- Which CTR should the LED current be designed for? - A) The typical one from the datasheet - B) The lower bound of the rank with corrections for heat and ageing - C) The upper bound of the rank - D) CTR does not matter, 10 mA is enough
- What happens if the LED current is raised from 5 to 10 mA with a 1 kΩ pull-up? - A) The input becomes faster in every way - B) Nothing changes - C) The fall speeds up, but the rise slows down — the speed limit drops - D) The optocoupler stops working
- Why is a general-purpose optocoupler unfit for MIDI? - A) It cannot withstand 5 V - B) With the worst CTR and the required speed it does not pull the output low or is too slow - C) MIDI needs no isolation - D) It does not work with a UART
Check yourself: answers - 1 — B. Why: The 80–160 % rank with corrections of 0.8 and 0.5 gives 32 % (172а.2). - 2 — C. Why: Experiment: rise 12.6 → 18.0 µs, limit 32092 → 15645 bit/s (172а.6). - 3 — B. Why: At a CTR of 32 % 5 mA gives 1.6 mA instead of the 3.1 mA needed (172а.7).
Next: section 173 "Power and input protection".