PyRaspBeta

Bare relay

A bare relay is what is inside a relay module: a coil and a set of mechanical contacts in a clear plastic case, with no driver, no diode and no opto-isolation. It costs $2.50 and is the part to reach for when you want to build the driver yourself — the coil is 5 V at about 72 mA, several times a GPIO's safe current, so it needs a transistor and a flyback diode exactly as the module has. What the contacts switch is a completely separate circuit: COM joins NC while the coil is off and NO when it is on, at whatever voltage the contacts are rated for.

Relay, bare

Pins

PinWhat it isType
1Coil end 1passive
2Coil end 2passive
COMCommon contactpassive
NONormally open contactpassive
NCNormally closed contactpassive
  • BareRelay() — Relay, bare — $2.50
  • Pins: 5
  • Chain ends: 1 >> relay-bare >> 2
  • Aliases: plus → 1, minus → 2, common → COM

1 and 2 are the coil; either way round works, because the current is what pulls the armature. COM is the moving contact, resting on NC and moving to NO once the coil current pulls in — about 40 mA — and back once it drops below about 10 mA. The contacts move roughly 10 ms after the coil energises and 5 ms after it lets go.

Wiring it in circuit.py

A transistor-driven coil with its flyback diode; the contacts switch their own LED circuit:

circuit.py
from wiring import *

pi = Pi()
relay = BareRelay()
q1 = Transistor("2N2222A")
rb = Resistor(1000)         # the base: about 2.6 mA is plenty
d1 = Diode("1N4148")        # flyback: cathode to +5 V, anode to the collector
led = LED("green")
r1 = Resistor(330)

pi.GPIO17 >> rb >> q1.base
q1.emitter >> pi.GND

pi.V5 >> relay.plus          # the coil
relay.minus >> q1.collector
d1.cathode >> relay.plus     # across the coil, band toward the positive rail
d1.anode >> q1.collector

pi.V3_3 >> relay.common      # the contacts, in a circuit of their own
relay.no >> r1 >> led.anode
led.cathode >> pi.GND

The coil pins are plus/minus (1/2), common is COM, and NO/NC are just no and nc. The relay module is this part with the driver, diode and isolation already wired.

Driving it in main.py

main.py
from gpiozero import OutputDevice
from time import sleep

relay = OutputDevice(17)   # high = coil energised, COM moves to NO

while True:
    relay.on()
    sleep(0.5)
    relay.off()
    sleep(0.5)

Electrical notes

  • The coil is 70 Ω and about 100 mH: at 5 V it settles at 72 mA with a 1.4 ms time constant, so the armature is asked to move about a millisecond after the GPIO goes high.
  • Pull-in and drop-out currents differ. The model uses 40 mA to pull in and 10 mA to let go, so a supply that cannot reach 40 mA leaves the contacts where they are, and a coil already on stays on until the current really falls away.
  • The contacts are potential-free and carry no coil current: a load wired through COM/NO sees only its own supply, which is what makes a relay a clean way to switch something the Pi cannot.
  • Switch-off is the dangerous moment: without the flyback diode the winding throws hundreds of volts at the transistor. With it, the current freewheels through the diode and the collector stays near 5 V.

Simulator vs real hardware

  • A real bare relay has no markings on the case. Find the coil with a multimeter: the two pins that read about 70 Ω are the coil; COM is the one that is connected to NC at rest.
  • The contacts' travel time is modelled (10 ms operate, 5 ms release) but there is no contact bounce and no arcing, so the simulator will never show the spikes a real relay makes on a long cable.
  • If you hear the click but nothing switches, the coil is doing its job and the contacts are wired wrong — COM is the pin that moves, not NO or NC.