13–14. Real loads
An LED takes a few milliamps. A lamp, a fan, a pump or a motor takes hundreds — sometimes thousands. A GPIO cannot supply that, and a motor can send a voltage spike back when it stops. Drivers solve both problems.

The rule for this module: the GPIO controls the driver; the driver controls the load. Never the GPIO directly.
Safety first. In this simulator everything is low voltage. In a real classroom, never switch mains electricity. A relay module rated for mains is still not a licence to wire mains: use a low-voltage lamp, fan or pump, and have an adult check the circuit.
Lesson 13 — Switch a real load
You will be able to: use a relay as an electrically controlled switch, use a MOSFET for the same job, and choose between them.
The idea
A relay is a mechanical switch with a coil. Current through the coil pulls an arm:
- COM is the moving contact;
- NO (normally open) is disconnected when the coil is off;
- NC (normally closed) is connected when the coil is off.
A relay gives isolation: the coil circuit and the load circuit are not electrically connected. That is why it can switch a different voltage from the one driving it.

A MOSFET is a transistor: a voltage on the gate lets current flow between drain and source. It is silent, fast and small, but it does not isolate the load. Use a logic-level MOSFET at 3.3 V, and put a pull-down on the gate so it is off while the pin is floating.
The exact tools
Relay module, active high:
from machine import Pin
relay = Pin(13, Pin.OUT)
relay.off() # coil off
relay.on() # coil on
relay.toggle()
MOSFET gate:
from machine import Pin
gate = Pin(13, Pin.OUT)
gate.off() # MOSFET off
gate.on() # MOSFET on
For the fan thermostat, the control is analogue:
from machine import ADC
pot = ADC(26)
reading = pot.read_u16()
if reading > 0.7 * 65535:
fan_on = True
elif reading < 0.3 * 65535:
fan_on = False
Build it
- Relay lamp. Coil from VBUS and GND, IN on GP13. Lamp between COM and NO on VBUS. Each debounced press of the button on GP12 toggles the lamp. Start with it off.
- One relay, two lamps. Put one lamp on COM–NO and one on COM–NC. Exactly one is lit at a time; a press swaps them. This is a changeover switch.
- Fan thermostat. A potentiometer on GP26 is the temperature dial. Above 70 % turn the fan on; below 30 % turn it off; in between keep the current state. That gap is hysteresis again.
- MOSFET switch. Replace the relay with a BSS138 (logic-level) MOSFET: gate GP13, source GND, drain to the fan's negative lead, fan positive to VBUS, 10 kΩ gate pull-down. Above 50 % drive the gate high.
Fun fact. The click you hear from a relay is a tiny mechanical arm moving. It is one of the last mechanical switches in a modern classroom — and it is still the easiest way to switch a load that has nothing in common with the Pico's 3.3 V.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| The lamp is always on | Wired to NC instead of NO | Move the load to COM–NO |
| The relay clicks but the lamp does nothing | The load has no power or no return path | Check the load's supply and ground |
| The relay never clicks | Active-low module driven as active-high | Use Relay("low"), or invert the logic |
| The GPIO is damaged | The coil was wired directly to the pin | The coil goes to a supply; the pin drives IN |
| The MOSFET gets hot | It is not fully on at 3.3 V | Use a logic-level MOSFET, or a relay |
Check yourself
- What is the difference between NO and NC?
- Why does a relay isolate the load?
- Why does a MOSFET gate need a pull-down?
Answers: NO is open when the coil is off; NC is closed · the coil and contacts are not electrically connected · so the MOSFET is off when the pin is floating.
Remember
- A relay is a coil-controlled changeover switch: COM, NO, NC.
- A MOSFET is a voltage-controlled switch; use a logic-level type and a gate pull-down.
- The GPIO drives the driver, never the load.
Problems: Relay lamp · One relay, two lamps · Fan thermostat · Transistor switch · Night lamp · Plant waterer
Lesson 14 — Motors
You will be able to: drive a DC motor forward, backward and at a controlled speed with an L298N.
The idea
A DC motor spins one way when voltage is positive across it and the other way when it is reversed. An H-bridge does that reversal electronically. The L298N has two channels; we use one:
| IN1 | IN2 | ENA | Result |
|---|---|---|---|
| 0 | 0 | any | coast / stop |
| 1 | 0 | 1 | forward |
| 0 | 1 | 1 | reverse |
| 1 | 1 | 1 | brake |
ENA enables the channel. A plain high is full speed; a PWM signal on ENA makes the average voltage lower, so the motor turns more slowly.

The exact tools
from machine import Pin, PWM
import utime
in1 = Pin(20, Pin.OUT)
in2 = Pin(21, Pin.OUT)
ena = Pin(22, Pin.OUT)
in1.on()
in2.off()
ena.on() # forward, full speed
Speed control:
pwm = PWM(Pin(22), freq=1000)
in1.on()
in2.off()
pwm.duty_u16(32768) # about half speed
Build it
- Forward: IN1 high, IN2 low, ENA high. The motor spins up to a steady speed.
- Reverse: swap IN1 and IN2. The speed reading becomes negative.
- Two buttons: GP12 forward, GP13 reverse. Debounce both; the last press wins. ENA stays high.
- Speed: a potentiometer on GP26 sets the duty on ENA. Half the pot is about half duty; full is full speed. Keep the direction forward.
Careful. A motor's stall current — the current when the shaft cannot turn — can be several times its running current. Do not power a motor from 3V3, and do not add a heavy load in the simulator's path. On real hardware, use a motor supply and a common ground.
Common mistakes
| What you see | Why | Fix |
|---|---|---|
| The motor does not move | ENA is low | Set ENA high, or give it PWM |
| It only goes one way | IN1 and IN2 are the same | One high, one low |
| It brakes instead of coasting | Both inputs high | Both low to coast |
| The speed does not change | The pot is not on GP26, or duty is fixed | ADC(26) and pwm.duty_u16 |
| The 3V3 rail sags | The motor is drawing from the logic rail | Power the motor from VBUS / its own supply |
Check yourself
- Which pin sets the direction: IN1 or ENA?
- What does ENA do?
- Why is PWM on ENA a speed control rather than a direction control?
Answers: both IN1 and IN2 together · enables the channel (and sets its average) · it changes the fraction of time the motor is driven, not which way.
Remember
- IN1/IN2 choose direction; ENA enables and can set speed.
- PWM on ENA = average voltage = speed.
- Motors need a driver and a supply that can deliver current.
Problems: Motor forward · Motor reverse · Forward and reverse · Motor speed
Choosing a driver
| Load | Driver | Why |
|---|---|---|
| LED, small buzzer | Resistor (and a transistor for a big buzzer) | A few milliamps is within a GPIO's budget |
| Lamp, fan, pump | Relay module | Isolation, easy to wire, clicks |
| Lamp, fan (quiet, fast) | Logic-level MOSFET | Silent, no moving parts, needs a common ground |
| DC motor, one direction | MOSFET | Simple speed control with PWM |
| DC motor, both directions | L298N H-bridge | Reverses the voltage and controls speed |
The classroom rule. If a part has its own power supply, its ground must be joined to the Pico's ground — unless the driver is a relay, which isolates it.
Next
- Capstone — put a sensor, a display and an actuator together.
- The course map — where you are.