---
title: Op-amp design lab
heading: From specifications to currents and a capacitor, and back again
eyebrow: C6 · Lesson 52 · Op-amp design lab
xp: 50
---

::: hook
Lesson 41's design procedure is a chain of small formulas, and chains of formulas belong in code: once they're functions, you can try a dozen designs in a second and see what each specification costs. In this lab you'll write a function that predicts a two-stage op amp's gain, bandwidth, phase margin and slew rate from its currents and capacitors, a function that runs the design procedure from a specification, and a search that finds the smallest compensation capacitor giving a required phase margin.
:::

::: play
- The two-stage op amp's phase margin and gain-bandwidth against its compensation capacitor, for the design of lesson 41 (20 µA tail, 100 µA output stage, 0.2 V overdrives, 10 pF load).
- More $C_c$ means more margin but less bandwidth. The readouts give both at the slider's value.
- Find the smallest $C_c$ that still gives 60°: that's the fastest stable design.
:::
:::

```sim plot
view: graph
x: Cc = 0.2..10 pF log
y: v = 0..100
slider: c = 0.2..10 = 2.2 pF log
plot: 90 - atan(sqrt((sqrt(1 + 4*(1e-4/(2*pi*Cc*1e-12)/15.915e6)^2) - 1)/2))*180/pi | label: phase margin, °
plot: 1e-4/(2*pi*Cc*1e-12)/1e6 | label: GBW, MHz | dashed | color: ch2
marker: x = c | label: chosen Cc
readout: phase margin, ° = 90 - atan(sqrt((sqrt(1 + 4*(1e-4/(2*pi*c*1e-12)/15.915e6)^2) - 1)/2))*180/pi | digits: 3
readout: GBW, MHz = 1e-4/(2*pi*c*1e-12)/1e6 | digits: 3
```

::: predict
## For that op amp, what is the smallest $C_c$ that gives 60° of phase margin?

- [ ] 0.5 pF
- [x] About 1.5 pF
- [ ] 2.2 pF
- [ ] About 5 pF

::: reveal
About 1.5 pF. The rule of thumb's 2.2 pF gives 67°, a little more than needed; 1.5 pF reaches exactly 60° and raises the GBW from 7.2 MHz to 10.6 MHz. A search like this, run on the full equations, is how design rules of thumb get refined.
:::
:::

::: nameit
## Performance from the design

For tail current $I_5$, second-stage current $I_7$, overdrives $v_{OV1}$ (input pair) and $v_{OV6}$ (output device), equal λ, compensation $C_c$ and load $C_L$:

$$g_{m1} = \frac{I_5}{v_{OV1}} \qquad g_{m6} = \frac{2I_7}{v_{OV6}} \qquad A_0 = \frac{1}{\lambda v_{OV1}}\cdot\frac{1}{\lambda v_{OV6}}$$

$$\text{GBW} = \frac{g_{m1}}{2\pi C_c} \qquad f_2 = \frac{g_{m6}}{2\pi C_L} \qquad \text{SR} = \min\left(\frac{I_5}{C_c}, \frac{I_7}{C_L + C_c}\right)$$

and the phase margin from the two-pole crossover of lesson 39, $f_c = f_2\sqrt{(\sqrt{1 + 4(\text{GBW}/f_2)^2} - 1)/2}$, PM = $90° - \arctan(f_c/f_2)$.

## Design from the specification

Given GBW, SR and $C_L$, with $g_{m6} = kg_{m1}$ (k ≈ 10) and $v_{OV6}$ chosen:

1. $C_c = 2.2C_L/k$;
2. $I_5 = \text{SR} \cdot C_c$;
3. $g_{m1} = 2\pi\,\text{GBW}\,C_c$, so $v_{OV1} = I_5/g_{m1}$;
4. $g_{m6} = kg_{m1}$, so $I_7 = g_{m6}v_{OV6}/2$.

## Searching

Phase margin rises steadily with $C_c$, so bisection on $C_c$ (in log space, between 1 fF and 1 nF) finds the smallest value that meets a margin.
:::

::: example
## Predict the performance of lesson 41's design: 20 µA, 100 µA, 0.2 V overdrives, λ = 0.05 V⁻¹, $C_c$ = 2.2 pF, $C_L$ = 10 pF.

::: solution
1. **DC gain.** $A_0$ = 80 dB.
2. **Gain-bandwidth and second pole.** GBW = 7.23 MHz and $f_2$ = 15.9 MHz.
3. **Phase margin.** The exact crossover is 6.7 MHz, so PM = 67.3°. The quick estimate with crossover at GBW gave 66°.
4. **Slew rate.**
   $$\begin{aligned} \text{SR} &= \min(9.1, 8.2) \\ &= 8.2\ \text{V}/\mu\text{s} \end{aligned}$$

**Answer:** 80 dB of gain, GBW = 7.23 MHz, a phase margin of 67.3° and a slew rate of 8.2 V/µs.
:::
:::

::: example
## Design for GBW = 10 MHz and SR = 10 V/µs into 5 pF, with k = 10 and $v_{OV6}$ = 0.2 V.

::: solution
1. **Compensation capacitor.** $C_c$ = 1.1 pF.
2. **Tail current.** $I_5$ = 11 µA.
3. **Input stage.** $g_{m1}$ = 69.1 µS, so $v_{OV1}$ = 0.16 V.
4. **Output stage.** $g_{m6}$ = 0.691 mS, so $I_7$ = 69 µA.
5. **Total current.** 11 + 69 = 80 µA.

**Answer:** $C_c$ = 1.1 pF, $I_5$ = 11 µA, $v_{OV1}$ = 0.16 V and $I_7$ = 69 µA, 80 µA in total.
:::
:::

::: check
given: I5 = 10 | 100; vov1 = 0.1 | 0.3; Cc = 0.5 | 5
answer: I5*1e-6/vov1/(2*pi*Cc*1e-12)/1e6
unit: MHz
tolerance: 0.5%

## A two-stage op amp has a {I5} µA tail, input overdrive {vov1} V and $C_c$ = {Cc} pF. What is its GBW?

::: right
Right. $\frac{I_5/v_{OV1}}{2\pi C_c}$ = {answer}.
:::

::: misses
- [any] Not yet. $g_{m1} = I_5/v_{OV1}$, then GBW = $g_{m1}/(2\pi C_c)$.
:::
:::

::: check
given: I7 = 50 | 500; vov6 = 0.15 | 0.3; CL = 2 | 20
answer: 2*I7*1e-6/vov6/(2*pi*CL*1e-12)/1e6
unit: MHz
tolerance: 0.5%

## Its output device carries {I7} µA at {vov6} V of overdrive, into {CL} pF. Where is the second pole?

::: right
Right. $\frac{2I_7/v_{OV6}}{2\pi C_L}$ = {answer}.
:::

::: misses
- [I7*1e-6/vov6/(2*pi*CL*1e-12)/1e6] $g_m = 2I/v_{OV}$.
- [any] Not yet. $g_{m6} = 2I_7/v_{OV6}$, then $f_2 = g_{m6}/(2\pi C_L)$.
:::
:::

::: check
kind: python

## Write `performance(I5, I7, vov1, vov6, lam, Cc, CL)` returning a dict with `A0_db`, `GBW` (Hz), `f2` (Hz), `pm` (degrees, from the exact two-pole crossover) and `sr` (V/s).

```python starter
import math

def performance(I5, I7, vov1, vov6, lam, Cc, CL):
    gm1 = I5 / vov1
    GBW = gm1 / (2 * math.pi * Cc)
    return {"A0_db": 0.0, "GBW": GBW, "f2": 1e12, "pm": 90.0, "sr": I5 / Cc}
```

```python solution
import math

def performance(I5, I7, vov1, vov6, lam, Cc, CL):
    gm1 = I5 / vov1
    gm6 = 2 * I7 / vov6
    A0 = (1 / (lam * vov1)) * (1 / (lam * vov6))
    GBW = gm1 / (2 * math.pi * Cc)
    f2 = gm6 / (2 * math.pi * CL)
    fc = f2 * math.sqrt((math.sqrt(1 + 4 * (GBW / f2) ** 2) - 1) / 2)
    pm = 90 - math.degrees(math.atan(fc / f2))
    sr = min(I5 / Cc, I7 / (CL + Cc))
    return {"A0_db": 20 * math.log10(A0), "GBW": GBW, "f2": f2, "pm": pm, "sr": sr}
```

```python tests
cases = [((20e-6, 100e-6, 0.2, 0.2, 0.05, 2.2e-12, 10e-12), {"A0_db": 80.0, "GBW": 7234315.595086153, "f2": 15915494.309189536, "pm": 67.25646670241048, "sr": 8196721.311475411}),
         ((50e-6, 200e-6, 0.15, 0.25, 0.08, 1e-12, 5e-12), {"A0_db": 72.39577516576789, "GBW": 53051647.69729845, "f2": 50929581.789406516, "pm": 51.00637118889222, "sr": 33333333.33333334}),
         ((10e-6, 80e-6, 0.1, 0.2, 0.04, 3e-12, 20e-12), {"A0_db": 89.89700043360187, "GBW": 5305164.769729845, "f2": 6366197.723675814, "pm": 55.51500575659698, "sr": 3333333.3333333335})]
for args, want in cases:
    got = performance(*args)
    for k, w in want.items():
        assert abs(got[k] - w) <= 1e-9 * abs(w) + 1e-9, f"For {args}: {k} should be {w:.6g}, got {got[k]:.6g}."
```

::: right
Right. The starter ignored the second pole, so it reported 90° of margin for every design, including the second one here, which has only 51°.
:::
:::

::: check
kind: python

## Write `design(GBW, SR, CL, k=10, vov6=0.2)` returning a dict with `Cc`, `I5`, `gm1`, `vov1`, `gm6` and `I7`, following the four steps.

```python starter
import math

def design(GBW, SR, CL, k=10, vov6=0.2):
    Cc = CL
    return {"Cc": Cc, "I5": SR * Cc, "gm1": 0.0, "vov1": 0.0, "gm6": 0.0, "I7": 0.0}
```

```python solution
import math

def design(GBW, SR, CL, k=10, vov6=0.2):
    Cc = 2.2 * CL / k
    I5 = SR * Cc
    gm1 = 2 * math.pi * GBW * Cc
    vov1 = I5 / gm1
    gm6 = k * gm1
    I7 = gm6 * vov6 / 2
    return {"Cc": Cc, "I5": I5, "gm1": gm1, "vov1": vov1, "gm6": gm6, "I7": I7}
```

```python tests
cases = [((10e6, 10e6, 5e-12), {"Cc": 1.1e-12, "I5": 1.1e-05, "gm1": 6.911503837897546e-05, "vov1": 0.15915494309189535, "gm6": 0.0006911503837897546, "I7": 6.911503837897546e-05}),
         ((50e6, 30e6, 2e-12), {"Cc": 4.4e-13, "I5": 1.32e-05, "gm1": 0.0001382300767579509, "vov1": 0.09549296585513721, "gm6": 0.001382300767579509, "I7": 0.00013823007675795092})]
for args, want in cases:
    got = design(*args)
    for k, w in want.items():
        assert abs(got[k] - w) <= 1e-9 * abs(w), f"For {args}: {k} should be {w:.6g}, got {got[k]:.6g}."
```

::: right
Right. Notice the second design asks for 50 MHz into 2 pF and gets an input overdrive under 0.1 V: fast op amps push their input devices toward weak inversion, where the square law stops applying.
:::
:::

::: check
kind: python

## Write `min_cc(I5, I7, vov1, vov6, lam, CL, pm=60)`: the smallest $C_c$ giving at least `pm` degrees, by bisection on $\log C_c$ between 1 fF and 1 nF (200 steps), using your `performance`.

```python starter
import math

def performance(I5, I7, vov1, vov6, lam, Cc, CL):
    gm1 = I5 / vov1
    gm6 = 2 * I7 / vov6
    GBW = gm1 / (2 * math.pi * Cc)
    f2 = gm6 / (2 * math.pi * CL)
    fc = f2 * math.sqrt((math.sqrt(1 + 4 * (GBW / f2) ** 2) - 1) / 2)
    return {"pm": 90 - math.degrees(math.atan(fc / f2))}

def min_cc(I5, I7, vov1, vov6, lam, CL, pm=60):
    return 2.2 * CL / 10
```

```python solution
import math

def performance(I5, I7, vov1, vov6, lam, Cc, CL):
    gm1 = I5 / vov1
    gm6 = 2 * I7 / vov6
    GBW = gm1 / (2 * math.pi * Cc)
    f2 = gm6 / (2 * math.pi * CL)
    fc = f2 * math.sqrt((math.sqrt(1 + 4 * (GBW / f2) ** 2) - 1) / 2)
    return {"pm": 90 - math.degrees(math.atan(fc / f2))}

def min_cc(I5, I7, vov1, vov6, lam, CL, pm=60):
    lo, hi = 1e-15, 1e-9
    for _ in range(200):
        m = math.sqrt(lo * hi)
        if performance(I5, I7, vov1, vov6, lam, m, CL)["pm"] >= pm:
            hi = m
        else:
            lo = m
    return math.sqrt(lo * hi)
```

```python tests
cases = [((20e-6, 100e-6, 0.2, 0.2, 0.05, 10e-12), 60, 1.5e-12), ((20e-6, 100e-6, 0.2, 0.2, 0.05, 10e-12), 70, 2.581784256837418e-12),
         ((50e-6, 200e-6, 0.15, 0.25, 0.08, 5e-12), 60, 1.5625e-12), ((50e-6, 200e-6, 0.15, 0.25, 0.08, 5e-12), 70, 2.6893586008723094e-12)]
for args, pm, want in cases:
    got = min_cc(*args, pm=pm)
    assert abs(got / want - 1) < 1e-6, f"For {args} at {pm}°: expected {want * 1e12:.4f} pF, got {got * 1e12:.4f} pF."
```

::: right
Right. For 60° the crossover must sit at $f_2\tan 30°$, which makes GBW exactly $\frac23f_2$ and $C_c = \frac{3g_{m1}C_L}{2g_{m6}}$: 1.5 pF for the first design. The rule-of-thumb 2.2 pF buys an extra 7°.
:::
:::

::: explain
## Why does a design procedure fix $C_c$ first, before any currents?

::: model
Every important speed specification runs through the compensation capacitor. The phase margin depends on the ratio of the second pole to the gain-bandwidth, which for a given load is set by $C_c$ and the ratio of the two stages' transconductances; once that ratio is chosen, $C_c$ follows from the load alone. Then the slew rate fixes the tail current, since the tail charges $C_c$, and the gain-bandwidth fixes the input transconductance, since $g_{m1}/C_c$ sets it. Choosing $C_c$ first turns each remaining specification into one equation for one unknown.
:::
:::

::: practice

::: check
given: CL = 2 | 10 | 50; k = 5 | 10
answer: 2.2*CL/k
unit: pF
tolerance: 0.5%

## For $g_{m6} = $ {k} $g_{m1}$ and a {CL} pF load, what $C_c$ does the design procedure choose?

::: misses
- [any] $2.2C_L/k$.
:::
:::

::: check
given: SR = 5 | 50; Cc = 0.5 | 3
answer: SR*Cc
unit: µA
tolerance: 0.5%

## What tail current gives {SR} V/µs with $C_c$ = {Cc} pF?

::: misses
- [any] $\text{SR} \cdot C_c$.
:::
:::

::: check
given: gm6 = 0.2 | 2; vov6 = 0.15 | 0.3
answer: gm6*vov6/2*1000
unit: µA
tolerance: 0.5%

## What current gives an output device $g_m$ = {gm6} mS at {vov6} V of overdrive?

::: misses
- [any] $I = g_mv_{OV}/2$.
:::
:::
:::
