PyRasp

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.

A GPIO pin gives a small signal, 3.3 V and a few milliamps, to a driver such as a relay or MOSFET. The driver switches a separate supply into the big load: a lamp, fan, pump or motor.

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 relay has three contacts. With the coil off, COM connects to NC, so a lamp on NC is on and a lamp on NO is off. With the coil on, COM connects to NO, so the lamp on NO is on and the lamp on NC is off.

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

  1. 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.
  2. 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.
  3. 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.
  4. 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 seeWhyFix
The lamp is always onWired to NC instead of NOMove the load to COM–NO
The relay clicks but the lamp does nothingThe load has no power or no return pathCheck the load's supply and ground
The relay never clicksActive-low module driven as active-highUse Relay("low"), or invert the logic
The GPIO is damagedThe coil was wired directly to the pinThe coil goes to a supply; the pin drives IN
The MOSFET gets hotIt is not fully on at 3.3 VUse a logic-level MOSFET, or a relay

Check yourself

  1. What is the difference between NO and NC?
  2. Why does a relay isolate the load?
  3. 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:

IN1IN2ENAResult
00anycoast / stop
101forward
011reverse
111brake

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.

Inside an H-bridge: four switches around the motor. With IN1 high and IN2 low, the top-left and bottom-right switches close and current flows through the motor from left to right, so it turns forward. Swapping IN1 and IN2 reverses it; PWM on ENA sets the speed.

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

  1. Forward: IN1 high, IN2 low, ENA high. The motor spins up to a steady speed.
  2. Reverse: swap IN1 and IN2. The speed reading becomes negative.
  3. Two buttons: GP12 forward, GP13 reverse. Debounce both; the last press wins. ENA stays high.
  4. 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 seeWhyFix
The motor does not moveENA is lowSet ENA high, or give it PWM
It only goes one wayIN1 and IN2 are the sameOne high, one low
It brakes instead of coastingBoth inputs highBoth low to coast
The speed does not changeThe pot is not on GP26, or duty is fixedADC(26) and pwm.duty_u16
The 3V3 rail sagsThe motor is drawing from the logic railPower the motor from VBUS / its own supply

Check yourself

  1. Which pin sets the direction: IN1 or ENA?
  2. What does ENA do?
  3. 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

LoadDriverWhy
LED, small buzzerResistor (and a transistor for a big buzzer)A few milliamps is within a GPIO's budget
Lamp, fan, pumpRelay moduleIsolation, easy to wire, clicks
Lamp, fan (quiet, fast)Logic-level MOSFETSilent, no moving parts, needs a common ground
DC motor, one directionMOSFETSimple speed control with PWM
DC motor, both directionsL298N H-bridgeReverses 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