---
title: Transistor models lab
heading: MOSFET, BJT and JFET models and their operating points, in Python
eyebrow: C5 · Lesson 27 · Transistor models lab
xp: 50
---

::: hook
Lessons 15 to 24 solved transistor circuits by hand: guess a region, solve, check the guess. A circuit simulator does the same with models that cover every region at once and a solver that never has to guess. In this lab you'll write those models for the MOSFET, the BJT (the Ebers–Moll model, which handles saturation without special cases) and the JFET, a bisection solver that finds a MOSFET's operating point in either region, and a worst-case check of a bias design.

::: play
- The curves are a MOSFET's drain current against drain voltage, at three gate voltages half a volt apart, from one model that covers cutoff, triode and saturation.
- Raise λ: the saturated parts of the curves tilt upward.
- Watch the knees: each one sits at $v_{DS} = v_{GS} - V_t$, which moves with the gate voltage.
:::
:::

```sim plot
view: graph
x: v = 0..6 V
y: i = 0..20 mA
slider: g = 1..4 step 0.1 = 3 V
slider: t = 0.4..2 step 0.1 = 1 V
slider: K = 0.5..4 step 0.1 = 2 mA/V²
slider: l = 0..0.1 step 0.005 = 0.02
plot: step(g - t)*(step(v - (g - t))*K/2*(g - t)^2 + step((g - t) - v)*K*((g - t)*v - v^2/2))*(1 + l*v) | label: middle gate voltage | samples: 400
plot: step(g + 0.5 - t)*(step(v - (g + 0.5 - t))*K/2*(g + 0.5 - t)^2 + step((g + 0.5 - t) - v)*K*((g + 0.5 - t)*v - v^2/2))*(1 + l*v) | color: ch2 | label: half a volt higher | samples: 400
plot: step(g - 0.5 - t)*(step(v - (g - 0.5 - t))*K/2*(g - 0.5 - t)^2 + step((g - 0.5 - t) - v)*K*((g - 0.5 - t)*v - v^2/2))*(1 + l*v) | color: ch3 | label: half a volt lower | samples: 400
readout: saturated current at the middle gate voltage, mA = step(g - t)*K/2*(g - t)^2 | digits: 3
```

::: predict
## The Ebers–Moll model is asked for a saturated BJT's $V_{CE}$ at a forced β of 10, with β = 100 and a reverse β of 1. What does it give?

- [x] About 0.07 V
- [ ] About 0.2 V
- [ ] About 0.5 V
- [ ] About 0.7 V

::: reveal
About 0.07 V: $V_T\ln\frac{1 + (\beta_\text{forced} + 1)/\beta_R}{1 - \beta_\text{forced}/\beta}$ = 25.85 mV × ln 13.3. The 0.2 V rule of thumb is for real parts, whose collector and emitter resistances add a drop the ideal model leaves out. The model's own number shows that a deeply saturated junction pair really can sit within a few tens of millivolts.
:::
:::

::: nameit
## One MOSFET model for every region

$$i_D = \begin{cases} 0 & v_{OV} \le 0 \\ K\left(v_{OV}v_{DS} - \tfrac12v_{DS}^2\right)(1 + \lambda v_{DS}) & 0 < v_{DS} < v_{OV} \\ \tfrac K2v_{OV}^2(1 + \lambda v_{DS}) & v_{DS} \ge v_{OV}\end{cases}$$

with $v_{OV} = v_{GS} - V_t$. Applying $(1 + \lambda v_{DS})$ in both regions keeps the current continuous at the knee, as SPICE's level-1 model does.

## Solving a bias circuit without guessing

For a MOSFET with $R_S$ and $R_D$, try a current I: it fixes $V_S = IR_S$ and $V_D = V_{DD} - IR_D$, and the model then says what current the transistor would carry at those voltages. The function

$$f(I) = i_D(V_G - IR_S,\ V_{DD} - I(R_D + R_S)) - I$$

falls steadily from positive to negative, so bisection between 0 and $\frac{V_{DD}}{R_D + R_S}$ finds the operating point in whichever region it lies.

## Ebers–Moll

The BJT as two coupled diodes:

$$i_C = I_S\left(e^{v_{BE}/V_T} - e^{v_{BC}/V_T}\right) - \frac{I_S}{\beta_R}\left(e^{v_{BC}/V_T} - 1\right),$$

$$i_B = \frac{I_S}{\beta_F}\left(e^{v_{BE}/V_T} - 1\right) + \frac{I_S}{\beta_R}\left(e^{v_{BC}/V_T} - 1\right).$$

In the active region $v_{BC}$ is negative and $i_C = \beta_Fi_B$; in saturation both junctions conduct, and solving the two equations for a forced β gives

$$V_{CE,\text{sat}} = V_T\ln\frac{1 + (\beta_\text{forced} + 1)/\beta_R}{1 - \beta_\text{forced}/\beta_F}.$$

## Worst-case bias

A bias design has to work for every part it might be built with. For divider bias, the current is lowest with the smallest β and the largest $V_{BE}$, and highest with the largest β and the smallest $V_{BE}$; checking those two corners bounds every combination in between.
:::

::: example
## Solve lesson 17's first circuit ($V_{DD}$ = 10 V, $V_G$ = 5 V, $R_S$ = 2 kΩ, $R_D$ = 3 kΩ, K = 1 mA/V², $V_t$ = 1 V) and its second ($R_S$ = 0, $R_D$ = 10 kΩ) with the bisection solver.

::: solution
1. **First circuit.** Bisection on $f(I)$ settles at 1.2192 mA, with
   $$\begin{aligned} V_S &= 2.438\ \text{V} \\ V_D &= 6.342\ \text{V} \end{aligned}$$
2. **Check its region.** The drain is above $V_G - V_t$ = 4 V, so it's saturated, matching the quadratic formula's 1.22 mA.
3. **Second circuit.** The same solver gives 0.9748 mA and $V_D$ = 0.2516 V, in triode.
4. **What did the work.** No region was guessed in either case; the model's own switch between formulas did the work.

**Answer:** the first circuit settles at 1.2192 mA in saturation ($V_D$ = 6.342 V), and the second at 0.9748 mA in triode ($V_D$ = 0.2516 V).
:::
:::

::: example
## A BJT model with $I_S = 10^{-15}$ A, $\beta_F$ = 100 and $\beta_R$ = 1 has $v_{BE}$ = 0.70 V and $v_{BC}$ = 0.60 V. Find $i_C$, $i_B$ and the forced β, and check the saturation formula.

::: solution
1. **The two exponentials.**
   $$\begin{aligned} e^{0.7/0.02585} &= 5.76 \times 10^{11} \\ e^{0.6/0.02585} &= 1.20 \times 10^{10} \end{aligned}$$
2. **Currents.** Then $i_C$ = 0.552 mA and $i_B$ = 17.8 µA.
3. **Forced β.**
   $$\frac{0.552\ \text{mA}}{17.8\ \mu\text{A}} = 31.0$$
   That's well below 100, so it's saturated.
4. **Check the saturation formula.** With β_forced = 31.0,
   $$V_T\ln\frac{1 + 32.0}{1 - 0.310} = 0.1000\ \text{V}$$
   exactly $v_{BE} - v_{BC}$. The formula is the model, solved in reverse.

**Answer:** $i_C$ = 0.552 mA and $i_B$ = 17.8 µA, a forced β of 31.0, and the saturation formula gives back exactly the 0.1000 V between $v_{BE}$ and $v_{BC}$.
:::
:::

::: example
## Lesson 30's divider bias (47 kΩ over 10 kΩ on 12 V, $R_E$ = 1 kΩ) must work for any β from 50 to 300 and any $V_{BE}$ from 0.6 to 0.8 V. What range of currents can it give?

::: solution
1. **Low corner.** With β = 50 and $V_{BE}$ = 0.8 V,
   $$\begin{aligned} I_E &= \frac{2.105 - 0.8}{1000 + 8246/51} \\ &= 1.124\ \text{mA} \end{aligned}$$
2. **High corner.** With β = 300 and 0.6 V, $I_E$ = 1.465 mA.
3. **Compare with nominal.** Nominal (β = 100, 0.7 V) is 1.299 mA, so the spread is −13.5% to +12.8%.
4. **If that's too wide.** A design that needs the current within 5% needs a larger emitter voltage, a stiffer divider, or feedback.

**Answer:** it can give anywhere from 1.124 mA to 1.465 mA, −13.5% to +12.8% around the nominal 1.299 mA.
:::
:::

::: check
kind: python

## Step 1. Write `mos_id(vgs, vds, K, Vt, lam=0.0)` returning the drain current of an NMOS in any region, as in the reading.

```python starter
def mos_id(vgs, vds, K, Vt, lam=0.0):
    return K / 2 * (vgs - Vt) ** 2

print(mos_id(3, 1, 2e-3, 1, 0.02))
```

```python solution
def mos_id(vgs, vds, K, Vt, lam=0.0):
    ov = vgs - Vt
    if ov <= 0:
        return 0.0
    if vds >= ov:
        return K / 2 * ov * ov * (1 + lam * vds)
    return K * (ov * vds - vds * vds / 2) * (1 + lam * vds)

print(mos_id(3, 1, 2e-3, 1, 0.02))
```

```python tests
assert abs(mos_id(3, 1, 2e-3, 1, 0.02) - 3.06e-3) < 1e-12, "Lesson 16: 3.06 mA in triode."
assert abs(mos_id(3, 5, 2e-3, 1, 0.02) - 4.4e-3) < 1e-12, "And 4.4 mA saturated."
assert mos_id(0.5, 5, 2e-3, 1) == 0.0, "Below threshold: cut off."
assert abs(mos_id(3, 2, 2e-3, 1) - 4e-3) < 1e-12, "At the knee, both formulas agree."
```

::: right
Right. One function, three regions, continuous at the boundaries.
:::
:::

::: check
kind: python

## Step 2. Write `mos_bias(VDD, RD, RS, VG, K, Vt)` returning `(I, VS, VD)` by bisection on $f(I)$ between 0 and $\frac{V_{DD}}{R_D + R_S}$ (take λ = 0). Your `mos_id` from step 1 is available.

```python starter
def mos_id(vgs, vds, K, Vt, lam=0.0):
    ov = vgs - Vt
    if ov <= 0:
        return 0.0
    if vds >= ov:
        return K / 2 * ov * ov * (1 + lam * vds)
    return K * (ov * vds - vds * vds / 2) * (1 + lam * vds)

def mos_bias(VDD, RD, RS, VG, K, Vt):
    I = K / 2 * (VG - Vt) ** 2
    return I, I * RS, VDD - I * RD

print(mos_bias(10, 3e3, 2e3, 5, 1e-3, 1))
```

```python solution
def mos_id(vgs, vds, K, Vt, lam=0.0):
    ov = vgs - Vt
    if ov <= 0:
        return 0.0
    if vds >= ov:
        return K / 2 * ov * ov * (1 + lam * vds)
    return K * (ov * vds - vds * vds / 2) * (1 + lam * vds)

def mos_bias(VDD, RD, RS, VG, K, Vt):
    lo, hi = 0.0, VDD / (RD + RS)
    for _ in range(100):
        I = (lo + hi) / 2
        if mos_id(VG - I * RS, VDD - I * (RD + RS), K, Vt) - I > 0:
            lo = I
        else:
            hi = I
    I = (lo + hi) / 2
    return I, I * RS, VDD - I * RD

print(mos_bias(10, 3e3, 2e3, 5, 1e-3, 1))
```

```python tests
I, VS, VD = mos_bias(10, 3e3, 2e3, 5, 1e-3, 1)
assert abs(I - 1.2192e-3) < 1e-7 and abs(VD - 6.34233) < 1e-4, f"Saturated: 1.2192 mA, VD = 6.342 V; got {I}, {VD}"
I2, VS2, VD2 = mos_bias(10, 10e3, 0, 5, 1e-3, 1)
assert abs(VD2 - 0.25162) < 1e-4, f"Triode: VD = 0.2516 V; got {VD2}"
```

::: right
Right. The solver doesn't care which region it lands in.
:::
:::

::: check
kind: python

## Step 3. Write `ebers_moll(vbe, vbc, IS=1e-15, bf=100, br=1, VT=0.02585)` returning `(ic, ib)`, and `vce_sat(beta_forced, bf=100, br=1, VT=0.02585)` from the saturation formula.

```python starter
import math

def ebers_moll(vbe, vbc, IS=1e-15, bf=100, br=1, VT=0.02585):
    ic = IS * math.exp(vbe / VT)
    return ic, ic / bf

def vce_sat(beta_forced, bf=100, br=1, VT=0.02585):
    return 0.2

print(ebers_moll(0.7, 0.6), vce_sat(10))
```

```python solution
import math

def ebers_moll(vbe, vbc, IS=1e-15, bf=100, br=1, VT=0.02585):
    a, b = math.exp(vbe / VT), math.exp(vbc / VT)
    ic = IS * (a - b) - IS / br * (b - 1)
    ib = IS / bf * (a - 1) + IS / br * (b - 1)
    return ic, ib

def vce_sat(beta_forced, bf=100, br=1, VT=0.02585):
    return VT * math.log((1 + (beta_forced + 1) / br) / (1 - beta_forced / bf))

print(ebers_moll(0.7, 0.6), vce_sat(10))
```

```python tests
ic, ib = ebers_moll(0.7, 0.6)
assert abs(ic - 5.518959e-4) < 1e-9 and abs(ib - 1.779155e-5) < 1e-11, f"Expected 0.552 mA and 17.8 µA; got {ic}, {ib}"
assert abs(vce_sat(ic / ib) - 0.1) < 1e-9, "The formula must return the 0.1 V the junctions were given."
ic2, ib2 = ebers_moll(0.65, -2.0)
assert abs(ic2 / ib2 - 100) < 1e-6, "Active region: ic = βF·ib."
assert abs(vce_sat(10) - 0.06697) < 1e-4, "Forced β of 10: about 0.067 V."
```

::: right
Right. One model gives the active region, saturation, and the saturation voltage.
:::
:::

::: check
kind: python

## Step 4. Write `jfet_selfbias(Idss, Vp, Rs)` returning `(ID, VGS)` for a self-biased n-channel JFET: the root of Shockley's equation with $V_{GS} = -I_DR_S$ that keeps $V_{GS}$ between $V_P$ and 0.

```python starter
def jfet_selfbias(Idss, Vp, Rs):
    return Idss, 0.0

print(jfet_selfbias(8e-3, -2, 250))
```

```python solution
import math

def jfet_selfbias(Idss, Vp, Rs):
    a = Idss * Rs * Rs / (Vp * Vp)
    b = -(1 + 2 * Idss * Rs / (-Vp))
    c = Idss
    ID = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a)
    return ID, -ID * Rs

print(jfet_selfbias(8e-3, -2, 250))
```

```python tests
ID, VGS = jfet_selfbias(8e-3, -2, 250)
assert abs(ID - 3.055728e-3) < 1e-9 and abs(VGS + 0.763932) < 1e-6, f"Lesson 20: 3.056 mA at −0.764 V; got {ID}, {VGS}"
ID2, VGS2 = jfet_selfbias(12e-3, -4, 1000)
assert -4 < VGS2 < 0 and abs(ID2 - 12e-3 * (1 - VGS2 / -4) ** 2) < 1e-12, "The result must satisfy Shockley's equation inside the valid range."
```

::: right
Right. The other root lies beyond pinch-off, where Shockley's equation no longer describes the device.
:::
:::

::: check
kind: python

## Step 5. Write `bias_corners(VCC, R1, R2, RE, bmin, bmax, vmin=0.6, vmax=0.8)` returning `(Imin, Imax)`, the emitter currents of a divider-biased stage at its two worst-case corners.

```python starter
def bias_corners(VCC, R1, R2, RE, bmin, bmax, vmin=0.6, vmax=0.8):
    VTH = VCC * R2 / (R1 + R2)
    I = (VTH - 0.7) / RE
    return I, I

print(bias_corners(12, 47e3, 10e3, 1e3, 50, 300))
```

```python solution
def bias_corners(VCC, R1, R2, RE, bmin, bmax, vmin=0.6, vmax=0.8):
    VTH = VCC * R2 / (R1 + R2)
    RTH = R1 * R2 / (R1 + R2)
    I = lambda b, v: (VTH - v) / (RE + RTH / (b + 1))
    return I(bmin, vmax), I(bmax, vmin)

print(bias_corners(12, 47e3, 10e3, 1e3, 50, 300))
```

```python tests
lo, hi = bias_corners(12, 47e3, 10e3, 1e3, 50, 300)
assert abs(lo - 1.123601e-3) < 1e-8 and abs(hi - 1.465127e-3) < 1e-8, f"Expected 1.124 and 1.465 mA; got {lo}, {hi}"
lo2, hi2 = bias_corners(12, 470e3, 100e3, 1e3, 50, 300)
assert hi2 / lo2 > hi / lo, "A ten-times-higher divider spreads the current more."
```

::: right
Right. Two corners bound every combination, because the current moves one way with β and the other with $V_{BE}$.
:::
:::

::: check
given: bf = 3 | 10 | 20; b = 100 | 200; br = 0.5 | 2
answer: 25.85*ln((1 + (bf + 1)/br)/(1 - bf/b))
unit: mV
tolerance: 0.5%

## What saturation voltage does the Ebers–Moll model give at a forced β of {bf}, with $\beta_F$ = {b} and $\beta_R$ = {br}?

::: right
Right. $V_T\ln\frac{1 + (\beta_\text{forced} + 1)/\beta_R}{1 - \beta_\text{forced}/\beta_F}$ = {answer}.
:::

::: misses
- [25.85*log((1 + (bf + 1)/br)/(1 - bf/b))] Use the natural logarithm.
- [any] $V_T\ln\frac{1 + (\beta_\text{forced} + 1)/\beta_R}{1 - \beta_\text{forced}/\beta_F}$.
:::
:::

::: check
kind: parts
given: VCC = 9 | 12; R1 = 39 | 68; R2 = 10 | 15; RE = 1 | 2.2; bmin = 40 | 80

## Divider bias of {R1} kΩ over {R2} kΩ on {VCC} V with $R_E$ = {RE} kΩ must work for β from {bmin} to 400 and $V_{BE}$ from 0.6 to 0.8 V.

::: part
answer: (VCC*R2/(R1 + R2) - 0.8)/(RE + R1*R2/(R1 + R2)/(bmin + 1))
unit: mA
tolerance: 0.5%

### What's the lowest emitter current?

::: misses
- [(VCC*R2/(R1 + R2) - 0.6)/(RE + R1*R2/(R1 + R2)/(bmin + 1))] The lowest current comes with the largest $V_{BE}$.
- [any] Smallest β with the largest $V_{BE}$.
:::
:::

::: part
answer: (VCC*R2/(R1 + R2) - 0.6)/(RE + R1*R2/(R1 + R2)/401)
unit: mA
tolerance: 0.5%

### What's the highest?

::: misses
- [any] Largest β with the smallest $V_{BE}$.
:::
:::
:::

::: explain
## Why does a circuit simulator use models that cover every region, instead of the hand method of guessing a region and checking it?

::: model
The hand method works for one transistor, but with many transistors the number of region combinations to guess grows quickly, and a wrong guess has to be found and undone. A model that gives the current in every region with one continuous function lets the solver simply find where all the equations agree, by bisection for one unknown or Newton's method for many, without ever deciding the regions in advance. The region falls out of the answer. Continuity at the region boundaries matters too, because a solver can stall or jump if the function it's solving has a step in it.
:::
:::

::: practice

::: check
given: bf = 5 | 15; b = 80 | 150; br = 1 | 3
answer: 25.85*ln((1 + (bf + 1)/br)/(1 - bf/b))
unit: mV
tolerance: 0.5%

## Ebers–Moll saturation voltage: forced β {bf}, $\beta_F$ = {b}, $\beta_R$ = {br}?

::: misses
- [any] $V_T\ln\frac{1 + (\beta_\text{forced} + 1)/\beta_R}{1 - \beta_\text{forced}/\beta_F}$.
:::
:::
:::
