Skip to content

Commit 04efb4d

Browse files
Merge pull request #376 from jClugstor/add_NMOS_PMOS
Add NMOS PMOS Transistors
2 parents 83ad67a + bd3446f commit 04efb4d

File tree

5 files changed

+417
-1
lines changed

5 files changed

+417
-1
lines changed

docs/src/API/electrical.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ IdealOpAmp
3434
Diode
3535
HeatingDiode
3636
VariableResistor
37+
NMOS
38+
PMOS
3739
PNP
3840
NPN
3941
```
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# MOSFET I-V Curves
2+
In this example, first we'll demonstrate the I-V curves of the NMOS transistor model.
3+
First of all, we construct a circuit using the NMOS transistor. We'll need to import ModelingToolkit and the Electrical standard library that holds the transistor models.
4+
5+
```@example NMOS
6+
using ModelingToolkit
7+
using ModelingToolkit: t_nounits as t
8+
using ModelingToolkitStandardLibrary.Electrical
9+
using ModelingToolkitStandardLibrary.Blocks: Constant
10+
using OrdinaryDiffEq
11+
using Plots
12+
```
13+
14+
Here we just connect the source pin to ground, the drain pin to a voltage source named `Vcc`, and the gate pin to a voltage source named `Vb`.
15+
```@example NMOS
16+
@mtkmodel SimpleNMOSCircuit begin
17+
@components begin
18+
Q1 = NMOS()
19+
Vcc = Voltage()
20+
Vb = Voltage()
21+
ground = Ground()
22+
23+
Vcc_const = Constant(k=V_cc)
24+
Vb_const = Constant(k=V_b)
25+
end
26+
27+
@parameters begin
28+
V_cc = 5.0
29+
V_b = 3.5
30+
end
31+
@equations begin
32+
#voltage sources
33+
connect(Vcc_const.output, Vcc.V)
34+
connect(Vb_const.output, Vb.V)
35+
36+
#ground connections
37+
connect(Vcc.n, Vb.n, ground.g, Q1.s)
38+
39+
#other stuff
40+
connect(Vcc.p, Q1.d)
41+
connect(Vb.p, Q1.g)
42+
end
43+
end
44+
45+
@mtkbuild sys = SimpleNMOSCircuit(V_cc = 5.0, V_b = 3.5)
46+
47+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
48+
sol = solve(prob)
49+
```
50+
51+
Now to make sure that the transistor model is working like it's supposed to, we can examine the plots of the drain-source voltage vs. the drain current, otherwise knowns as the I-V curve of the transistor.
52+
```@example NMOS
53+
v_cc_list = collect(0.05:0.1:10.0)
54+
55+
I_D_list = []
56+
I_D_lists = []
57+
58+
for V_b in [1.0, 1.4, 1.8, 2.2, 2.6]
59+
I_D_list = []
60+
for V_cc in v_cc_list
61+
@mtkbuild sys = SimpleNMOSCircuit(V_cc=V_cc, V_b=V_b)
62+
prob = ODEProblem(sys, Pair[], (0.0, 10.0))
63+
sol = solve(prob)
64+
push!(I_D_list, sol[sys.Q1.d.i][1])
65+
end
66+
push!(I_D_lists, I_D_list)
67+
end
68+
69+
reduce(hcat, I_D_lists)
70+
plot(v_cc_list, I_D_lists, title="NMOS IV Curves", label=["V_GS: 1.0 V" "V_GS: 1.4 V" "V_GS: 1.8 V" "V_GS: 2.2 V" "V_GS: 2.6 V"], xlabel = "Drain-Source Voltage (V)", ylabel = "Drain Current (A)")
71+
```
72+
73+
We can see that we get exactly what we would expect: as the drain-source voltage increases, the drain current increases, until the the transistor gets in to the saturation region of operation.
74+
Then the only increase in drain current is due to the channel-length modulation effect. Additionally, we can see that the maximum current reached increases as the gate voltage increases.

src/Electrical/Analog/mosfets.jl

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
"""
2+
NMOS(;name, V_tn, R_DS, lambda)
3+
4+
Creates an N-type MOSFET transistor
5+
6+
# Structural Parameters
7+
- `use_transconductance`: If `true` the parameter `k_n` needs to be provided, and is used in the calculation of the current
8+
through the transistor. Otherwise, `mu_n`, `C_ox`, `W`, and `L` need to be provided and are used to calculate the transconductance.
9+
10+
- `use_channel_length_modulation`: If `true` the channel length modulation effect is taken in to account. In essence this gives
11+
the drain-source current has a small dependency on the drains-source voltage in the saturation region of operation.
12+
13+
# Connectors
14+
- `d` Drain Pin
15+
- `g` Gate Pin
16+
- `s` Source Pin
17+
18+
# Parameters
19+
- `mu_n`: Electron mobility
20+
- `C_ox`: Oxide capacitance (F/m^2)
21+
- `W`: Channel width (m)
22+
- `L`: Channel length
23+
- `k_n`: MOSFET transconductance parameter
24+
25+
Based on the MOSFET models in (Sedra, A. S., Smith, K. C., Carusone, T. C., & Gaudet, V. C. (2021). Microelectronic circuits (8th ed.). Oxford University Press.)
26+
"""
27+
@mtkmodel NMOS begin
28+
@variables begin
29+
V_GS(t)
30+
V_DS(t)
31+
V_OV(t)
32+
end
33+
34+
@components begin
35+
d = Pin()
36+
g = Pin()
37+
s = Pin()
38+
end
39+
40+
@parameters begin
41+
V_tn = 0.8, [description = "Threshold voltage (V)"]
42+
R_DS = 1e7, [description = "Drain to source resistance (Ω)"]
43+
44+
lambda = 0.04, [description = "Channel length modulation coefficient (V^(-1))"]
45+
46+
if !use_transconductance
47+
mu_n, [description = "Electron mobility"]
48+
C_ox, [description = "Oxide capacitance (F/m^2)"]
49+
W, [description = "Channel width (m)"]
50+
L, [description = "Channel length (m)"]
51+
else
52+
k_n = 20e-3, [description = "MOSFET transconductance parameter"]
53+
end
54+
end
55+
56+
@structural_parameters begin
57+
use_transconductance = true
58+
end
59+
60+
begin
61+
if !use_transconductance
62+
k_n = mu_n * C_ox * (W / L)
63+
end
64+
end
65+
66+
@equations begin
67+
V_DS ~ ifelse(d.v < s.v, s.v - d.v, d.v - s.v)
68+
V_GS ~ g.v - ifelse(d.v < s.v, d.v, s.v)
69+
V_OV ~ V_GS - V_tn
70+
71+
d.i ~ ifelse(d.v < s.v, -1, 1) * ifelse(V_GS < V_tn,
72+
V_DS / R_DS,
73+
ifelse(V_DS < V_OV,
74+
k_n * (1 + lambda * V_DS) * (V_OV - V_DS / 2) * V_DS + V_DS / R_DS,
75+
((k_n * V_OV^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS
76+
)
77+
)
78+
79+
g.i ~ 0
80+
s.i ~ -d.i
81+
end
82+
end
83+
84+
85+
86+
"""
87+
PMOS(;name, V_tp, R_DS, lambda)
88+
89+
Creates an N-type MOSFET transistor
90+
91+
# Structural Parameters
92+
- `use_transconductance`: If `true` the parameter `k_p` needs to be provided, and is used in the calculation of the current
93+
through the transistor. Otherwise, `mu_n`, `C_ox`, `W`, and `L` need to be provided and are used to calculate the transconductance.
94+
95+
- `use_channel_length_modulation`: If `true` the channel length modulation effect is taken in to account. In essence this gives
96+
the drain-source current has a small dependency on the drains-source voltage in the saturation region of operation.
97+
98+
# Connectors
99+
- `d` Drain Pin
100+
- `g` Gate Pin
101+
- `s` Source Pin
102+
103+
# Parameters
104+
- `mu_p`: Electron mobility
105+
- `C_ox`: Oxide capacitance (F/m^2)
106+
- `W`: Channel width (m)
107+
- `L`: Channel length
108+
- `k_p`: MOSFET transconductance parameter
109+
110+
Based on the MOSFET models in (Sedra, A. S., Smith, K. C., Carusone, T. C., & Gaudet, V. C. (2021). Microelectronic circuits (8th ed.). Oxford University Press.)
111+
"""
112+
@mtkmodel PMOS begin
113+
@variables begin
114+
V_GS(t)
115+
V_DS(t)
116+
end
117+
118+
@components begin
119+
d = Pin()
120+
g = Pin()
121+
s = Pin()
122+
end
123+
124+
@parameters begin
125+
V_tp = -1.5, [description = "Threshold voltage (V)"]
126+
R_DS = 1e7, [description = "Drain-source resistance (Ω)"]
127+
128+
lambda = 1 / 25, [description = "Channel length modulation coefficient (V^(-1))"]
129+
130+
if !use_transconductance
131+
mu_p, [description = "Hole mobility"]
132+
C_ox, [description = "Oxide capacitance (F/m^2)"]
133+
W, [description = "Channel width (m)"]
134+
L, [description = "Channel length (m)"]
135+
else
136+
k_p = 20e-3
137+
end
138+
end
139+
140+
@structural_parameters begin
141+
use_transconductance = true
142+
end
143+
144+
begin
145+
if !use_transconductance
146+
k_p = mu_p * C_ox * (W / L)
147+
end
148+
end
149+
150+
@equations begin
151+
V_DS ~ ifelse(d.v > s.v, s.v - d.v, d.v - s.v)
152+
V_GS ~ g.v - ifelse(d.v > s.v, d.v, s.v)
153+
154+
d.i ~ -ifelse(d.v > s.v, -1.0, 1.0) * ifelse(V_GS > V_tp,
155+
V_DS / R_DS,
156+
ifelse(V_DS > (V_GS - V_tp),
157+
k_p * (1 + lambda * V_DS) * ((V_GS - V_tp) - V_DS / 2) * V_DS +
158+
V_DS / R_DS,
159+
((k_p * (V_GS - V_tp)^2) / 2) * (1 + lambda * V_DS) + V_DS / R_DS,
160+
)
161+
)
162+
163+
g.i ~ 0
164+
s.i ~ -d.i
165+
end
166+
end

src/Electrical/Electrical.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ include("Analog/sensors.jl")
2424
export Voltage, Current
2525
include("Analog/sources.jl")
2626

27+
export NMOS, PMOS
28+
include("Analog/mosfets.jl")
29+
2730
export NPN, PNP
2831
include("Analog/transistors.jl")
2932

0 commit comments

Comments
 (0)