Electronics Design AU
Components

How Do You Design a Relay Driver Circuit for a Microcontroller GPIO?

Last updated 6 July 2026 · 7 min read

Direct Answer

A microcontroller GPIO cannot drive a relay coil directly — most relay coils draw tens of milliamps, well beyond a GPIO's typical few-milliamp to 20-milliamp source/sink rating, and the coil's inductance generates a large back-EMF voltage spike when current is switched off, which would exceed the GPIO's absolute maximum voltage rating without protection. The standard driver circuit is a low-side switch — an NPN BJT or an N-channel MOSFET between the coil's low side and ground, switched on by the GPIO through a base or gate resistor — with a flyback (freewheeling) diode connected across the coil, cathode to the supply rail, to clamp the turn-off voltage spike and give the collapsing magnetic field a safe path to dissipate its energy. For multiple relays, a Darlington driver array IC (the ULN2003/ULN2803 family) packages several of these switch-plus-flyback-diode stages in one part. Where electrical isolation between the control circuit and the switched load matters, an optocoupler-isolated driver stage adds that separation; where the mechanical wear and switching speed of an electromechanical relay are a problem, a solid-state relay (SSR) is a coil-free, contact-free alternative with a different set of trade-offs.

Detailed Explanation

Relays remain one of the most common ways to let a low-power microcontroller control a higher-power or higher-voltage load — a mains-voltage appliance, a solenoid valve, a motor contactor — because the relay's mechanical contacts provide genuine galvanic isolation between the low-voltage control side and the switched load, and the coil itself needs only a small, well-defined drive circuit to operate.

Why a GPIO Can't Drive a Relay Coil Directly

Two separate problems rule out connecting a relay coil straight to a GPIO pin. First, current: most small relay coils draw tens of milliamps at their rated coil voltage, while a typical MCU GPIO is rated for a few milliamps to perhaps 20 mA of source or sink current (check the specific MCU's datasheet — this varies significantly by family) — well short of what most coils need. Second, and more consequential, is the coil's inductance: when current through an inductor is interrupted, the inductor generates whatever voltage is necessary to try to maintain that current flow, and for a relay coil this voltage spike commonly reaches many times the coil's rated drive voltage — far beyond a GPIO pin's absolute maximum voltage rating.

The Standard Driver Circuit: Low-Side Switch Plus Flyback Diode

The conventional relay driver circuit places a switching transistor — an NPN BJT or, in most new designs, a logic-level N-channel MOSFET — on the low side of the coil, between the coil and ground, with the coil's high side connected to its rated supply voltage:

  1. The GPIO drives the transistor's base (through a current-limiting base resistor, for a BJT) or gate (directly, for a logic-level MOSFET) rather than the coil itself. This is the same low-side switching arrangement covered in BJT vs MOSFET selection — the transistor, not the GPIO, supplies the coil's actual drive current from the coil's own supply rail.
  2. A flyback (freewheeling) diode is connected directly across the coil terminals, cathode toward the supply rail, anode toward the switched (transistor) side. While the transistor is on and the coil is energised, the diode is reverse-biased and does nothing. The instant the transistor turns off, the collapsing magnetic field forces current to keep flowing in the same direction through the coil — the diode now conducts, giving that current a closed loop to circulate through and dissipate as heat in the coil's own resistance, clamping the voltage spike to roughly one diode drop above the supply rail instead of an uncontrolled breakdown-level spike. This is the same flyback mechanism covered generally on what is a diode? applied specifically to a relay coil.
  3. Choose the transistor's voltage and current ratings with margin over the coil's rated current and the driver supply voltage — a logic-level MOSFET avoids the base-resistor sizing and saturation-voltage calculations a BJT needs, and is the more common choice in new designs for exactly that simplicity.

Driver ICs: ULN2003/ULN2803 and Purpose-Built Relay Drivers

For designs switching several relays, a Darlington transistor array IC like the ULN2003 (7 channels) or ULN2803 (8 channels) integrates the switching transistor and flyback clamp diode for each channel into a single package, accepting logic-level inputs directly from GPIO pins with no external transistor or diode needed per channel. The trade-off is the Darlington output stage's higher saturation voltage compared to a well-selected discrete MOSFET, which matters for power dissipation at higher coil currents but is rarely significant for the small coils typical of signal-level relays. Purpose-built relay driver ICs from various manufacturers offer similar integration with additional features (input filtering, diagnostic feedback) for designs that need them.

Isolation: Optocoupled Relay Drivers

A relay's contacts already provide galvanic isolation on the switched load side, but the coil driver circuit itself (the transistor, its base/gate connection back to the GPIO) is still electrically connected to the control-side ground. Where the design needs isolation on the control side too — a relay controlled from an isolated communication bus, or a design with a noisy or potentially hazardous ground reference on the relay/load side — an optocoupler between the GPIO and the switching transistor's base/gate adds that separation. See optocoupler vs digital isolator for the isolation-technology trade-offs this applies to a relay driver stage specifically.

Solid-State Relays as an Alternative

A solid-state relay (SSR) replaces the coil-and-contacts mechanism entirely with a semiconductor switch (a triac for AC loads, or a MOSFET/IGBT-based output stage for DC), optically isolated from its control input — no coil driver circuit, no flyback diode, and no mechanical contact wear, but a different set of trade-offs covered in the FAQ above (off-state leakage current, on-state heat dissipation, cost). SSRs suit high-cycle-count or high-frequency switching applications where an electromechanical relay's mechanical lifetime would be the limiting factor.

Zeus Design designs relay, contactor, and solid-state load-switching driver circuits — including isolation and flyback/snubber protection — as part of complete product electronics development.

Design Considerations

  • Always include the flyback diode — there is no valid relay coil driver circuit without one. See the FAQ above for the failure mode this prevents; this is not an optional refinement even for "quick" prototype circuits, since transistor failures from missing flyback protection can appear only after repeated cycling, not on the first test.
  • Size the switching transistor for the coil's actual rated current, not an assumed "small load" value. Relay coil current varies significantly between parts — always check the specific relay's datasheet coil current rating rather than assuming a generic small value.
  • Consider a contact snubber separately from the coil flyback diode if switching an inductive load through the relay's contacts. The flyback diode protects the coil driver circuit; it does nothing for arcing across the relay's own contacts when they switch a separate inductive load (a motor, another coil) — that's a different problem requiring an RC snubber or MOV across the contacts themselves, sized to the switched load's characteristics.
  • Check the relay's mechanical and electrical lifetime ratings against the application's expected switching frequency. Electromechanical relay contacts have a finite mechanical (dry-circuit) and electrical (rated-load) cycle life, both specified in the datasheet — an application that switches far more often than the relay's rated lifetime accommodates should reconsider a solid-state relay or a different switching approach.

Common Mistakes

  • Omitting the flyback diode, or getting its polarity backwards. Reversed polarity effectively short-circuits the supply through the diode and transistor when the transistor turns on, rather than protecting anything — verify cathode-to-supply, anode-to-switched-node orientation carefully during design review, not just at prototype bring-up.
  • Driving the coil directly from a GPIO "because the datasheet current looks close enough." Even where a coil's rated current happens to fall within a GPIO's absolute maximum rating, the coil's flyback voltage spike still exceeds the pin's voltage rating without a flyback diode — current headroom alone doesn't make direct GPIO drive safe.
  • Assuming the relay's rated coil voltage tolerates the driver supply's full tolerance range without checking. A 5V-rated coil driven from a supply that can sag or rise outside the coil's specified operating voltage range may fail to pull in reliably (undervoltage) or run hot and wear out early (overvoltage) — verify the actual driver supply's real-world tolerance against the coil's rated operating range, not just its nominal voltage.
  • Confusing coil flyback protection with contact-side snubbing. As covered above, these are two different protection problems on two different parts of the circuit — solving one does not solve the other.
  • Selecting a relay's contact rating from its coil-side connector current instead of its actual contact rating. A relay's contact current and voltage rating (for the switched load) is entirely independent of its coil voltage and current rating (for the driver circuit) — always check the datasheet's contact rating table specifically, not the coil specification, when sizing for the switched load.

Frequently Asked Questions

What happens if I forget the flyback diode on a relay driver circuit?
When the switching transistor turns off, the relay coil's collapsing magnetic field tries to maintain its current flow, generating a large voltage spike across the coil — with no path to dissipate that energy safely, the spike can exceed the switching transistor's breakdown voltage rating, destroying it, often after an unpredictable number of switching cycles rather than failing immediately. Even when the transistor survives (some parts have enough avalanche energy rating to absorb occasional spikes), the unsuppressed spike radiates as EMI and can couple noise into nearby sensitive circuitry. A flyback diode is not an optional refinement — treat it as a mandatory part of any relay (or any inductive-load) driver circuit.
Can I use a relay driver IC like the ULN2003 instead of discrete transistors?
Yes, and for designs driving more than one or two relays it's usually the simpler choice. The ULN2003 (7 channels) and ULN2803 (8 channels) integrate a Darlington transistor switch and a flyback (clamp) diode per channel in one package, directly accepting standard logic-level GPIO inputs and switching loads up to the part's rated voltage and per-channel current. This removes the need to select and size a discrete transistor and diode per relay, at the cost of the Darlington stage's higher saturation voltage compared to a well-chosen discrete MOSFET — a difference that matters for power dissipation at higher currents but is usually negligible for typical small-signal relay coils.
When should I use a solid-state relay (SSR) instead of an electromechanical relay?
A solid-state relay uses a semiconductor switch (typically a triac for AC loads, or a MOSFET/IGBT arrangement for DC) with optical isolation between the control input and the switched output, rather than a mechanical coil and contacts. Choose an SSR when the application needs very high switching frequency or lifetime (no mechanical contact wear), silent operation, or fast switching — but accept its trade-offs: SSRs typically have a small but non-zero leakage current even when off (which can be enough to keep a high-impedance load like an LED indicator faintly lit), generate more heat in the on-state than a mechanical contact's near-zero resistance, and AC-rated SSRs commonly need a heatsink at higher currents. Choose an electromechanical relay when true galvanic isolation with zero off-state leakage matters, when switching frequency is low, or when cost is the dominant factor for simple on/off control.

References

Related Questions

Related Forum Discussions