Unit C - Actuators & Displays

12. Transistors, MOSFETs & Relays

Switch loads safely without overloading an Arduino pin.

Estimated time 4 hours

Learning outcomes

  • Explain why loads need drivers
  • Compare an NPN low-side switch with a logic-level N-MOSFET
  • Wire a logic-level N-MOSFET low-side switch with gate resistors
  • Place a flyback diode correctly across an inductive load
  • Describe relay coil versus contacts and keep to ELV DC only

Parts and preparation

Uno, logic-level N-MOSFET, 220 ohm gate resistor, 10 k ohm gate pull-down, diode, low-voltage DC fan or lamp and external supply. Optional: NPN (e.g. 2N2222), base resistor and 5 V relay module for comparison.

Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.

Transistors, MOSFETs & Relays instructional connection diagram

Why loads need a driver

An Arduino Uno I/O pin is a logic control line, typically safe around 20 mA continuous. Fans, lamps, motors and relay coils often need far more current, and motors/relays are inductive.

A driver (NPN transistor, MOSFET, or a module) lets the pin switch a small control signal while an external supply provides the load current. Always join the external supply GND to the Uno GND so the control voltage has a common reference.

Unsafe direct drive of a fan from an Arduino pin contrasted with a safe driver path where the pin controls a driver and an external supply powers the load
Pins control. External supplies power. Share GND between Uno and the load supply.

NPN transistor as a low-side switch

An NPN bipolar transistor can switch a load on the low side: load from external +V to the collector, emitter to GND. A base resistor (often about 1 k ohm) limits base current from the Arduino pin.

When the pin is HIGH, base current turns the NPN on and collector current flows through the load. When the pin is LOW, the transistor is off. NPNs are current-driven: you must budget base current (roughly collector current divided by beta, with a safety margin). They suit modest loads such as relay coils, buzzers and small lamps.

Schematic of NPN low-side switch with base resistor from Uno pin, load from external positive to collector, emitter to common ground, and flyback diode across the load
NPN low-side switch: Rb into the base, load above the collector, common GND, flyback on inductive loads.
TerminalConnects to
Collector (C)Load return (switched node)
Base (B)Arduino pin via Rb
Emitter (E)Common GND
Load +External +V (not the Uno pin)

Logic-level N-channel MOSFET

An N-channel enhancement MOSFET is also used as a low-side switch: load from +V to drain, source to GND. The gate is driven by voltage, so steady gate current is tiny compared with an NPN base.

Logic-level means the datasheet shows a useful low RDS(on) at about 4.5 V gate (or lower) - not merely that the threshold voltage Vth is under 5 V. A non-logic-level part may stay partly on, run hot, and fail.

Course gate network: series ~220 ohm from the pin to the gate (limits peak charge current), and ~10 k ohm from gate to GND (keeps the MOSFET off if the pin floats). This lesson's practical build uses the MOSFET with PWM on D9.

Schematic of logic-level N-MOSFET low-side switch with 220 ohm series gate resistor, 10k pull-down, load from external positive to drain, source to ground, and flyback diode
Logic-level N-MOSFET: 220 ohm series, 10 k ohm pull-down, load on the drain, source to common GND.
ItemCourse practice
DeviceLogic-level N-ch MOSFET (check datasheet)
Series gate RAbout 220 ohm
Gate pull-downAbout 10 k ohm to GND
ControldigitalWrite or analogWrite (PWM)

NPN vs N-MOSFET

Both can switch a load to GND. Prefer an NPN for simple modest DC loads and many relay-coil drivers. Prefer a logic-level N-MOSFET when you want efficient PWM into a fan or lamp and the part is rated for the current.

Always check package pin order - TO-92, TO-220 and SMD packages are not interchangeable by guesswork.

Comparison cards summarising NPN BJT as current-driven for modest loads versus logic-level N-MOSFET as voltage-driven with low RDS(on) for PWM loads
NPN: base current, modest loads. Logic-level MOSFET: gate voltage, better for many PWM jobs when rated.

Suggested parts (South Africa)

These are practical examples often stocked by South African distributors such as Mantech Electronics (mantech.co.za). Search the part number on the site and always confirm the datasheet, pinout and current rating for your load. Stock changes - treat this as a shopping starting point, not a guarantee.

TypePackageExample partTypical course use
NPNSMD (SOT-23)MMBT2222A / MMBT3904Small PCB loads, LED strips (check Ic)
NPNTO-92BC337 / 2N2222ABreadboard: relays, buzzers, small lamps
NPN powerTO-220TIP31C (or TIP120 Darlington)Higher current DC switching (watch heat / VCE sat)
N-MOSFET logicSMD (SOT-23)2N7002Small PWM / signal switching only
N-MOSFET logicTO-922N7000Breadboard demos; low current loads
N-MOSFET logic powerTO-220IRLZ44NFans / lamps with PWM (course-style low-side)

Flyback diode

Inductive loads (motors, relay coils, solenoids) store energy in a magnetic field. When you switch the current off, that energy tries to keep the current flowing and can produce a high voltage spike that damages the transistor, MOSFET or nearby electronics.

Place a diode reverse-biased across the coil during normal on-state: cathode toward the positive supply, anode toward the switched (transistor/MOSFET) side. When the switch opens, the diode gives the current a safe path.

Without a flyback diode an inductive spike appears when the switch opens; with a reverse-biased diode across the coil the current recirculates safely
Cathode to +V, anode to the switched node. Omit only for non-inductive loads such as a pure resistive lamp.

Relays and module limits

A relay uses a coil (inductive) to move mechanical contacts. The coil side and the contact side can be electrically isolated, which is useful for separating circuits - but contact voltage and current ratings still apply.

Many Arduino relay modules include their own driver transistor and flyback diode; you drive an IN pin and supply VCC/GND. This course switches only extra-low-voltage DC loads. Do not wire mains electricity. If you ever drive a bare relay coil from a discrete NPN, treat the coil as an inductive load with a flyback diode.

Relay module control side with Uno pin into IN and coil, isolation gap, and contact side switching an extra-low-voltage DC load with a warning never to wire mains
Coil/control side vs contact side. Isolation helps, but ratings and ELV-only rules still apply.

Wiring and safe build sequence

Breadboard wiring for lesson 12: Transistors, MOSFETs & Relays
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. D9 -> 220 ohm -> MOSFET gate
  2. 10 k ohm from gate -> GND
  3. MOSFET source -> common GND
  4. Load between external +V and drain
  5. Diode across inductive load (cathode to +V)
  6. External supply GND -> Uno GND
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
const byte loadPin = 9;

void setup() {
  pinMode(loadPin, OUTPUT);
}

void loop() {
  for (int power = 0; power <= 255; power++) {
    analogWrite(loadPin, power);
    delay(10);
  }
  analogWrite(loadPin, 0);
  delay(1000);
}

How the code works

  1. The Arduino drives only the gate; the external supply provides load current.
  2. PWM can control a DC fan or lamp when the driver and load support it.
  3. If you substitute an NPN for on/off tests, use digitalWrite and a suitable base resistor - do not assume the same PWM thermal behaviour.

Test and record evidence

Expected result: The low-voltage load increases in power, switches off, then repeats.

Practical evidence checklist

Common faults and checks
  • Confirm MOSFET pin order from its datasheet.
  • A non-logic-level MOSFET may overheat at 5 V gate drive.
  • Common GND is essential.
  • Missing flyback diode on motors/relays can cause random resets or damaged drivers.
Extension challenge: Add a thermistor threshold to control the driven fan, with hysteresis to prevent rapid switching.

Check your understanding

Q1. Why not power a fan from an I/O pin?

Show answer

Its current and inductive behavior exceed the pin's safe capability.

Q2. What makes a MOSFET 'logic-level' for 5 V Arduino use?

Show answer

Useful low RDS(on) at about 4.5 V gate (or lower), not only a low Vth.

Q3. How is a flyback diode oriented?

Show answer

Reverse-biased during normal operation: cathode to positive, anode to the switched side.

Q4. What must you never switch with a course relay build?

Show answer

Mains electricity - use extra-low-voltage DC only.