Transistors as Switches
Transistors let a small control signal switch a large load. An MCU GPIO pin (sourcing a few mA at 3.3V) can control a motor, relay, LED strip, or solenoid drawing amps at 12V or more.
Why It Matters
Microcontrollers cannot drive loads directly — their GPIO pins max out at 10-20mA. Every actuator, power LED, relay, and motor in an embedded system needs a transistor (or transistor-based driver IC) between the MCU and the load. Understanding switching circuits is the link between Digital Logic and the physical world.
How It Works
MOSFET Basics
A MOSFET is a voltage-controlled switch. The gate-source voltage (Vgs) controls whether current flows between drain and source.
Vgs > Vth -> ON (drain-source conducts, Rds_on typically 10-200 milliohms)
Vgs < Vth -> OFF (drain-source open, leakage only)
Vth (threshold voltage): typically 1-4V. A “logic level” MOSFET has Vth low enough to fully turn on from a 3.3V or 5V GPIO pin. Standard MOSFETs need 10V on the gate — unusable with MCUs directly.
Gate charge (Qg): the gate is a capacitor. Switching speed depends on how fast you can charge/discharge it. Higher Qg = slower switching = more transition losses.
N-Channel Low-Side Switch
The load sits between Vcc and the drain. The MOSFET connects the load to ground.
Vcc (12V)
|
[LOAD] (motor, LED strip, relay coil)
|
Drain ─┐
MOSFET
Gate ──┤ ← MCU GPIO (3.3V or 5V)
│
Source ─┘
|
GND
MCU HIGH → MOSFET ON → current flows → load active
MCU LOW → MOSFET OFF → no current → load off
Simple, reliable, works with any load voltage. The load’s ground path goes through the MOSFET.
P-Channel High-Side Switch
Source connects to Vcc. Gate must be pulled LOW (relative to source) to turn on.
Vcc (12V)
|
Source ─┐
MOSFET
Gate ──┤ ← drive circuit (NOT direct MCU if Vcc > 5V)
│
Drain ──┘
|
[LOAD]
|
GND
Advantage: the load keeps its ground reference intact (important for ground-sensitive circuits). Disadvantage: gate drive is trickier when Vcc is high — you often need a level shifter or an N-channel MOSFET to pull the P-channel gate low.
Flyback Diode for Inductive Loads
Motors, relays, and solenoids are inductors. When you switch off, the collapsing magnetic field generates a voltage spike that destroys MOSFETs.
Vcc
|
[RELAY]──┐
| [D] ← flyback diode (1N4148 or similar)
| | cathode to Vcc, anode to drain
Drain ──────┘
|
MOSFET
|
GND
Always add a flyback diode across inductive loads. A 1N4148 works for small relays; use a Schottky (1N5819) for faster clamping with motors.
H-Bridge for Motor Direction
Four transistors arranged so you can reverse current through the motor.
Vcc Vcc
| |
[Q1 P] [Q3 P]
| |
+─── MOTOR ────+
| |
[Q2 N] [Q4 N]
| |
GND GND
Q1+Q4 ON, Q2+Q3 OFF → forward
Q3+Q2 ON, Q1+Q4 OFF → reverse
ALL OFF → coast
Q2+Q4 ON → brake (shorts motor)
In practice, use an H-bridge IC (L298N, DRV8833, TB6612FNG) rather than discrete transistors. They include protection diodes, dead-time logic, and current sensing.
Logic Level MOSFETs
| Part | Type | Vds_max | Id_max | Rds_on (at Vgs=4.5V) | Vth |
|---|---|---|---|---|---|
| IRLZ44N | N-ch | 55V | 47A | 22 milliohm | 1-2V |
| IRL540N | N-ch | 100V | 36A | 44 milliohm | 1-2V |
| IRLML6344 | N-ch (SMD) | 30V | 5A | 29 milliohm | 0.8V |
| Si2301 | P-ch (SMD) | 20V | 2.8A | 100 milliohm | 0.7V |
Heat Dissipation
When a MOSFET is fully on, it dissipates:
P_dissipated = Rds_on x I²
IRLZ44N switching 5A: 0.022 x 25 = 0.55W (fine without heatsink).
IRLZ44N switching 20A: 0.022 x 400 = 8.8W (needs heatsink or forced air).
Switching losses add to this at high PWM frequencies — the MOSFET passes through its linear region during transitions, dissipating more power.
MOSFET vs BJT
| MOSFET | BJT | |
|---|---|---|
| Control | Voltage (gate) | Current (base) |
| Input impedance | Very high (gate is capacitor) | Low (base draws current) |
| Switching speed | Fast (ns) | Slower (us) |
| Power handling | Excellent (low Rds_on) | Limited |
| MCU drive | Direct (logic level types) | Needs base resistor (1k-10k) |
| Best for | Power switching, PWM | Small signal, legacy circuits |
Calculation Example
# Size a MOSFET for a 12V LED strip drawing 3A
v_load = 12.0
i_load = 3.0
# IRLZ44N: Rds_on = 0.022 ohm at Vgs = 5V
rds_on = 0.022
p_mosfet = rds_on * i_load**2 # 0.198W (no heatsink needed)
v_drop = rds_on * i_load # 0.066V (negligible)
# BJT alternative: need base resistor
# NPN 2N2222, Ic = 3A, hfe(min) = 50
hfe = 50
ib_needed = i_load / hfe # 60 mA (too much for most MCU pins!)
# This is why MOSFETs are preferred for power switching
# Gate charge time (switching speed)
qg = 48e-9 # 48 nC for IRLZ44N
i_gate_drive = 10e-3 # 10 mA from MCU pin
t_switch = qg / i_gate_drive # 4.8 us (acceptable for low-freq PWM)Related
- Voltage Current Resistance — Ohm’s law for sizing components
- Motors and Actuators — what transistors drive
- Digital Logic — logic gates are transistor pairs
- Power Supply Design — MOSFETs in switching regulators