P3 · Diodes & Transistors · Lesson 3 of 3

Choosing + using active components

~14 min

Slide 1

When to use a transistor vs a MOSFET vs a relay

To switch a load, you have three main options: a BJT, a MOSFET, or a relay. Each suits different situations. Knowing which to reach for is part of active-component fluency.

The three switching options

BJT (bipolar transistor):

  • Switches with a small base current (needs a base resistor).
  • Good for: small loads, simple cheap circuits, learning.
  • Limits: continuous base current wastes a little power; not ideal for high currents.

MOSFET (logic-level):

  • Switches with a gate voltage (negligible steady current).
  • Good for: efficient switching of meaningful currents (motors, high-power LEDs, heaters).
  • Limits: must be logic-level to drive from a 3.3V/5V pin; gate needs protection for robustness.

Relay (electromechanical):

  • A physical switch operated by a coil — provides full electrical isolation between control and load.
  • Good for: switching AC mains, high-voltage or high-current loads, when you need galvanic isolation, when the load type is unknown/varied.
  • Limits: slow, wears out (mechanical contacts), the coil itself needs a transistor + flyback diode to drive, audible click, larger.

Choosing between them

SituationChoose
Small DC load (LED string, small motor)BJT or logic-level MOSFET
Larger DC load (bigger motor, heater)Logic-level MOSFET (efficiency)
AC mains load (a lamp, an appliance)Relay (isolation + AC handling)
Need electrical isolationRelay (or opto-isolated driver)
High-speed switching (PWM a motor)MOSFET (relays can't switch fast)
Switching an unknown/varied loadRelay (handles AC/DC, resistive/inductive)

A useful mental model:

  • Low-power DC switching → transistor (BJT or MOSFET).
  • Mains / isolation / "just connect these contacts" → relay.
  • Efficiency + speed at meaningful DC current → MOSFET.

The relay needs a transistor anyway

A subtlety: a relay's coil is itself a load that a microcontroller can't drive directly (the coil draws tens to hundreds of mA — too much for a GPIO). So driving a relay means: GPIO → base resistor → transistor → relay coil (with a flyback diode across the coil).

So a relay circuit contains a transistor switch. The relay gives you isolation + AC capability + "dumb contacts"; the transistor is what lets the microcontroller drive the relay's coil. Many relay modules package the transistor + flyback diode + the relay together, so you just feed them a logic signal — but underneath, it's the same transistor switch driving the coil.

Why isolation matters (the relay's superpower)

The relay's unique advantage is galvanic isolation: the control side (your microcontroller, low voltage) is physically separate from the load side (which might be 230V AC mains). The coil and the contacts share no electrical connection — only a magnetic one. This means:

  • A fault on the mains side can't reach your microcontroller.
  • You can switch dangerous voltages safely from safe logic.
  • Different ground references on each side are fine.

For switching anything connected to AC mains (a Nigerian household appliance, a pump, a mains light), the relay's isolation is a safety necessity. You never switch mains directly with a bare transistor referenced to your logic ground.

Solid-state relays (briefly)

A middle ground: solid-state relays (SSRs) provide relay-like isolation + AC switching, but with no moving parts (using internal opto-isolation + a triac/MOSFET). Faster and longer-lived than mechanical relays, but pricier and with their own quirks (leakage, minimum load). They come up when you need relay isolation + AC switching but also speed or longevity.

The practical default

For most maker + embedded projects:

  • DC loads → logic-level MOSFET (efficient) or BJT (simple/small).
  • AC mains loads → a relay (or relay module), driven by a transistor, for isolation + safety.

The next slide covers the complete microcontroller-drives-a-load pattern — the standard circuit that ties together everything in L1 and L2.

Slide 2

The microcontroller-drives-a-load pattern

The microcontroller-drives-a-load pattern is the single most important circuit pattern in embedded electronics. Almost every project where a microcontroller controls something physical uses it. It ties together everything from L1 and L2.

The fundamental problem, restated

A microcontroller GPIO pin can source/sink only ~20mA at logic voltage. Most real loads need far more:

  • Motor: hundreds of mA to several amps.
  • Relay coil: tens to hundreds of mA.
  • Solenoid / pump: hundreds of mA to amps.
  • High-power LED / LED string: hundreds of mA.

You cannot connect these to a GPIO pin directly — you'd exceed the pin's limit and damage the microcontroller. The pattern solves this.

The pattern

The microcontroller-drives-a-load pattern:

  1. The microcontroller GPIO provides only the weak control signal.
  2. A base/gate resistor connects the pin to a transistor (BJT) or MOSFET.
  3. The transistor switches the load, drawing the load's larger current from a separate supply capable of delivering it (NOT through the microcontroller).
  4. A flyback diode across the load if it's inductive (motor, relay, solenoid).
  5. Shared ground between the microcontroller and the load supply (so the control signal is referenced correctly).

The microcontroller controls; the transistor carries the power; the separate supply provides the current. The pin never sees the load current.

Worked example: a microcontroller switching a 12V motor

You want an ESP32 (3.3V logic) to turn a 12V, 500mA motor on and off.

  1. Supply: a 12V supply for the motor (the ESP32 runs on its own 3.3V).
  2. Switch: a logic-level N-channel MOSFET (turns on fully at 3.3V gate).
  3. Wiring: motor from +12V to MOSFET drain; MOSFET source to ground; gate through a ~100Ω resistor to the ESP32 GPIO; a 10k pull-down from gate to ground.
  4. Flyback diode: across the motor (cathode to +12V) — the motor is inductive, so this is mandatory.
  5. Common ground: the ESP32 ground and the 12V supply ground are connected.

Now ESP32 GPIO HIGH → MOSFET on → motor runs (drawing 500mA from the 12V supply, not from the ESP32). GPIO LOW → MOSFET off → motor stops, with the flyback diode safely absorbing the inductive kick.

The ESP32 pin only ever supplies the tiny gate-charging current. The 500mA comes entirely from the 12V supply through the MOSFET.

Every L1 + L2 concept appears here

This one circuit uses nearly everything from the last two modules:

  • Resistors (L1): the base/gate resistor, the gate pull-down, an LED current-limit resistor if there's an indicator.
  • The transistor/MOSFET (L2): the switch itself.
  • The flyback diode (L2): protecting the switch from the inductive load.
  • Power awareness (L0/L1): the separate supply sized for the load, the common ground.
  • The GPIO limit (foreshadowing L4): why you can't drive the load directly.

If you understand this circuit fully — why each part is there, what happens if you omit one — you've integrated the core of L1 and L2.

What goes wrong if you skip a part

  • No transistor (load direct to GPIO): exceeds the pin's current limit → fried microcontroller.
  • No base resistor (BJT): base draws excess current → fried pin or transistor.
  • Non-logic-level MOSFET on a 3.3V pin: MOSFET only partially turns on → overheats, load underpowered.
  • No flyback diode (inductive load): switch-off spike → destroyed transistor.
  • No gate pull-down (MOSFET): floating gate at boot → load randomly on during startup.
  • No common ground: the control signal has no reference → unreliable or non-functional switching.

Each part of the pattern prevents a specific failure. The pattern is the accumulated wisdom of these failure modes.

The pattern scales

This same pattern, scaled up, drives motors in robots, relays in home automation, solenoids in valves, heaters in 3D printers, and the loads in countless products. The components change (bigger MOSFETs, motor-driver ICs that package several switches, gate drivers for very high power) but the principle is constant: weak logic controls a transistor that switches the real power.

The P-Check for this module is a small version of this pattern: build a transistor switch (BJT or MOSFET, with its base/gate resistor) that switches an LED (with its current-limit resistor) under control of a signal. It's the pattern in miniature — and the foundation for everything you'll build where a microcontroller controls the physical world.

The final slide covers reading the transistor/MOSFET datasheet specs you need to choose the right part for a given load.

Quick check

A microcontroller GPIO pin can typically source/sink only ~20mA. To switch a 500mA motor, you:

Slide 3

Reading transistor/MOSFET datasheets: the key specs

Before using a transistor or MOSFET, you check a few datasheet specs to confirm it can handle your load. Choosing a part that's under-rated is how transistors burn out; this slide covers the specs that matter.

The specs that matter for switching

For a BJT:

  1. Vce(max) — maximum collector-emitter voltage. Must exceed your supply voltage with margin. (Switching a 12V load → need Vce(max) comfortably above 12V; the spike from an inductive load can be higher, though the flyback diode clamps it.)

  2. Ic(max) — maximum collector current. Must exceed your load current with margin. (A 500mA load → want Ic(max) of 1A+, for headroom.)

  3. hFE (gain) — the current gain. Determines how much base current you need to switch the load (base current ≈ load current ÷ hFE). Lower gain means more base current required.

  4. Power dissipation — the transistor dissipates roughly Vce(sat) × Ic when on. Check it stays within the package's rating; high-current switching may need a heatsink.

For a MOSFET:

  1. Vds(max) — maximum drain-source voltage. Must exceed your supply with margin.

  2. Id(max) — maximum drain current. Must exceed your load current with margin.

  3. Vgs(th) and the Rds(on)-at-your-gate-voltage — the crucial logic-level check. Confirm the MOSFET turns on fully at your gate drive voltage (3.3V or 5V). The datasheet should specify Rds(on) at a Vgs of 3.3V/4.5V/5V — if it only gives Rds(on) at 10V, it's not logic-level and won't fully turn on from a logic pin.

  4. Rds(on) — the on-resistance. Lower is better (less waste heat). The MOSFET dissipates I²load × Rds(on) when on. A low Rds(on) MOSFET runs cool even at high current.

The margin principle

For every rating, pick a part rated comfortably above your actual use — typically 1.5–2× for voltage and current. Reasons:

  • Transients exceed steady values. Inrush currents, voltage spikes, and surges momentarily exceed the steady-state numbers.
  • Derating extends life. A part run near its limit runs hot and fails sooner; a part run at half its rating lasts far longer.
  • Real conditions vary. Temperature, supply variation, and load changes push the actual stress above your nominal calculation.

Running a transistor right at its rated maximum is asking for a failure. Always leave headroom.

A worked selection

You want to switch a 12V, 1A motor from a 5V Arduino pin.

  • Supply: 12V. So Vds(max) needs to be comfortably above 12V → look for ≥30V (lots of margin, cheap).
  • Load current: 1A. So Id(max) needs to be comfortably above 1A → look for ≥5A (the motor's stall/inrush current can be several times its running current — motors especially need margin here).
  • Logic level: 5V gate drive. So need a logic-level MOSFET fully on at 5V (check Rds(on) at Vgs=4.5V or 5V).
  • Rds(on): low, so it runs cool. At 1A, an Rds(on) of 0.05Ω dissipates only I²R = 1² × 0.05 = 0.05W — negligible.

A part like the IRLZ44N (logic-level, 55V, 47A, low Rds(on)) is massive overkill in the good way — it'll switch this motor effortlessly and run cold. Overkill on a cheap MOSFET is fine; under-rating is what burns parts.

Don't forget the supporting components

Choosing the transistor is only part of it. The full switch needs:

  • The base/gate resistor (sized for the transistor type).
  • The gate pull-down (MOSFET, ~10k).
  • The flyback diode (inductive loads — rated for the load current).
  • The separate supply (sized for the load, with the common ground).

The transistor datasheet tells you the switch is capable; the supporting components make the circuit reliable.

The module summary

Across L2:

  • Diodes — one-way valves. Forward drop (~0.7V silicon, ~0.3V Schottky). Types: rectifier, Schottky, Zener, LED. The flyback diode protects against inductive kick — mandatory on every coil.
  • Transistors as switches — a small control input switches a large load current. NPN low-side switch (with its base resistor); MOSFETs (voltage-controlled, efficient, must be logic-level for direct GPIO drive).
  • Choosing + using — BJT vs MOSFET vs relay; the microcontroller-drives-a-load pattern; datasheet specs (voltage, current, logic-level, on-resistance) with margin.

The active components — diodes and transistors — are where electronics becomes control. A diode steers and protects; a transistor switches and amplifies. Together with the passives from L1, they're the complete toolkit for the analog and switching circuits beneath every embedded system.

With L0 (fundamentals), L1 (passives), and L2 (diodes + transistors) done, you've covered the discrete components. L3 moves up a level to digital electronics + logic — how combinations of these transistors form the logic gates that make computation possible, and the binary world the microcontroller (L4) lives in.