Module 6 - Drivers & Actuators

16. Driving Loads: Transistors, MOSFETs & Relays

Apply transistor and MOSFET switching to Uno outputs: size the drive, protect against inductive spikes, and verify the switch with meter and oscilloscope.

Estimated time 1 session + optional extrasIn class Week 6 · Session 26

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 (IRLZ44N), 220 ohm gate resistor, 10 k ohm gate pull-down, 1N4007 (or 1N5819) flyback diode, 5-12 V DC fan or lamp and a matching 5-12 V 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.

Driving Loads: Transistors, MOSFETs & Relays instructional connection diagram

Why loads need a driver

Your electronics modules covered how transistors and MOSFETs work. This lesson is about applying them to a microcontroller output. 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.

A fan wired straight from Uno pin D9 wants about 200 mA, ten times the pin's safe 20 mA. Through a driver, D9 only sends a tiny current to a MOSFET gate, the fan's current flows in its own loop from a 12 V supply, and the supply GND is joined to the Uno GND.
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.

Two coil and MOSFET circuits at switch-off. Without a diode the drain voltage spikes far above +V when the switch opens; with a diode across the coil (band to +V, anode to the drain) the current loops through the diode and the drain only reaches about +V plus 0.7 V. A 1N4007 drawing shows the band marks the cathode.
Cathode to +V, anode to the switched node. Omit only for non-inductive loads such as a pure resistive lamp.

Verify the switch with meter and scope

With the load on at full power, measure VDS across the MOSFET with the multimeter. A logic-level part that is fully on shows a small voltage - ID × RDS(on), usually well under 0.2 V for a small fan. A reading of a volt or more means the gate is not fully driven and the part will run hot.

On the scope, put channel 1 on the gate and channel 2 on the drain while PWM runs. The drain is inverted relative to the gate: LOW when the MOSFET is on. With the flyback diode fitted, each turn-off is clamped to about one diode drop above +V - capture this trace for your logbook.

Keep the diode fitted at all times on your own build. The 'no diode' spike is a facilitator demo only: it can be tens of volts and can damage the MOSFET or the scope input. Watch the demo and sketch that trace beside yours.

MeasurementHealthy resultIf not
Gate voltage (pin HIGH)About 5 VCheck the 220 Ω series resistor and the pin
VDS, load fully onWell under 0.2 VNon-logic-level part or poor gate drive
Drain at turn-off, inductive loadClamped near +V + 0.7 VFlyback diode missing or reversed
MOSFET body after 1 minuteBarely warmToo much current or not fully switched on

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.

A relay across two separate circuits: Uno D7 drives the module's transistor and the coil on the control side; the COM and NO contacts switch a lamp from its own supply on the switched side. No wire crosses the isolation gap - only the coil's magnetism moves the contact arm. The contacts could switch higher current, voltage or AC up to their rating, but this course uses extra-low-voltage DC only and never mains.
Only magnetism crosses the gap, so the Uno is isolated from the switched circuit. Contact ratings and the ELV-only rule still apply.

Wiring and safe build sequence

Breadboard wiring for 16. Driving Loads: Transistors, MOSFETs & Relays
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. IRLZ44N pins, looking at the printed front with the legs down: G (gate) left, D (drain) middle, S (source) right - the metal tab is also D
  2. D9 -> 220 ohm -> MOSFET gate
  3. 10 k ohm from gate -> GND
  4. MOSFET source -> common GND
  5. Load between external +V (5-12 V, to suit the fan) and drain
  6. 1N4007 or 1N5819 across the fan or any inductive load: cathode (band) to +V, anode to drain
  7. 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.

Worked sketch

Download .ino
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.
Challenge (extension - optional): Add a potentiometer on A0 (as in Lesson 14) and switch the fan on above a threshold, with hysteresis to prevent rapid switching (for example on above 600, off below 500).

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.

Q5. VDS reads 1.5 V with the fan fully on. What does that suggest?

Show answer

The MOSFET is not fully switched on - not logic-level or poorly driven - and will overheat.

Code Along 19: Switching a Fan (MOSFET Driver)

Optional: practise in your browser

Write and test this lesson's code on a virtual Uno. Work through the 4 exercises in order. Press Test and each one checks itself. This practice is optional - do it if you have time.

Exercises passed 0 / 4About 30-45 min, optionalOpen as its own page

Optional practice: a fan on the virtual Uno

This practice is optional. Do it if you have time. Nothing in class depends on it.

The virtual board has a DC fan switched by a logic-level MOSFET on D9, wired like the lesson. The pin only drives the MOSFET's gate - the fan's current comes from its own supply. So the code is exactly what you would write for an LED on D9:

  • digitalWrite(fanPin, HIGH) turns the fan on at full power, LOW turns it off
  • analogWrite(fanPin, 0 to 255) sets the speed with PWM, as in Lesson 15

What the simulator does not model: the external supply, the gate resistors, the flyback diode, a MOSFET getting hot, or a fan that will not start at low PWM. Those are what the bench is for.

Worked example

The lesson's sketch

Press Run and watch the fan speed up from off to full, stop for a second, then start again.

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);
}

Turn on JavaScript to edit, run and test this code in the browser.

  • Each step of the loop raises the power by 1 and waits 10 ms, so the ramp takes about 2.5 s.
  • A real fan may not start turning until the PWM value is quite high. That is normal.
Exercise 19.1

Fan on, fan off

Not started

Turn the fan on for 2 seconds, then off for 1 second, over and over.

Use digitalWrite - on is HIGH, off is LOW.

const byte fanPin = 9;

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

void loop() {
  // Fan on, wait 2 seconds, fan off, wait 1 second

}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 19.2

Half speed, then full speed

Not started

Now set the speed with analogWrite. Each time round loop():

  • run the fan at half power (128) for 2 seconds
  • then at full power (255) for 2 seconds
  • then off (0) for 1 second
const byte fanPin = 9;

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

void loop() {
  // Half power for 2 s, full power for 2 s, off for 1 s

}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 19.3

Soft start, then hold

Not started

Starting a fan at full power all at once pulls a big surge of current. A soft start brings the power up in small steps instead.

Each time round loop():

  • use a for loop to raise the power from 0 to 255, one step at a time, waiting 10 ms after each step
  • hold full power for 2 seconds
  • then turn the fan off for 1 second

It is the lesson's sketch with a 2 second hold added.

const byte fanPin = 9;

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

void loop() {
  // for loop: power from 0 to 255, analogWrite each step, then delay(10)

  // hold full power for 2 seconds

  // off for 1 second

}

Turn on JavaScript to edit, run and test this code in the browser.

Exercise 19.4

A thermostat that does not flicker

Not started

The board now has a temperature sensor on A0 (move its slider while the sketch runs). Switch the fan on when the room is hot.

A single switch point causes trouble: if the fan turns on above 600 and off below 600, a reading that wobbles around 600 switches the fan on and off many times a second. The fix is a gap between the two switch points, called hysteresis:

  • reading above 600: fan on (HIGH)
  • reading below 500: fan off (LOW)
  • in between: do nothing - the fan stays as it was

Read the sensor every 50 ms. The test warms the room up, lets it cool a little, cools it right down, then warms it again - and checks the fan only changes at the right points.

const byte fanPin = 9;
const byte sensorPin = A0;

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

void loop() {
  int reading = analogRead(sensorPin);
  // Above 600: fan on. Below 500: fan off. In between: leave the fan alone.

  delay(50);
}

Turn on JavaScript to edit, run and test this code in the browser.

Try it on a real Uno

Build the MOSFET fan circuit from Lesson 16 - gate resistor, pull-down, flyback diode and common GND - and upload Half speed, then full speed. Measure VDS at full power: well under 1 V means the MOSFET is fully on.

For the thermostat, a potentiometer on A0 stands in for the temperature sensor, as in the lesson's challenge.