PyRaspBeta

Relay module

A relay module is an electrically operated switch: a small current through its coil throws a set of mechanical contacts that can switch a completely separate, much hungrier circuit. It costs $4 and is how a GPIO gets to control a heater, fan, pump or lamp — the coil takes 5 V at about 72 mA, several times a GPIO's safe 16 mA, and the load current flows through the contacts, never through the Pi. A relay also cannot dim or set speed: its contacts are on or off, with milliseconds of mechanical delay.

Relay module

Pins

PinWhat it isType
DC+Coil supply 5 Vpower
DC-Groundground
INControl input (see trigger jumper)io
NONormally open contactpassive
COMCommon contactpassive
NCNormally closed contactpassive
SettingTypeRangeDefault
triggerenumlow, highlow
  • Relay("low") — Relay module — $4
  • Pins: 6
  • Aliases: signal → IN, vcc → DC+, gnd → DC-

DC+/DC− power the coil from 5 V. IN feeds the input stage — an opto-isolator or transistor, about 2.5 mA, which is what the GPIO actually drives. COM is the moving contact: it joins NC while the coil is off and NO when it is on, so a load you switch on and off wires from the supply through COM to NO and back to ground.

Which input level closes the contacts is the module's trigger: Relay("high") (what the examples use) closes on a high input, Relay("low") — the default — on a low one. Cheap modules are usually active-low.

Wiring it in circuit.py

A high-trigger relay switching a heater from the Pi's 5 V rail:

circuit.py
from wiring import *

pi = Pi()

relay = Relay("high")        # IN high = contacts closed
heater = Heater()

pi.V5 >> relay.dc_plus
pi.V5 >> relay.com            # the switched supply enters at COM
relay.no >> heater.plus       # and leaves at NO when the relay closes
heater.minus >> relay.dc_minus
relay.dc_minus >> pi.GND      # coil ground, load ground and Pi ground: one net
pi.GPIO17 >> relay.signal

The signal alias is IN, vcc is DC+ and gnd is DC−, so pi.GPIO17 >> relay.signal reads more friendly than `relay.pin("IN")`. A pre-wired version of this circuit — relay, load and wires — is the relay + heater kit (and its fan, pump and lamp siblings).

Driving it in main.py

main.py
from gpiozero import OutputDevice
from time import sleep

heat = OutputDevice(17)      # active-high module: on() drives IN high

while True:
    heat.on()
    sleep(2)
    heat.off()
    sleep(2)

For an active-low module — Relay() with no argument — the same class inverts: use OutputDevice(17, active_high=False) so on() pulls IN low. One catch on a bench: 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 does not clear the 2.5 V threshold. Power the module's input side from 3V3 or use a high-trigger module.

Electrical notes

  • Coil: 5 V, 72 mA while energised. The input stage draws about 2.5 mA — safe for any GPIO.
  • Contacts are rated 10 A at 250 VAC / 10 A at 30 VDC; the model gives them 0.05 Ω closed and open otherwise. The shop's loads only need ~100 mA, so the contacts are massively overspecced.
  • Switching delays: contacts close about 10 ms after the coil energises and open about 5 ms after it releases (both adjustable in the feed). A real module's opto and snubber add a little more.
  • Trigger thresholds: a low-trigger module switches on when DC+ − IN exceeds 2.5 V (and IN is not floating); a high-trigger one when IN rises above 1.8 V.
  • Never drive a bare relay coil from a GPIO — it needs ~70 mA and a flyback diode. The module includes both the driver transistor and the diode; that is the whole point of the module.
  • Mains contacts are rated but out of scope: mains can kill. The lessons here are about low voltages.

Simulator vs real hardware

  • The input stage is modelled as a 1300 Ω resistance from IN to DC+ (low-trigger) or to DC− (high-trigger); the coil becomes a resistance drawing 72 mA from 5 V while energised.
  • Contact movement is scheduled at operate/release time. If the input changes again before the contacts move, the pending move is cancelled — the relay always follows the latest input, which is gentler than a real one.
  • There is no contact bounce in the model; the feed's delays are the only timing.
  • The relay is the bridge between the board and the simulated world: the load behind it — heater, fan, pump or lamp — acts on its zone, so the thermostat recipe reads a DHT22 and switches a heater through the relay.

Going deeper

Further reading