MOSFET
A MOSFET is a switch with an insulated gate: put a voltage on the gate and a channel opens between the drain and the source. It costs $0.60 and draws essentially no gate current, which makes it the natural upgrade from a BJT when you want a load switched efficiently, or several of them switched at once. The catch is the gate is a capacitor: it holds whatever charge it was given, so it needs a pull-down resistor to stay off while the Pi boots, and it needs enough gate voltage to turn on properly — that is what "logic-level" means.
Pins
| Pin | What it is | Type |
|---|---|---|
| D | Drain (on the tab) | passive |
| G | Gate | passive |
| S | Source | passive |
| Setting | Type | Range | Default |
|---|---|---|---|
model | enum | 2N7000, BSS138, IRLZ44N, AO3401 | 2N7000 |
MOSFET("2N7000")— MOSFET — $0.60- Pins: 3
- Aliases:
drain→D,gate→G,source→S
G–D–S face the flat side, and the drain is on the metal tab. The Part number sets the threshold and on-resistance: 2N7000 and BSS138 are small TO-92 parts, IRLZ44N is a logic-level power part, and AO3401 is a P-channel for high-side switching. From a 3.3 V GPIO a logic-level part is partly on; a non-logic-level one never really turns on, which is the lesson the simulator reproduces.
Wiring it in circuit.py
An N-channel MOSFET switching an LED from 3V3:
from wiring import *
pi = Pi()
m1 = MOSFET("2N7000")
r1 = Resistor(10_000) # keeps the gate low while the pin boots
r2 = Resistor(330)
led = LED("red")
pi.GPIO17 >> m1.gate
m1.gate >> r1 >> pi.GND # pull-down: the gate must never float
pi.V3_3 >> r2 >> led.anode
led.cathode >> m1.drain # the MOSFET sinks the LED current
m1.source >> pi.GNDThe gate takes no steady current, so r1 does not set a gate current the way a base resistor does — its job is to define the gate voltage when nothing else does. Touching a floating gate (or its wire) is how real MOSFETs die of static: the simulator's pull-down is the habit worth copying.
Driving it in main.py
from gpiozero import OutputDevice
from time import sleep
switch = OutputDevice(17) # gate: high = on
while True:
switch.on()
sleep(0.5)
switch.off()
sleep(0.5)Electrical notes
Vgs(th)is where conduction starts (2.1 V for the 2N7000).Rds(on)is quoted at 4.5 V or 10 V — at 3.3 V the channel is only partly open, so the part runs warmer than the datasheet suggests.- The level-1 square-law model in the simulator uses each part's threshold and on-resistance. Reverse conduction and the body diode are not modelled yet, so drain-below-source reads as off.
- P-channel parts switch the high side: source to the positive rail, gate pulled low to turn on.
- Over its current, power or voltage rating the part warns, and with the feed's On overload set to
burn outit fails open for the rest of the run.
Simulator vs real hardware
- A real 2N7000 in a TO-92 has pins S–G–D, not G–D–S: wire by name and check your part's datasheet.
- Static electricity destroys real gates in a way the simulator does not model; the pull-down is still the right habit.
- Switching speed is not modelled (no gate charge), so the simulator will happily switch a MOSFET faster than a real one through a weak GPIO.