---
title: Interfacing logic to the world
heading: Driving LEDs, crossing between 5 V and 3.3 V, and how many inputs one output can feed
eyebrow: C7 · Lesson 11 · Interfacing logic to the world
xp: 40
---

::: hook
A logic output is not an ideal voltage source: lesson 1 showed its high level sagging as it sources current, and lesson 10 let several outputs share a wire. Real boards connect logic to LEDs, to chips on a different supply, and to more inputs than the datasheet example shows. A 5 V signal wired straight into a 3.3 V chip can feed current through its protection diodes and latch it up or slowly damage it; a 3.3 V output may not reach a 5 V CMOS input's high threshold at all. Each crossing has a small, calculable fix: a resistor, a divider, a transistor, or a buffer with the right thresholds.
:::

::: play
- A 5 V square wave drives a 3.3 V chip's input through a resistive divider (1.8 kΩ over 3.3 kΩ). The capacitor is the input's and the wiring's capacitance.
- The divider scales 5 V to about 3.2 V, safely inside the 3.3 V chip's range. But it also has a Thevenin resistance, and with the input capacitance that slows the edges.
- Raise both resistors to save power and watch the edges round off. At some point a fast signal no longer reaches its levels before it changes.
:::

```sim circuit
V1 in 0 square 0 5 2M Hz    @ 0,0 down
R1 in out 1.8k              @ 0,0 right
R2 out 0 3.3k               @ 2,0 down
C1 out 0 10p                @ 4,0 down
slider: R1 180..180k log
slider: R2 330..330k log
scope: v(in), v(out)
time: 0..1u s
meter: v(out)
```

::: predict
## A 3.3 V microcontroller's output (guaranteed at least 2.4 V high) drives a 5 V chip from the 74HC family, whose inputs need 3.5 V to read high. What happens?

- [ ] It works, since 2.4 V is above the usual 2 V threshold
- [x] It may not work: 2.4 V is below the 3.5 V the input requires
- [ ] It damages the 5 V chip
- [ ] It works only with a pull-down resistor

::: reveal
It may not work. 74HC inputs on 5 V use CMOS thresholds, 70% of the supply, so a valid high is 3.5 V or more, and 2.4 V sits in the forbidden band. Many boards work by luck on the bench and fail across temperature and parts. The fix is a 74HCT part, which runs on 5 V but has TTL thresholds (high above 2.0 V), or a level shifter.
:::
:::

::: nameit
## Driving an LED

A logic output can source or sink a few milliamps. Size the series resistor from the output's guaranteed level at that current:

$$R = \frac{V_{OH} - V_F}{I_{LED}} \quad\text{(sourcing)}, \qquad R = \frac{V_{DD} - V_F - V_{OL}}{I_{LED}} \quad\text{(sinking)}$$

Check the chip's total current limit too: a port of eight LEDs at 10 mA each may exceed it. For more current, use the output to switch a transistor.

## Higher voltage into a lower-voltage input

Inputs have protection diodes to their supply. A 5 V signal into a 3.3 V input turns on the upper diode, pushing current into the 3.3 V rail. Datasheets limit this injection current, often to 1 mA or so, and a chip that's powered off can be powered through its inputs. Options:

- A **series resistor** limits the injected current: $R \ge (V_{in} - V_{DD} - V_{diode})/I_{max}$.
- A **divider** scales the level: $V_{out} = V_{in}R_2/(R_1 + R_2)$, with Thevenin resistance $R_1 \| R_2$ charging the input capacitance, so edges rise with $\tau = (R_1 \| R_2)C$ and a 10 to 90% time of $2.2\tau$.
- A **5 V-tolerant input**, which some chips offer on chosen pins.

## Lower voltage into a higher-voltage input

The output's high level must exceed the input's $V_{IH}$:

- Use a part with **TTL-compatible** thresholds (74HCT, 74ACT): $V_{IH}$ = 2.0 V on a 5 V supply.
- Use an **open-drain output** with a pull-up to the higher supply (lesson 10).
- Use a **MOSFET level shifter**: an NMOS with its gate at the lower supply and pull-ups on both sides. It works in both directions, which suits I²C, but its rising edges are only as fast as the pull-ups can charge the bus: $t_r \approx 2.2RC$.

## Noise margins and fan-out

The noise margins between a driver and a receiver are $NM_H = V_{OH} - V_{IH}$ and $NM_L = V_{IL} - V_{OL}$; both must be positive, with room for ground noise. **Fan-out** is how many inputs one output can drive. For families with real input currents it's

$$N = \min\left(\frac{I_{OL}}{|I_{IL}|}, \frac{I_{OH}}{I_{IH}}\right)$$

For CMOS inputs, which draw almost no DC current, the limit is capacitance: each input adds a few picofarads, and the edges slow down.
:::

::: example
## A 3.3 V microcontroller pin holds at least 2.9 V while sourcing 8 mA. It drives a red LED that drops 2.0 V at that current. What resistor, and what current with the nearest E12 value above?

::: solution
1. **Size the resistor.** Use the guaranteed high level, 2.9 V:
   $$\begin{aligned} R &= \frac{2.9 - 2.0}{8\ \text{mA}} \\ &= 112\ \Omega \end{aligned}$$
2. **Round up to E12.** The next E12 value up is 120 Ω, giving about
   $$\frac{2.9 - 2.0}{120\ \Omega} = 7.5\ \text{mA}$$
   (a little more in practice, since the pin's high level rises as its current falls).
3. **Why the guaranteed $V_{OH}$ matters.** With 3.3 V the calculation gives 162 Ω, and the LED would get less current than planned on a weak part, but never more.

**Answer:** 112 Ω; the E12 value above it, 120 Ω, gives about 7.5 mA.
:::
:::

::: example
## A 5 V sensor output drives a 3.3 V microcontroller pin with 10 pF of input and trace capacitance. Design a divider from E12 values, and find its edge time. Compare a single series resistor that limits injection to 1 mA, with the clamp at 3.6 V.

::: solution
1. **Divider output.** A 1.8 kΩ over 3.3 kΩ divider gives
   $$\begin{aligned} V &= 5 \times \frac{3.3}{5.1} \\ &= 3.24\ \text{V} \end{aligned}$$
   inside the 3.3 V range.
2. **Divider edge time.** Its Thevenin resistance is $1.8 \| 3.3$ = 1.16 kΩ, so
   $$\begin{aligned} \tau &= 1.16\ \text{k}\Omega \times 10\ \text{pF} \\ &= 11.6\ \text{ns} \end{aligned}$$
   and the 10 to 90% rise is 25.6 ns: fine up to a few megahertz.
3. **Divider current.** It draws $5/5.1$ kΩ ≈ 1 mA whenever the signal is high.
4. **Series resistor.**
   $$\begin{aligned} R &\ge \frac{5 - 3.6}{1\ \text{mA}} \\ &= 1.4\ \text{k}\Omega \end{aligned}$$
   With 1.5 kΩ, τ = 15 ns.
5. **Series resistor's cost.** It draws nothing while the signal is low, but while it's high it relies on the clamp diode carrying up to 0.9 mA into the 3.3 V rail, which the datasheet must allow and the rail must absorb.

**Answer:** 1.8 kΩ over 3.3 kΩ gives 3.24 V and a 25.6 ns rise, drawing about 1 mA while high; a 1.5 kΩ series resistor gives τ = 15 ns but sends up to 0.9 mA through the clamp diode while high.
:::
:::

::: example
## An I²C bus between a 3.3 V and a 5 V device uses a MOSFET level shifter with 10 kΩ pull-ups on each side. With 50 pF on the 5 V side, what's the rise time there, and is it fast enough for 100 kHz I²C (1 µs maximum) and 400 kHz (300 ns)?

::: solution
1. **Rise time with 10 kΩ.**
   $$\begin{aligned} t_r &\approx 2.2RC \\ &= 2.2 \times 10^4 \times 50 \times 10^{-12} \\ &= 1.1\ \mu\text{s} \end{aligned}$$
   slightly too slow even for 100 kHz.
2. **With 4.7 kΩ.** A 4.7 kΩ pull-up gives 0.52 µs, fine at 100 kHz.
3. **For 400 kHz.** It needs about 2.2 kΩ or less (0.24 µs), at the cost of more current when the line is low:
   $$\frac{5\ \text{V}}{2.2\ \text{k}\Omega} = 2.3\ \text{mA}$$
   within the 3 mA I²C devices must sink.

**Answer:** 1.1 µs, slightly too slow even for 100 kHz; 4.7 kΩ (0.52 µs) works at 100 kHz, and 400 kHz needs about 2.2 kΩ or less.
:::
:::

::: check
given: VOH = 2.8 | 2.9 | 4.4; Vf = 1.8 | 2.0 | 2.1; I = 5 | 8 | 10
answer: (VOH - Vf)/(I*1e-3)
unit: Ω
tolerance: 0.5%

## An output guaranteed at {VOH} V high while sourcing {I} mA drives an LED with {Vf} V across it. What series resistor sets that current?

::: right
Right. $(V_{OH} - V_F)/I$ = {answer}.
:::

::: misses
- [VOH/(I*1e-3)] Subtract the LED's forward voltage.
- [any] Not yet. $R = (V_{OH} - V_F)/I$.
:::
:::

::: check
given: Vin = 5 | 12; VDD = 1.8 | 3.3; Imax = 0.5 | 1 | 2
answer: (Vin - VDD - 0.3)/(Imax*1e-3)
unit: Ω
tolerance: 0.5%

## A {Vin} V signal drives a {VDD} V chip's input through a series resistor. The clamp diode conducts at 0.3 V above the supply, and the datasheet allows {Imax} mA of injection. What's the smallest safe resistor?

::: right
Right. $(V_{in} - V_{DD} - 0.3)/I_{max}$ = {answer}.
:::

::: misses
- [(Vin - VDD)/(Imax*1e-3)] Subtract the clamp diode's 0.3 V too.
- [Vin/(Imax*1e-3)] Only the excess above the clamp drives current.
- [any] Not yet. The resistor carries the difference between the signal and the clamped input.
:::
:::

::: check
kind: parts
given: R1 = 1 | 1.8 | 10; R2 = 2.2 | 3.3 | 4.7 | 15; C = 5 | 10 | 22

## A 5 V signal drives a divider of {R1} kΩ over {R2} kΩ into an input with {C} pF.

::: part
answer: 5*R2/(R1 + R2)
unit: V
tolerance: 0.5%

### What's the high level at the input?

::: misses
- [5*R1/(R1 + R2)] The output is across the bottom resistor.
- [any] Not yet. $5R_2/(R_1 + R_2)$.
:::
:::

::: part
answer: 2.2*R1*R2/(R1 + R2)*1e3*C*1e-12*1e9
unit: ns
tolerance: 0.5%

### What's its 10 to 90% rise time?

::: misses
- [2.2*R1*1e3*C*1e-12*1e9] The capacitor sees $R_1$ in parallel with $R_2$, the Thevenin resistance.
- [R1*R2/(R1 + R2)*1e3*C*1e-12*1e9] That's one time constant; the 10 to 90% time is $2.2\tau$.
- [any] Not yet. $2.2(R_1 \| R_2)C$.
:::
:::

::: right
Right. A divider trades speed for power: larger resistors waste less current and slow the edges.
:::
:::

::: check
given: VOH = 2.4 | 2.9 | 4.4; VIH = 2 | 3.5; VOL = 0.3 | 0.5; VIL = 0.8 | 1.5
answer: VOH - VIH
unit: V
tolerance: 0.5%

## A driver guarantees $V_{OH}$ = {VOH} V and $V_{OL}$ = {VOL} V; the receiver needs $V_{IH}$ = {VIH} V and $V_{IL}$ = {VIL} V. What's the high-level noise margin? (Negative means it fails.)

::: right
Right. $NM_H = V_{OH} - V_{IH}$ = {answer}.
:::

::: misses
- [VIL - VOL] That's the low-level margin.
- [VIH - VOH] It's the output's level minus the input's requirement.
- [any] Not yet. $NM_H = V_{OH} - V_{IH}$.
:::
:::

::: check
given: IOL = 4 | 8 | 16; IIL = 0.1 | 0.4 | 1.6; IOH = 0.4 | 1; IIH = 20 | 40
answer: floor(IOL/IIL + 1e-9)*step(IOH*1e3/IIH - IOL/IIL) + floor(IOH*1e3/IIH + 1e-9)*step(IOL/IIL - IOH*1e3/IIH)
tolerance: 0.5%

## An output can sink {IOL} mA low and source {IOH} mA high. Each input it drives draws {IIL} mA low and {IIH} µA high. How many inputs can it drive?

::: right
Right: {answer}, the smaller of the two ratios.
:::

::: misses
- [any] Not yet. Compute $I_{OL}/|I_{IL}|$ and $I_{OH}/I_{IH}$ and take the smaller, rounded down.
:::
:::

::: check
given: R = 2.2 | 4.7 | 10; C = 50 | 100 | 200
answer: 2.2*R*1e3*C*1e-12*1e6
unit: µs
tolerance: 0.5%

## A pull-up of {R} kΩ charges a bus of {C} pF. What's the 10 to 90% rise time, in µs?

::: right
Right. $2.2RC$ = {answer}.
:::

::: misses
- [R*1e3*C*1e-12*1e6] That's one time constant. The 10 to 90% time is $2.2RC$.
- [any] Not yet. $t_r = 2.2RC$.
:::
:::

::: check
kind: choice

## A 3.3 V FPGA must drive a 5 V 74HC-family counter's clock input. Which fix is cleanest?

- [x] Replace the counter with its 74HCT version, which runs on 5 V with TTL input thresholds
- [ ] Add a 10 kΩ pull-up from the clock line to 5 V
- [ ] Add a series resistor
- [ ] Raise the FPGA's output current

::: right
Right. HCT inputs read high above 2.0 V, which a 3.3 V output easily reaches, and nothing else changes.
:::

::: misses
- [2] A pull-up to 5 V on a push-pull 3.3 V output would push current into the FPGA's protection diode when it drives high.
- [3] A series resistor doesn't raise the voltage; the problem is the threshold.
- [4] More current doesn't raise the output above its 3.3 V supply.
:::
:::

::: check
kind: python
given: Vin = 5 | 12 | 24; Vout = 3 | 3.2; Rmax = 2 | 10 | 50

## Choose a divider from E24 values (10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27, 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91, times any power of ten from 100 Ω to 1 MΩ) that brings `Vin` = {Vin} V down to at most `Vout_max = 3.3` V and as close as possible to `Vout` = {Vout} V, with Thevenin resistance at most `Rmax` = {Rmax} kΩ. Set `R1`, `R2` (ohms) and `v` (the output voltage). Break ties by the smaller Thevenin resistance.

```python starter
R1, R2 = 1000.0, 1000.0
v = Vin / 2
```

```python solution
base = [10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27, 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91]
vals = sorted({b * 10 ** e for b in base for e in range(1, 6)} | {1e6})
vals = [x for x in vals if 100 <= x <= 1e6]
best = None
for a in vals:
    for b in vals:
        out = Vin * b / (a + b)
        th = a * b / (a + b)
        if out > 3.3 or th > Rmax * 1e3:
            continue
        key = (abs(out - Vout), th)
        if best is None or key < best[0]:
            best = (key, a, b, out)
_, R1, R2, v = best
```

```python tests
base = [10, 11, 12, 13, 15, 16, 18, 20, 22, 24, 27, 30, 33, 36, 39, 43, 47, 51, 56, 62, 68, 75, 82, 91]
vals = sorted({b * 10 ** e for b in base for e in range(1, 6)} | {1e6})
vals = [x for x in vals if 100 <= x <= 1e6]
assert R1 in vals and R2 in vals, "Both resistors must be E24 values between 100 Ω and 1 MΩ."
assert abs(v - Vin * R2 / (R1 + R2)) < 1e-12, "v should be the divider's output."
assert v <= 3.3 and R1 * R2 / (R1 + R2) <= Rmax * 1e3, "The output must stay at or below 3.3 V, with the Thevenin resistance within Rmax."
best = min(abs(Vin * b / (a + b) - Vout) for a in vals for b in vals if Vin * b / (a + b) <= 3.3 and a * b / (a + b) <= Rmax * 1e3)
assert abs(abs(v - Vout) - best) < 1e-12, f"A closer pair exists: the best error is {best:.5f} V."
```

::: right
Right. A search over standard values beats rounding a calculated ratio, which can land on a pair whose output overshoots the limit.
:::
:::

::: explain
## A 5 V Arduino's output is wired straight to a 3.3 V sensor's input. The sensor works, but its 3.3 V rail measures 3.6 V whenever the Arduino's output is high and the sensor's own regulator is lightly loaded. Explain what's happening and how to fix it.

::: model
When the 5 V output goes high, the sensor's input protection diode to its 3.3 V rail conducts, so the output pushes current through the diode into the sensor's supply. If the rest of the circuit draws less than that current, the regulator can't sink it (most regulators only source), so the rail rises toward 5 V minus a diode drop, here 3.6 V. That stresses the sensor and anything else on the rail, and if the sensor is unpowered, it can power it through the pin. The fix is to limit or remove the injection: a series resistor sized to keep the current within the datasheet's limit, a divider that scales 5 V to about 3.2 V, or a level-shifter buffer powered from 3.3 V with 5 V-tolerant inputs.
:::
:::

::: practice

::: check
given: VDD = 3.3 | 5; Vf = 1.8 | 3.0; I = 2 | 10
answer: (VDD - Vf - 0.3)/(I*1e-3)
unit: Ω
tolerance: 0.5%

## An output sinks {I} mA with 0.3 V low, lighting an LED ({Vf} V) from the {VDD} V supply. What resistor?

::: misses
- [(VDD - Vf)/(I*1e-3)] Subtract the output's low level too.
- [any] $R = (V_{DD} - V_F - V_{OL})/I$.
:::
:::

::: check
given: R1 = 1 | 10; R2 = 2 | 20; V = 5 | 12
answer: V*R2/(R1 + R2)
unit: V
tolerance: 0.5%

## A {V} V signal drives a divider of {R1} kΩ over {R2} kΩ. What's the output?

::: misses
- [V*R1/(R1 + R2)] The output is across the bottom resistor.
- [any] $VR_2/(R_1 + R_2)$.
:::
:::
:::
