---
title: Mirror and differential pair lab
heading: Mirrors, Widlar sources, and the large-signal differential pair, in Python
eyebrow: C6 · Lesson 8 · Mirror and differential pair lab
xp: 50
---

::: hook
Lessons 3 to 7 built the integrated amplifier's two essential pieces: the current mirror and the differential pair. Their formulas came with approximations: a mirror with an exact β but an Early effect added by hand, a Widlar equation that can't be solved in closed form, a pair whose small-signal gain says nothing about how far it can swing. In this lab you'll compute them exactly, solve the Widlar source numerically, trace both pairs' full transfer curves, and finish with an OTA's gain, bandwidth and slew rate.

::: play
- The curves are the fraction of the tail current carried by one side of a bipolar pair and of a MOSFET pair, against the differential input.
- The bipolar pair switches over within about ±100 mV, set by $V_T$. The MOSFET pair's range is set by its overdrive: raise it and the curve stretches.
- At the same tail current, which pair has the steeper slope at the center, and which stays linear further?
:::
:::

```sim plot
view: graph
x: v = -0.6..0.6 V
y: r = 0..1
slider: ov = 0.05..0.5 step 0.01 = 0.2 V
plot: 1/(1 + exp(-v/0.02585)) | label: bipolar pair | samples: 400
plot: step(v + sqrt(2)*ov)*step(sqrt(2)*ov - v)*(0.5 + v/(2*ov)*sqrt(abs(1 - (v/(2*ov))^2))) + step(v - sqrt(2)*ov) | color: ch2 | label: MOSFET pair | samples: 600
readout: MOSFET pair fully switched at, V = sqrt(2)*ov | digits: 3
```

::: predict
## A bipolar pair and a MOSFET pair with 0.2 V of overdrive share the same tail current. Which carries 90% of the tail on one side with the smaller input?

- [x] The bipolar pair, at about 57 mV
- [ ] The MOSFET pair, at about 57 mV
- [ ] Both at the same input
- [ ] The MOSFET pair, at about 20 mV

::: reveal
The bipolar pair: $V_T\ln9$ = 56.8 mV. The MOSFET pair needs 0.18 V. The bipolar pair's higher transconductance per milliamp is the same fact seen from the other side: it switches over a narrower range of input, so it stays linear over a narrower range too.
:::
:::

::: nameit
## The mirror with every error

A BJT mirror loses two base currents and copies with a different $V_{CE}$ on each side:

$$I_O = \frac{I_\text{ref}}{1 + 2/\beta}\cdot\frac{1 + V_{CE2}/V_A}{1 + V_{BE}/V_A}.$$

## The Widlar source

With $R_E$ in the output transistor's emitter, the two $V_{BE}$'s differ by $I_OR_E$:

$$I_OR_E = V_T\ln\frac{I_\text{ref}}{I_O}.$$

Given $I_O$ it gives $R_E$ directly; given $R_E$ it's transcendental in $I_O$. The left side rises with $I_O$ and the right falls, so bisection between 0 and $I_\text{ref}$ (on a log scale, since $I_O$ may be orders of magnitude smaller) finds the one crossing.

## The pairs' transfer curves

Bipolar: $\frac{i_{C1}}{I} = \frac{1}{1 + e^{-v_{id}/V_T}}$, so a fraction f flows on one side at $v_{id} = V_T\ln\frac{f}{1 - f}$.

MOSFET, with $V_{OV} = \sqrt{I/K}$ the overdrive at balance:

$$i_{D1} = \frac I2 + \frac{I}{V_{OV}}\cdot\frac{v_{id}}{2}\sqrt{1 - \left(\frac{v_{id}/2}{V_{OV}}\right)^2}$$

for $|v_{id}| \le \sqrt2V_{OV}$; beyond that, one side carries the whole tail.

## The OTA

With tail I, input overdrive $v_{OV}$, and every device's channel-length modulation λ: $A_d = g_{m1}(r_{o2} \parallel r_{o4}) = \frac{2}{\lambda v_{OV}}$, $\text{GBW} = \frac{g_{m1}}{2\pi C_L}$ and $\text{SR} = \frac{I}{C_L}$.
:::

::: example
## A BJT mirror with β = 100 and $V_A$ = 100 V copies 1 mA, with its output at 5 V. What does it deliver?

::: solution
1. **Output current.** The base currents divide it by 1.02, and the Early effect multiplies it by 1.05/1.007:
   $$\begin{aligned} I_O &= \frac{1\ \text{mA}}{1.02}\cdot\frac{1.05}{1.007} \\ &= 1.0223\ \text{mA} \end{aligned}$$
2. **The two errors.** The base currents make it 2% low and the Early effect 4.3% high, so the output errs high by 2.2%.
3. **Why not count on cancelling.** The two errors depend on different things (β and the output voltage), so a design can't rely on them cancelling.

**Answer:** the mirror delivers 1.0223 mA, 2.2% high.
:::
:::

::: example
## A Widlar source has a 1 mA reference and $R_E$ = 1 kΩ. What does it deliver?

::: solution
1. **Solve for the current.** Bisection on $I_OR_E - V_T\ln\frac{I_\text{ref}}{I_O}$ gives 69.1 µA.
2. **Check.** The resistor drops 69.1 mV, which equals $V_T\ln\frac{1}{0.0691}$.
3. **Lesson 3's design, run the other way.** For 10 µA,
   $$\begin{aligned} R_E &= \frac{0.02585\ln100}{10\ \mu\text{A}} \\ &= 11.9\ \text{k}\Omega \end{aligned}$$
   and the solver recovers exactly 10 µA from that resistor.

**Answer:** the Widlar source delivers 69.1 µA.
:::
:::

::: example
## A MOSFET pair with K = 20 mA/V² and a 1 mA tail gets 0.1 V, then 0.4 V, of differential input. What does one side carry?

::: solution
1. **Overdrive at balance.**
   $$\begin{aligned} V_{OV} &= \sqrt{\frac{1\ \text{mA}}{20\ \text{mA/V}^2}} \\ &= 0.224\ \text{V} \end{aligned}$$
2. **At 0.1 V of input.**
   $$\begin{aligned} i_{D1} &= 0.5 + \frac{1}{0.224} \times 0.05 \\ &\quad \times \sqrt{1 - 0.05} \\ &= 0.718\ \text{mA} \end{aligned}$$
3. **At 0.4 V of input.** The input exceeds $\sqrt2V_{OV}$ = 0.316 V, so that side carries the whole 1 mA and the other is off.

**Answer:** one side carries 0.718 mA at 0.1 V of input, and the whole 1 mA at 0.4 V.
:::
:::

::: check
kind: python

## Step 1. Write `mirror_out(Iref, beta, VA, VCE2, VBE=0.7)` returning the output of a BJT mirror with base-current and Early-effect errors.

```python starter
def mirror_out(Iref, beta, VA, VCE2, VBE=0.7):
    return Iref

print(mirror_out(1e-3, 100, 100, 5))
```

```python solution
def mirror_out(Iref, beta, VA, VCE2, VBE=0.7):
    return Iref / (1 + 2 / beta) * (1 + VCE2 / VA) / (1 + VBE / VA)

print(mirror_out(1e-3, 100, 100, 5))
```

```python tests
assert abs(mirror_out(1e-3, 100, 100, 5) - 1.022256e-3) < 1e-9, f"Expected 1.0223 mA; got {mirror_out(1e-3, 100, 100, 5)}"
assert abs(mirror_out(1e-3, 1e12, 1e12, 5) - 1e-3) < 1e-12, "With ideal transistors it copies exactly."
```

::: right
Right. Both errors are a percent or two, and they don't cancel by design.
:::
:::

::: check
kind: python

## Step 2. Write `widlar(Iref, RE, VT=0.02585)` returning $I_O$ by bisection on a log scale between $10^{-15}$ A and `Iref`.

```python starter
import math

def widlar(Iref, RE, VT=0.02585):
    return VT * math.log(100) / RE

print(widlar(1e-3, 1000))
```

```python solution
import math

def widlar(Iref, RE, VT=0.02585):
    lo, hi = 1e-15, Iref
    for _ in range(200):
        m = math.sqrt(lo * hi)
        if m * RE - VT * math.log(Iref / m) > 0:
            hi = m
        else:
            lo = m
    return math.sqrt(lo * hi)

print(widlar(1e-3, 1000))
```

```python tests
import math
assert abs(widlar(1e-3, 1000) - 6.908281e-5) < 1e-10, f"1 mA and 1 kΩ: 69.08 µA; got {widlar(1e-3, 1000)}"
assert abs(widlar(1e-3, 0.02585 * math.log(100) / 1e-5) - 1e-5) < 1e-12, "Lesson 3's 11.9 kΩ gives exactly 10 µA."
assert abs(widlar(1e-4, 5000) - 1.128114e-5) < 1e-10, "0.1 mA and 5 kΩ: 11.28 µA."
```

::: right
Right. A log-scale bisection finds currents orders of magnitude below the reference in a fixed number of steps.
:::
:::

::: check
kind: python

## Step 3. Write `bjt_pair(vid, I, VT=0.02585)` returning `(ic1, ic2)`, and `vid_for(f, VT=0.02585)` returning the input that puts a fraction f of the tail on one side.

```python starter
def bjt_pair(vid, I, VT=0.02585):
    return I / 2, I / 2

def vid_for(f, VT=0.02585):
    return 0.0

print(bjt_pair(0.05, 1e-3), vid_for(0.9))
```

```python solution
import math

def bjt_pair(vid, I, VT=0.02585):
    ic1 = I / (1 + math.exp(-vid / VT))
    return ic1, I - ic1

def vid_for(f, VT=0.02585):
    return VT * math.log(f / (1 - f))

print(bjt_pair(0.05, 1e-3), vid_for(0.9))
```

```python tests
import math
a, b = bjt_pair(0.05, 1e-3)
assert abs(a - b - 1e-3 * math.tanh(0.05 / (2 * 0.02585))) < 1e-15, "The difference must be I·tanh(v/2VT)."
assert abs(vid_for(0.9) - 0.0567983) < 1e-6 and abs(vid_for(0.99) - 0.1187838) < 1e-6, f"90%: 56.8 mV; 99%: 118.8 mV; got {vid_for(0.9)}, {vid_for(0.99)}"
assert abs(bjt_pair(vid_for(0.75), 2e-3)[0] - 1.5e-3) < 1e-15, "The two functions must agree."
```

::: right
Right. Each tenfold step closer to full switching costs another $V_T\ln10$, about 60 mV.
:::
:::

::: check
kind: python

## Step 4. Write `mos_pair(vid, I, K)` returning $i_{D1}$ for a MOSFET pair, including full switching beyond $\sqrt2V_{OV}$ in either direction.

```python starter
import math

def mos_pair(vid, I, K):
    vov = math.sqrt(I / K)
    return I / 2 + I / vov * vid / 2

print(mos_pair(0.1, 1e-3, 20e-3))
```

```python solution
import math

def mos_pair(vid, I, K):
    vov = math.sqrt(I / K)
    if vid >= math.sqrt(2) * vov:
        return I
    if vid <= -math.sqrt(2) * vov:
        return 0.0
    x = vid / 2 / vov
    return I / 2 + I / vov * vid / 2 * math.sqrt(1 - x * x)

print(mos_pair(0.1, 1e-3, 20e-3))
```

```python tests
assert abs(mos_pair(0.1, 1e-3, 20e-3) - 7.179449e-4) < 1e-10, f"0.1 V: 0.718 mA; got {mos_pair(0.1, 1e-3, 20e-3)}"
assert mos_pair(0.4, 1e-3, 20e-3) == 1e-3 and mos_pair(-0.4, 1e-3, 20e-3) == 0.0, "Beyond √2·VOV one side takes it all."
assert abs(mos_pair(-0.2, 1e-3, 20e-3) - 1e-4) < 1e-12, "At −0.2 V: 0.1 mA."
```

::: right
Right. The square law makes the curve reach full switching at a finite input, unlike the exponential.
:::
:::

::: check
kind: python

## Step 5. Write `ota(I, vov, lam, CL)` returning `(Ad, GBW, SR)` for a five-transistor OTA: gain, gain-bandwidth in Hz, and slew rate in V/s.

```python starter
def ota(I, vov, lam, CL):
    return 1.0, 1.0, 1.0

print(ota(100e-6, 0.2, 0.05, 1e-12))
```

```python solution
import math

def ota(I, vov, lam, CL):
    gm1 = 2 * (I / 2) / vov
    ro = 1 / (lam * I / 2)
    return gm1 * ro / 2, gm1 / (2 * math.pi * CL), I / CL

print(ota(100e-6, 0.2, 0.05, 1e-12))
```

```python tests
Ad, GBW, SR = ota(100e-6, 0.2, 0.05, 1e-12)
assert abs(Ad - 100) < 1e-9 and abs(GBW - 79577471.5) < 1 and abs(SR - 1e8) < 1e-3, f"Lesson 7: 100, 79.6 MHz, 100 V/µs; got {Ad}, {GBW}, {SR}"
assert abs(ota(400e-6, 0.2, 0.05, 1e-12)[0] - 100) < 1e-9, "The gain doesn't depend on the tail current."
```

::: right
Right. Current buys speed; overdrive and λ set the gain.
:::
:::

::: check
given: f = 0.6 | 0.8 | 0.95 | 0.999
answer: 25.85*ln(f/(1 - f))
unit: mV
tolerance: 0.5%

## What differential input puts a fraction {f} of a bipolar pair's tail on one side?

::: right
Right. $V_T\ln\frac{f}{1 - f}$ = {answer}.
:::

::: misses
- [25.85*log(f/(1 - f))] Use the natural logarithm.
- [2*25.85*ln(f/(1 - f))] $i_{C1} = \frac{I}{1 + e^{-v_{id}/V_T}}$, with $V_T$, not $2V_T$.
- [any] Solve $\frac{1}{1 + e^{-v_{id}/V_T}} = f$.
:::
:::

::: check
given: Iref = 0.5 | 1 | 2; Io = 5 | 20 | 100
answer: 25.85*ln(Iref*1000/Io)/Io
unit: kΩ
tolerance: 0.5%

## What emitter resistor makes a Widlar source deliver {Io} µA from a {Iref} mA reference?

::: right
Right. $R_E = \frac{V_T}{I_O}\ln\frac{I_\text{ref}}{I_O}$ = {answer}.
:::

::: misses
- [any] $I_OR_E = V_T\ln\frac{I_\text{ref}}{I_O}$.
:::
:::

::: explain
## Why does the Widlar equation need a numerical solution when you know $R_E$ but not when you know $I_O$?

::: model
The equation sets the resistor's voltage, the output current times the resistance, equal to the thermal voltage times the logarithm of the reference over the output current. If you know the output current, the right side is just a number and dividing by the current gives the resistor. If you know the resistor, the unknown current appears both outside a logarithm and inside it, and no algebra isolates it. But the left side grows steadily with the current while the right side falls, so they cross exactly once, and bisection can close in on that crossing to any precision.
:::
:::

::: practice

::: check
given: Iref = 0.2 | 3; Io = 2 | 50
answer: 25.85*ln(Iref*1000/Io)/Io
unit: kΩ
tolerance: 0.5%

## Widlar: {Iref} mA in, {Io} µA out. Emitter resistor?

::: misses
- [any] $\frac{V_T}{I_O}\ln\frac{I_\text{ref}}{I_O}$.
:::
:::
:::
