
Short answer: a microcontroller should not normally drive a relay coil directly from a GPIO pin.
Use the GPIO as a logic command and let a low-side BJT, a logic-level N-channel MOSFET, or a relay-driver IC carry the coil current. Add a defined flyback or clamp path for a DC coil unless it is already integrated. Direct drive is acceptable only when the exact GPIO and coil pass every voltage, current, transient, reset-state, temperature, and timing check at worst case.
This is a model-level decision, not a rule based on the words "3.3 V," "5 V," or "small relay." A pin that can light an LED may still be unable to energize a relay reliably. A relay that happens to click during a bench test may fail to pull in when the coil is hot, the supply is low, several outputs are active, or the product is starting up.
Choose the Driver Topology in 60 Seconds
| Application | Best starting point | Critical check |
|---|---|---|
| One moderate-current DC coil | Low-side NPN BJT | Guaranteed base drive, saturation loss, and diode path |
| Low GPIO loading or low switch drop | Logic-level N-MOSFET | RDS(on) specified at the actual gate voltage |
| Several coils | Multichannel low-side driver | Per-channel, package, clamp, and thermal limits |
| Single-coil latching relay | H-bridge or bidirectional driver | Set/reset polarity, pulse duration, and safe startup |
| Galvanic isolation required | Isolated control plus isolated relay-side power | Complete barrier, not an optocoupler label alone |
| Proposed direct GPIO connection | Exception only | All guaranteed pin, port, coil, clamp, reset, and temperature limits |
Why Direct GPIO Drive Usually Fails
A GPIO is a logic output with finite internal resistance. As source current rises, its HIGH voltage falls; as sink current rises, its LOW voltage rises. The MCU data sheet therefore specifies output voltage at stated current, supply, and temperature conditions. It may also impose simultaneous limits on one pin, a group of pins, a port, a supply pin, and the whole device.
STMicroelectronics AN4899 explains why designers must use the exact product data sheet for both individual GPIO and aggregate current limits. A useful caution appears in the Microchip ATmega328P automotive data sheet: 40 mA per pin is listed under absolute maximum ratings, while guaranteed output-voltage conditions are tested at lower currents. The values differ among MCUs, but the reading rule is universal.
Absolute maximum is not an operating target.
It marks a stress boundary beyond which permanent damage may occur. It does not promise a valid logic voltage, acceptable lifetime, or reliable operation at that current. Design from guaranteed electrical characteristics and recommended conditions.
An overloaded pin can collapse the voltage enough that the relay chatters or never pulls in. The coil can also disturb a shared rail and reset the MCU. At turn-off, an uncontrolled inductive transient can overstress the driver path. Even if one bench sample survives, the circuit may have no production margin.
Calculate the Relay Coil Requirement First
For a non-latching DC coil at steady state, start with Icoil = Vcoil / Rcoil. Then read the full relay data: rated voltage, rated current or resistance, tolerance, must-operate voltage, must-release voltage, maximum allowable voltage, temperature notes, duty, and exact coil suffix.
| Coil field | What it controls | Design mistake to avoid |
|---|---|---|
| Rated coil voltage | The intended coil version | Assuming a 5 V coil is a 5 V logic input |
| Current or resistance | Supply and switch current | Using only a typical or nominal value |
| Must-operate voltage | Guaranteed pull-in boundary | Treating it as the preferred supply voltage |
| Must-release voltage | Guaranteed return boundary | Ignoring leakage or slow current decay |
| Maximum coil voltage | Upper limit under stated conditions | Using it as a continuous target |
| Coil type | Non-latching or latching topology | Applying continuous drive to a pulse-rated latching coil |
Do not describe every DC relay as having coil "inrush." Inductance initially opposes the rise of current. The steady current is mainly set by applied voltage and winding resistance, and a cold copper winding can draw more than a hot winding because resistance is lower. AC coils behave differently and must be evaluated from their sealed/unsealed and frequency-specific ratings.
QIANJI's guide to reading a relay data sheet before buying shows how to keep nominal values, guaranteed limits, reference values, and test conditions separate.
Worked Example: A 5 V Coil on a 3.3 V MCU
OMRON's G2R PCB power relay data sheet lists one high-sensitivity 5 VDC coil at 70 Ω and 71.4 mA, with a DC coil-resistance tolerance of ±10% at 23°C. This is an illustrative relay variant, not a universal QIANJI design value.
| Condition | Illustrative arithmetic | Meaning |
|---|---|---|
| Nominal 5 V, 70 Ω | 5 / 70 = 71.4 mA | Already above the normal logic-load range of many GPIOs |
| 5.25 V, 63 Ω at 23°C tolerance edge | 5.25 / 63 = 83.3 mA | Useful current-screening edge, not a full thermal worst case |
| 3.3 V applied directly | 3.3 / 70 = 47.1 mA before GPIO droop | Still heavy for a GPIO and below the rated coil voltage |
The transistor stage allows the 3.3 V pin to control a separate 5 V coil supply. Verify two different corners: at low supply and high driver drop, the voltage at the coil must still clear the guaranteed must-operate requirement with margin; at high supply and low coil resistance, current, switch loss, diode stress, supply capacity, and coil heating must remain acceptable. QIANJI's pick-up versus drop-out voltage guide explains why these boundaries are not interchangeable.
How to Design a Low-Side NPN BJT Driver
Connect the coil from its positive supply to the NPN collector. Connect the emitter to the common return. Drive the base from the GPIO through a resistor. Place the DC coil clamp across the coil with the correct polarity. This arrangement keeps the relay current out of the GPIO while allowing a small base current to command a larger collector current.
Choose collector-emitter voltage, collector current, pulse capability, package dissipation, and temperature range with margin. Do not size the base resistor from the transistor's headline small-signal hFE. For switching, use a forced gain supported by the device data and then verify saturation.
Preliminary BJT relationships
IB = Icoil / forced β
RB = (VOH(min) − VBE) / IB
For the 71.4 mA example and a provisional forced β of 10, base current is about 7.14 mA. If a hypothetical GPIO stayed at 3.3 V and VBE were 0.8 V, arithmetic gives about 350 Ω. That is not a released component value. Replace both voltages with guaranteed worst-case values, select a standard resistor, and then recheck GPIO source current, VCE(sat), coil voltage, resistor power, transistor temperature, and startup state.
How to Choose a Logic-Level MOSFET Driver
An N-channel MOSFET is often the cleaner choice when you want very low steady GPIO current and a small switch voltage drop. Connect source to the return, drain to the low side of the coil, and gate to the GPIO through the selected network. Add a gate-to-source pull-down so the relay remains off while the MCU pin is high impedance during reset.
Select VDS above the measured supply-plus-clamp peak with margin, and verify drain current, pulse energy, package thermal performance, gate leakage, and safe operation at temperature. Most importantly, do not choose by VGS(th). Infineon's MOSFET data-sheet explanation defines threshold at a small specified drain current. It marks the beginning of conduction, not guaranteed low resistance at relay current.
| MOSFET item | What to demand | Why it matters |
|---|---|---|
| RDS(on) | Maximum at 2.5 V, 3.0 V, or the real gate voltage | Controls drop and conduction heat |
| VGS(th) | Use only as a threshold characteristic | Does not prove a fully enhanced switch |
| VDS | Margin above measured clamp and overshoot | Turn-off produces the highest switch-node stress |
| Gate network | Series resistor as needed plus defined pull-down | Controls peak pin current, ringing, and reset behavior |
| Temperature | Hot RDS(on) and package dissipation | Resistance usually rises with junction temperature |
Place the Flyback Diode or Clamp Correctly
For the common low-side DC circuit, connect the diode cathode to the positive coil supply and the anode to the transistor side of the coil. The diode is reverse-biased while the coil is energized. When the transistor opens, coil current continues in the same direction through the diode loop instead of forcing the switch node to an uncontrolled voltage.
Conceptual flyback circuit; it is not a model-specific QIANJI wiring diagram. Diagram by Inductiveload via Wikimedia Commons, released into the public domain. No changes made.
Choose the clamp for reverse voltage, initial current, pulse energy, repetition rate, temperature, layout overshoot, and required relay release time. A small signal diode, rectifier, Schottky diode, TVS, diode-plus-Zener network, and driver-integrated clamp are not interchangeable simply because all limit voltage.
A plain diode keeps the decay voltage low, so current and magnetic force fall more slowly. A higher-voltage clamp normally speeds decay but increases stress and electromagnetic noise. TI's relay-driver application brief demonstrates this timing trade-off; its measured results belong to that circuit, not every relay. QIANJI's diode versus TVS versus RC snubber guide provides a broader selection and validation path.
Keep coil suppression and contact-load suppression separate.
The diode across the relay's own coil protects the coil driver. It does not automatically protect the relay contacts from the motor, solenoid, valve, transformer, or other load switched by those contacts. Design and validate both energy paths.
When a Driver IC Is Better Than a Discrete Switch
A driver IC is attractive for several relays, higher coil voltages, compact routing, input conditioning, diagnostics, protection, or peak-and-hold control. It can reduce repeated discrete circuitry, but it does not remove the need to read the data sheet.
| Option | Strength | Release check |
|---|---|---|
| Darlington array | Low cost and multiple channels | Output drop, clamp pin, package heat |
| NMOS array | Lower output drop at common coil currents | Channel/package current and integrated clamp path |
| Smart low-side driver | Diagnostics and protection features | Fault response, clamp level, retry behavior |
| Peak-and-hold driver | Reduced steady coil power where supported | Guaranteed pull-in, hold current, PWM, and EMC |
For example, TI describes the TPL7407LA as a seven-channel NMOS low-side driver compatible with 1.8 V to 5 V logic and with common-cathode clamp diodes for inductive loads. Those features do not mean every combination is valid: check maximum output voltage, current per channel, total package dissipation, clamp connection, ambient temperature, and simultaneous duty.
Can the Coil and MCU Use Different Power Supplies?
Yes. A 3.3 V MCU can command a 5 V, 12 V, or 24 V DC relay coil through a suitably rated driver. In an ordinary non-isolated low-side circuit, the MCU ground and coil-supply return normally share a defined reference. The driver must withstand the coil rail and clamp transient, while the GPIO sees only its logic-side network.
Separate rails do not automatically prevent resets. Supply coupling can still occur through ground impedance, connectors, USB cables, shields, or signal paths. Put the clamp close to the coil/driver loop, decouple the coil rail for its load step, keep high-current returns away from sensitive analog and reset paths, and measure the rail at the MCU pins during switching.
| Supply arrangement | Normal requirement | Main risk |
|---|---|---|
| One shared rail | Enough current, local decoupling, low return impedance | Rail dip resets or corrupts the MCU |
| Different rails, shared ground | Common reference and voltage-rated driver | Ground bounce and clamp current coupling |
| Galvanically isolated sides | Isolated signal and isolated relay-side power | A hidden ground/jumper bypasses the barrier |
Why a 5 V Relay Module May Not Accept 3.3 V Logic
A relay module can include a transistor, diode, indicator LED, resistors, terminals, and sometimes an optocoupler. That can simplify assembly, but the module still has electrical limits. Verify the input threshold and current at 3.3 V, active-HIGH or active-LOW behavior, coil-supply range, default state, quiescent current, contact rating, PCB spacing, and schematic.
An optocoupler does not automatically create useful isolation. If a jumper or external cable ties the input ground to the relay-side ground, the intended barrier is bypassed. If isolation is necessary, verify the complete path, including the relay-side power source, creepage, clearance, insulation rating, connector arrangement, and relevant equipment standard.
COM, NO, and NC describe the contact state when the coil is de-energized. They do not state which terminal is safe to touch or what voltage the assembled module may switch. Coil-driver success does not qualify hazardous-voltage spacing, fusing, enclosure, terminals, or contact life.
Latching Relays Need a Different Driver
A single-coil latching relay normally sets and resets by reversing coil polarity, so it needs an H-bridge or equivalent bidirectional circuit. A dual-coil latching relay uses separate set and reset windings and can use two controlled driver channels. In both cases, follow the published pulse voltage and duration; continuous energization can exceed the permitted duty.
Firmware must prevent conflicting commands and limit pulse time. A remembered software state can differ from the physical contact state after a brownout, interrupted pulse, manual movement, or MCU reset. Use feedback when the application must know the real state. For the full comparison, see QIANJI's latching versus non-latching relay guide.
Validate Hardware and Firmware as One System
- Identify exact parts. Record the MCU order code and GPIO, relay full suffix, coil data, driver, clamp, supply, and PCB revision.
- Review every limit. Compare guaranteed GPIO VOH/VOL, all current limits, coil operate/release conditions, driver voltage/current/thermal ratings, and clamp pulse data.
- Check polarity and physical loops. Confirm transistor pins, diode direction, return path, gate/base network, clamp location, decoupling, and separation between coil and contact circuits.
- Define startup behavior. Test power-up, power-down, reset, bootloader, watchdog, debugger attachment, GPIO initialization, and brownout.
- Measure the ON state. Record voltage at the coil, coil current, switch drop, rail disturbance, and component temperature at supply and temperature corners.
- Measure the OFF transition. Observe switch-node voltage, coil-current decay, ringing, MCU immunity, and real contact release with appropriate probes.
- Test the contact load separately. Include inrush, inductive energy, switching frequency, fault protection, electrical life, and regulatory requirements.
- Control changes. Requalify after changes to the relay, transistor, clamp, layout, supply, harness, firmware timing, or load.
Troubleshoot by the Observed Symptom
| Symptom | Likely cause | Measure next |
|---|---|---|
| Relay chatters or will not pull in | Low coil voltage, supply droop, or excessive switch drop | Voltage directly across the coil during pull-in |
| MCU resets when relay turns on | Shared-rail dip, ground bounce, or poor decoupling | MCU VDD and ground at the device pins |
| Glitch occurs at turn-off | Missing/wrong clamp, long loop, or layout coupling | Driver node and coil voltage with safe probing |
| Relay releases too slowly | Low-voltage diode keeps current circulating | Coil-current decay and actual contact transition |
| Relay turns on during boot | Floating input, wrong pull resistor, or active-LOW module | Gate/base node from power application through initialization |
| Transistor runs hot | Insufficient drive, high output drop, or thermal under-rating | VCE(sat) or VDS, current, duty, and package temperature |
What Should an OEM Send to a Relay Supplier?
"5 V relay for a microcontroller" is not enough to select or qualify a part. Send a complete control and load profile so the supplier can match an exact coil and contact configuration.
| Information group | Details to provide | Why it changes selection |
|---|---|---|
| Control side | MCU, GPIO voltage, driver topology, coil rail, clamp | Defines pull-in margin, transient, and release |
| Relay function | Non-latching/latching, contact form, poles, mounting | Changes coil topology and package |
| Contact load | AC/DC voltage, make/carry/break current, inrush, L/R or power factor | Determines contact stress and life evidence |
| Mission profile | Cycles, duty, ambient, vibration, humidity, target life | Controls thermal and endurance margin |
| Commercial/compliance | Annual quantity, sample need, approvals, traceability, PCN | Aligns evidence, production, and change control |
Use QIANJI's PCB relay range as a shortlist, then approve the exact model from its current data sheet and finished-system test.
Frequently Asked Questions
Can a tiny reed-relay coil connect directly to a GPIO?
Possibly, but only after a worst-case calculation proves the exact coil and GPIO satisfy guaranteed output voltage, pin current, aggregate current, temperature, reset-state, and turn-off transient requirements. A small transistor usually costs little and creates useful design margin.
Can I power the coil from the MCU board's 5 V pin?
Only if that rail, regulator, USB path, connector, and wiring can supply the coil plus every other load without excessive drop, noise, or heat. The GPIO should still command a driver rather than carry coil current.
Does the flyback diode go in series with the coil?
No in the usual low-side fixed-polarity DC circuit. It goes across the coil and is reverse-biased during normal energization. A series diode can serve other protection purposes but does not create the same recirculating current path.
Is a 1N4007 always the correct relay diode?
No. Select the diode from reverse voltage, initial turn-off current, pulse energy, repetition rate, temperature, recovery behavior where relevant, and release-time requirement. Follow relay and driver guidance for the exact circuit.
Do I need an optocoupler?
Not for every extra-low-voltage relay driver. A transistor with a shared ground is often sufficient. Use intentional isolation when noise, architecture, or safety requirements demand it, and verify the complete barrier and isolated power arrangement.
Can I use PWM to reduce relay holding power?
Only when the exact relay and driver design support a defined pull-in phase followed by a proven hold level. Check coil temperature, minimum hold margin, vibration, PWM current ripple, acoustics, release behavior, EMC, and fault response. Do not apply generic PWM to an unverified relay.
Final Recommendation
Treat the GPIO as a command signal, not the relay's power source. A reliable microcontroller relay driver normally uses a low-side BJT, logic-level MOSFET, or dedicated driver sized from the exact coil, with a defined reset state and a deliberate flyback or clamp path. Then verify pull-in, steady operation, turn-off, release, supply integrity, and thermal behavior on the finished assembly.
Direct drive is the exception. If it is proposed, document the guaranteed GPIO voltage at the required current, every pin/port/device limit, the relay's worst-case coil demand, the turn-off energy path, power sequencing, and measured behavior across supply and temperature. If any item is uncertain, add the driver stage.
Discuss Your Relay and Coil Requirements with QIANJI
Send the MCU part number, GPIO voltage, coil supply range, relay full order code or target specification, driver/clamp plan, contact load, operating temperature, approvals, sample quantity, and annual volume. QIANJI can help identify candidate relay models and provide model-specific data for your own circuit validation.
Technical references
- STMicroelectronics AN4899 – STM32 Microcontroller GPIO
- Microchip – ATmega328P Automotive Data Sheet
- OMRON – G2R PCB Power Relay Data Sheet
- Texas Instruments – TPL7407LA Low-Side Driver
- Texas Instruments – Using DRV89xx-Q1 as a High-Side or Low-Side Switch
- Infineon – Power MOSFET Data-Sheet Explanation
Engineering and safety note: this article explains component-selection logic; it is not a certified reference design. Verify the latest data sheets, approvals, applicable standards, and model-specific instructions. Validate the complete relay, driver, clamp, PCB, supply, firmware, contact load, enclosure, environment, and production configuration before release.
