Home / Learn / Electronics basics

Relays explained: how they work and how to drive one from an Arduino or Pico

New to this? Read from the top. Already know the basics? Jump to Going deeper for intermediate and advanced detail.

Inside a relay: a coil pulls an armature so the common contact moves from normally closed to normally open. Beside it, the driver circuit: a microcontroller pin drives an NPN transistor through a base resistor, with a flyback diode across the coil
Beginner

A relay is a switch worked by an electromagnet. A small current through its coil pulls a contact across, so a 5 V signal can switch a completely separate circuit. It's how a microcontroller can turn on a pump, a fan or a lamp. This guide covers how relays work, how to drive one safely, and what the ratings mean.

How a relay works

Inside a typical relay there's a coil wound on an iron core, a hinged iron armature, a spring, and a set of contacts. With no current, the spring holds the armature away from the coil. When current flows, the coil becomes a magnet, pulls the armature in, and the moving contact swaps over. Remove the current and the spring pulls it back. You can usually hear the click.

Because the coil and contacts are physically separate, the switched circuit is isolated from the control circuit. That isolation is the main reason relays are still used.

COM, NO and NC

  • COM (common) is the moving contact.
  • NO (normally open) connects to COM only while the coil is energised.
  • NC (normally closed) connects to COM while the coil is off.

Wire your load through COM and NO if it should be off when the controller is off or unpowered, which is the safe default for most things. Use NC only when the load must stay on if the controller fails.

Read the two sets of ratings

RatingWhat it means
Coil voltageThe voltage the coil is designed for, such as 5 V or 12 V DC. Use the right one: a 12 V relay won't pull in reliably on 5 V.
Coil resistance or currentSets how much current the driver must supply. A common 5 V hobby relay coil is about 70 Ω, so it draws 5 ÷ 70 = 71 mA.
Contact ratingThe most current and voltage the contacts can switch, often different for AC and DC, and for resistive and inductive loads. DC is harder to break than AC because the arc doesn't self-extinguish at a zero crossing.

Why you can't connect a coil straight to a pin

An Arduino Uno's ATmega328P is rated at 40 mA absolute maximum per pin, and Arduino recommend staying well below that. A Raspberry Pi Pico's pins default to a 4 mA drive setting and the Pi's GPIO is 3.3 V. A 71 mA coil is too much for any of them. The second problem is the voltage spike when the coil turns off: the inductor tries to keep its current flowing, and with nowhere to go, the voltage jumps high enough to damage the pin.

The standard driver circuit

Use a transistor as a switch, and a diode across the coil:

  1. Coil from +5 V to the collector of an NPN transistor (or the drain of a logic-level N-channel MOSFET).
  2. Emitter (or source) to 0 V.
  3. A resistor from the microcontroller pin to the base. To switch the transistor hard on, aim for a base current of about a tenth of the coil current: 7.1 mA for this coil, so about (5 − 0.7) ÷ 7.1 mA ≈ 602 Ω on a 5 V Arduino; 560 Ω is the nearest common value below. Our transistor base resistor calculator works it out for other coils and 3.3 V boards.
  4. A flyback diode (a 1N4148 or 1N4001) across the coil, with its cathode to +5 V. It does nothing while the coil is on, then catches the spike when it turns off.
  5. Connect the grounds of the relay supply and the microcontroller together.

The full reasoning is in how to use a transistor as a switch.

Relay modules

Ready-made relay modules already include the transistor, diode and an indicator LED, and some add an opto-isolator. Check three things before you buy:

  • Trigger level: many modules switch on when the input is pulled low (active-low), which surprises people when the relay clicks on at start-up.
  • 3.3 V compatibility: a module designed for 5 V boards may not switch reliably from a 3.3 V Pico or ESP32 pin.
  • Real contact rating: the numbers printed on the blue relay are the relay's ratings, not necessarily what the module's PCB tracks and terminals can safely carry.

Mains: be careful

A relay rated for 230 V contacts doesn't make a breadboard or a cheap module safe for mains. Mains wiring needs proper enclosures, creepage and clearance distances between the low-voltage and mains sides, fusing, strain relief and earthing. For mains loads at home, use a certified product such as a smart plug or an installed smart switch, or get a qualified electrician involved.

Solid-state relays

A solid-state relay (SSR) uses an opto-isolator and a semiconductor switch instead of moving contacts. It's silent, doesn't wear out and switches fast. The trade-offs: it drops a volt or so and gets warm (larger loads need a heatsink), it leaks a small current when off, and most AC SSRs with a TRIAC output won't switch DC at all. Choose a mechanical relay when you need a true air gap and low losses; choose an SSR for frequent switching and silence.

Intermediate and advanced

Going deeper

How the flyback diode slows release

With a plain diode across the coil, the coil current decays slowly, through the diode's 0.7 V and the coil's own resistance. That delays the contacts opening by a few milliseconds and slows the contacts as they separate, which can increase arcing on heavy loads. Where fast release matters, add a zener in series with the diode (or use a TVS diode): the coil's energy is dumped at the higher clamp voltage, so the current falls faster. Make sure the transistor is rated for the supply voltage plus the clamp voltage.

Contact life and inductive loads

Datasheets give electrical life at the rated resistive load, often around 100,000 operations, and much longer mechanical life with no load. Motors, transformers and solenoids draw a large inrush current and produce an arc when broken, both of which shorten contact life. Derate the contacts heavily for these loads, and fit a snubber (a resistor and capacitor in series) across the contacts or an RC or varistor across the load as the relay or load maker advises.

Latching relays

A latching relay holds its position with no coil current and needs only a short pulse to change over. That makes it attractive for battery projects, since an ordinary relay coil drawing 70 mA would flatten a small battery in hours.

Driving from 3.3 V

From a 3.3 V pin, the base resistor for the same 7 mA base current is about (3.3 − 0.7) ÷ 0.007 ≈ 370 Ω, giving 7 mA from the pin. That's more than the Pico's default 4 mA drive setting is specified for. A logic-level MOSFET whose datasheet guarantees its on-resistance at a 2.5 V gate drive, or a small Darlington array such as the ULN2003 (under 1 mA per input), takes almost nothing from the pin.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.