← LOGBOOK LOG-010
COMPLETE · ELECTRONICS ·
COMPONENTSRELAYSTRANSISTORSSWITCHING

Relays & Transistors

Two ways to use a small signal to control a large one — the electromechanical relay and the semiconductor transistor.

The Core Problem

A microcontroller GPIO pin can source or sink around 20–40mA at 3.3V or 5V. That’s enough for an LED, but nothing like what a motor, lamp, or high-voltage device needs. You need a way to use that small signal to switch a much larger load.

Two solutions: relay (electromechanical) or transistor (semiconductor).

Relays

A relay is an electrically operated mechanical switch. A small current through a coil creates a magnetic field that physically moves a metal armature, closing (or opening) a separate set of contacts.

The circuit has two completely isolated parts:

  1. Control circuit — low voltage, low current. Powers the coil.
  2. Load circuit — can be high voltage, high current, even AC. Switched by the contacts.

The contacts never touch the coil electrically. This isolation is the relay’s superpower.

Relay Specs

  • Coil voltage: what voltage activates it (common: 5V, 12V)
  • Coil current: how much the control side draws (typically 60–150mA — too much for a GPIO pin directly)
  • Contact rating: what the load side can handle (e.g., 10A at 250VAC)
  • NO / NC contacts: Normally Open (switch closes when activated) or Normally Closed (switch opens when activated)

Driving a Relay from a Microcontroller

Since the coil needs more current than a GPIO can supply, use a transistor as the buffer:

GPIO pin ── [Base resistor] ── Transistor base
Transistor collector ── Relay coil ── Vcc
Transistor emitter ── GND
[Flyback diode] across relay coil (essential — see below)

Flyback Diode — Critical

When the relay coil de-energizes, the collapsing magnetic field generates a large reverse voltage spike. Without a diode across the coil, this spike hits the transistor and can destroy it. Always place a 1N4007 (or equivalent) across the coil, cathode toward Vcc.

Relay Downsides

  • Slow (switching times in milliseconds)
  • Mechanical wear — limited cycle life
  • Generates noise and slight sparking at contacts
  • Bulky compared to solid-state solutions

Good for: mains voltage switching, complete electrical isolation, switching high currents, applications where contact resistance must be near zero.

Transistors

A transistor is a semiconductor device that uses a small signal to control a large one — but entirely in solid state, no moving parts.

There are two main families: BJT (Bipolar Junction Transistor) and MOSFET. Covering BJT here.

NPN BJT (e.g. 2N2222, BC547)

Three terminals: Base (B), Collector (C), Emitter (E).

A small current into the Base controls a much larger current from Collector to Emitter.

Current gain (hFE or β): I_C = β × I_B

A 2N2222 has β ≈ 100–300. Force 1mA into the base → up to 300mA flows collector to emitter.

In switching mode (saturation):

  • Base resistor limits base current
  • Collector current limited by the load
  • The transistor is either fully ON (Vce ≈ 0.2V) or fully OFF (Ic ≈ 0)

Base resistor calculation:

To switch a relay coil drawing 100mA:

I_B needed = I_C / β = 100mA / 100 = 1mA (minimum)
Use 5× for saturation: I_B = 5mA
R_B = (V_GPIO - V_BE) / I_B = (5V - 0.7V) / 0.005A = 860Ω → use 1kΩ

PNP vs NPN

NPN: Base current flows into base. Collector to Emitter when ON. Common emitter to GND. Think of it as a “low-side switch” — it switches the ground path.

PNP: Base current flows out of base. Emitter to Collector when ON. Common emitter to Vcc. “High-side switch.”

For simple microcontroller switching: NPN with load between Vcc and collector is the go-to.

Transistor vs Relay

RelayTransistor
IsolationCompleteNone
SpeedSlow (ms)Fast (ns–µs)
WearYes (mechanical)No
AC switchingYesNo (for MOSFETs: limited)
NoiseClick + sparkSilent
CostMoreLess

What I Built

Used a 2N2222 transistor + 1kΩ base resistor to switch a 5V relay from a GPIO pin. The relay’s NO contacts switched a higher-current LED (that I couldn’t drive directly from the pin). Verified the flyback diode was absolutely necessary — without it, the transistor ran hot and eventually failed.