Relays and switching loads
A GPIO is a signal, not a power supply: 3.3 V and a safe 16 mA or so. A motor, heater, pump or lamp wants hundreds of milliamps, or kilowatts. A relay module is an electrically operated switch the GPIO can throw: a tiny current energises a coil, and the coil's movement opens or closes contacts in a completely separate circuit.
Why the GPIO cannot drive the load itself
- The relay's own coil alone needs 72 mA at 5 V — several times a GPIO's safe limit.
- The loads in the shop are power-hungry by nature. The heater, fan, pump and lamp appear on the board as small 5 V stand-ins, but each acts in its simulated room with its rated power — hundreds or thousands of watts. An L298N motor driver exists for the same reason: a motor draws its power from its own supply, not from a pin.
- A relay keeps the Pi's low-voltage side isolated from the switched load. Nothing from the load circuit flows back into the GPIO — which a directly driven coil or motor certainly would, as a voltage spike when it switches off.
How a relay module works
The relay module costs $4 and is a single part with a coil side and a contact side:
| Pin | What it is | Type |
|---|---|---|
| DC+ | Coil supply 5 V | power |
| DC- | Ground | ground |
| IN | Control input (see trigger jumper) | io |
| NO | Normally open contact | passive |
| COM | Common contact | passive |
| NC | Normally closed contact | passive |
| Setting | Type | Range | Default |
|---|---|---|---|
trigger | enum | low, high | low |
- DC+ / DC− power the coil: 5 V and ground.
- IN is the control input. It feeds an opto-isolator or transistor stage (about 2.5 mA at the pin), not the coil directly, so the GPIO's job is tiny and safe.
- COM is the common contact. NC is closed while the relay is off; NO closes when the coil is energised. For a load you switch on and off, wire the supply through COM and the load through NO.
Active-high or active-low. Which input level closes the relay depends on the module's trigger jumper, and the simulator models it: Relay("high") closes when IN is high, Relay("low") (the default) when IN is low. Cheap modules are usually active-low, and there is a catch: a low-trigger module powered from 5 V may not release when the GPIO goes high, because 5 V − 3.3 V = 1.7 V across its input is not enough to turn it off (the simulator needs 2.5 V). Power the module's input side from 3V3, or use an active-high module.
The coil is not instant. Contacts move about 10 ms after the coil energises (operate) and 5 ms after it releases, and the simulation schedules them that way. The contacts themselves are rated 10 A at 250 VAC / 30 VDC — far beyond anything on a 5 V breadboard.
Wiring a heater through a high-trigger relay:
from wiring import *
pi = Pi()
relay = Relay("high")
heater = Heater()
pi.V5 >> relay.dc_plus
relay.com >> relay.dc_plus
relay.no >> heater.plus
heater.minus >> relay.dc_minus
pi.GND >> relay.dc_minus
pi.GPIO17 >> relay.signal
from gpiozero import OutputDevice
from time import sleep
heat = OutputDevice(17) # active-high module: on() drives IN high
while True:
heat.on()
sleep(5)
heat.off()
sleep(5)
For an active-low module, wire Relay() (the default) and use OutputDevice(17, active_high=False), so on() pulls IN low.
The world behind the contacts
The heater is not just a drawing: switched on, it warms its simulated room at the rated power set in the inspector, and the room loses heat to the outdoors. The fan moves air, the pump waters the soil, the lamp lights the room. That coupling is what makes a real control loop possible — read a DS18B20, switch the heater through the relay, and the temperature actually responds. The thermostat recipe builds exactly that.
Going deeper
- Inside the simulated relay. The input stage is a 1300 Ω resistance from IN to DC+ (low-trigger) or from IN to DC− (high-trigger). A low-trigger module switches on when DC+ − IN is more than 2.5 V and IN is not floating; a high-trigger one when IN rises above 1.8 V. The coil becomes a resistance drawing 72 mA from 5 V while energised. Contacts are 0.05 Ω when closed and open otherwise, and if the coil changes again before they move, the pending move is cancelled — the relay always follows the latest input.
- How loads scale. On the board, every load is a 50 Ω stand-in (5 V, 100 mA). Its effect on the world is scaled by (V ÷ 5 V)², clamped, and below 2% of full it counts as off. So a relay that fully connects 5 V gives full rated power; in practice the contacts are open or closed, and the interesting number is the rated power you give the load.
- Relays cannot dim or set speed. The contacts are mechanical and take milliseconds to move: switching them at PWM frequencies would buzz, arc and wear out. On/off control — a thermostat band, a timer — is what a relay is for. For motor speed and direction use the L298N driver, which takes PWM on ENA.
- Never drive a bare relay coil. A coil is an inductor: switching it off kicks back a voltage spike that destroys GPIOs. Modules include the driver transistor and a flyback diode; a bare coil would need both added by hand.
- Mains voltages are out of scope, and dangerous. The contacts are rated for mains, but real mains wiring can kill — use a proper enclosure and qualified help. The shop's loads are simulated 5 V devices, so the wiring above switches low voltage; the isolation lesson is what carries over.
- Coil currents add up. Four relays at once draw about 290 mA from the 5 V pin before their loads. The simulator does not put a budget on the 5 V rail, but on a real Pi the supply feeding those pins is shared with the board itself.
- The Songle relay datasheet is at SRD-05VDC-SL-C, and gpiozero's output device docs cover
OutputDeviceandactive_high.