All articles

    What Is a Relay and How Do You Drive It from Arduino and ESP32?

    How a relay works, its terminals (COM, NO, NC) and ratings, AC vs DC contact ratings, coil current, driving it from a microcontroller, wiring a 220 V lamp correctly, difficult loads, testing with a multimeter, relay vs SSR vs MOSFET, projects and safety.

    Updated 6 min read

    A relay is an electrically operated switch. A small signal from an Arduino or ESP32 can switch a lamp, motor or heater on 220 V, with no electrical connection between the two circuits. That's why it's at the heart of most smart home projects.

    How does it work?

    Inside a relay there's:

    1. A coil (an electromagnet).
    2. A metal arm that's pulled towards the coil when it's energised.
    3. Contacts that move with the arm.
    When the coil is energised, the arm moves from NC to NO and the lamp lights
    When the coil is energised, the arm moves from NC to NO and the lamp lights

    The terminals

    • COM (common): the shared terminal that moves.
    • NO (normally open): disconnected from COM at rest, and connected when the relay switches on.
    • NC (normally closed): connected to COM at rest, and disconnected when the relay switches on.

    If you want the load to run when you send a signal, use COM and NO.

    The ratings printed on it

    For example, on an SRD-05VDC-SL-C relay:

    • 05VDC: the coil voltage is 5 V DC.
    • 10A 250VAC: the maximum current and voltage for the contacts.

    Note that these maximums are for ordinary (resistive) loads. Motors and lamps that draw a large current at switch-on need more margin.

    You'll also find 10A 30VDC on the same relay: the same current but at a much lower DC voltage. That's not a misprint. Breaking direct current is harder than alternating current, because AC passes through zero 100 times a second so the arc extinguishes itself, whereas with DC the arc keeps burning between the contacts and erodes them. So if you're switching a 24 V DC motor, for example, go by the DC rating, not the AC one.

    How much does the coil consume?

    A small 5 V relay coil is about 70 Ω, so it draws about 71 mA and 0.36 W for as long as it's on. That matters in battery projects, and with 8 relays on together it's more than half an amp, so the supply has to handle it. There's also a latching relay that stays in position without the coil staying powered, and needs only a pulse to flip.

    Number of poles

    An ordinary relay is SPDT: one common terminal that moves between NO and NC. There's also DPDT, with two sets moving together, useful if you need to switch two wires at once or reverse a motor.

    Driving it from a microcontroller

    The coil draws current (typically 70–90 mA for a 5 V relay), which is more than a microcontroller pin can supply. So you need:

    1. A transistor to drive the coil.
    2. A protection diode in reverse across the coil.
    The pin drives a transistor, the transistor drives the relay coil with the diode reversed across it. On the other side, the relay breaks the lamp's live (L) wire
    The pin drives a transistor, the transistor drives the relay coil with the diode reversed across it. On the other side, the relay breaks the lamp's live (L) wire

    The full calculation is in the transistor as a switch article.

    Ready-made relay modules

    A module includes the transistor, diode and an LED, and sometimes an optocoupler to isolate the signal. You connect VCC, GND and the signal directly. Note that some modules are active-low (they switch on with a LOW signal). Details, including how to use the JD-VCC jumper so the isolation is real, are in the relay module article. And the optocoupler itself is in the optocoupler article.

    Wiring a 220 V lamp correctly

    The relay must break the live wire, not the neutral. If it breaks the neutral, the lamp goes off, but its wires and holder stay live the whole time, and anyone changing the "off" lamp could get a shock.

    1. Live from the supply → COM.
    2. NO → one terminal of the lamp.
    3. The lamp's other terminal → straight to neutral.

    Identify the live wire with a test screwdriver (the one that lights up), and don't rely on wire colours, because household wiring colours aren't always right.

    Difficult loads

    • Motors, fans and compressors: at start-up they draw up to 5 times their normal current, and they spark when disconnected. Choose a relay rated at least twice the load current.
    • LED lamps and power supplies: they contain capacitors that draw a very high current peak for a fraction of a second at switch-on, which can weld the relay contacts together (you'll find the relay stuck on and not switching off).
    • Heaters and kettles: easy resistive loads, but high current. A 2000 W heater on 220 V = 9 A, very close to the 10 A limit. Use a bigger relay or a contactor here.

    With large inductive loads, a circuit called a snubber (a resistor and capacitor in series, across the contacts) reduces arcing and extends the relay's life.

    Test the relay with a multimeter

    1. The coil: in resistance mode, across the coil terminals. About 70 Ω for a small 5 V relay; OL means the coil is open.
    2. The contacts with it off: in continuity mode, COM to NC should beep, and COM to NO shouldn't.
    3. Switch it on: give the coil its rated voltage (5 V from a bench supply), and you'll hear a click. Now COM to NO should beep, and COM to NC shouldn't.

    If the click happens but the contacts don't change, or COM to NO beeps while it's off, the contacts are welded or worn. How to use a multimeter is in this article.

    Relay, SSR or MOSFET?

    • Mechanical relay: cheap, switches AC and DC, and fully isolated. But it's audible, its contacts have a limited life, and it's not suitable for fast switching.
    • SSR (solid state relay): no moving parts, silent and faster, but more expensive and it heats up.
    • MOSFET: for DC loads only, and very fast (suitable for speed and brightness control with PWM).

    Project ideas

    • A lamp controlled from your phone: an ESP32 serves a web page with a button, and the button switches the relay. The method is in the ESP32 web page article.
    • A water pump that runs by itself: a level sensor in the tank, and the relay starts the pump when the water drops and stops it when it's full. For pumps, use a relay or contactor rated well above the motor current.
    • A lamp that switches on at night: a light sensor decides, and the relay acts. Code without flickering is in the light and temperature sensors article.

    Safety with 220 V

    • Disconnect the power before touching any wire.
    • Keep the 220 V wires separated and insulated from the low-voltage circuit, inside an enclosure.
    • Use wire that can carry the load current, and a suitable fuse.
    • If you're not sure, get an electrician; a mistake here can be dangerous.

    The full rules are in the mains safety article.

    FAQ

    The relay clicks and drops out by itself? Usually the supply isn't strong enough: the coil draws current, the voltage drops, the microcontroller resets, and the cycle repeats. Power the relay from a stronger supply, or put a 470 µF capacitor on its supply.

    How long does a relay last? The datasheet usually says millions of operations with no load, and about 100,000 at full load. So if it switches every minute, it could be worn out in two months. For frequent switching, use an SSR or a MOSFET.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.