Back to Sensors & Actuators Series

Solenoids & Electromagnets

April 10, 2026 Wasil Zafar 16 min read

The simplest electromagnetic actuator — fast, forceful, and binary. Master solenoid types, driver circuits with flyback protection, PWM hold-down technique, and practical embedded code.

Contents

  1. Overview & Types
  2. Working Principle
  3. Electrical & Mechanical Specs
  4. Driver Circuits
  5. Control Methods
  6. Code Example — Arduino & ESP32
  7. Real-World Applications
  8. Advantages vs. Alternatives
  9. Limitations & Considerations

Overview & Types

Solenoids and electromagnets are the simplest electromagnetic actuators – a coil of wire that produces a magnetic field when energized, pulling or pushing a ferromagnetic plunger. They provide fast, binary (on/off) linear motion and are found in everything from door locks to pinball machines to fuel injectors.

Key Insight: Solenoids excel at binary actuation — fast, forceful push or pull with simple on/off control. When you need a “digital” mechanical action (lock/unlock, open/close, strike/release), a solenoid is usually the simplest solution.

Types

  • Linear Push Solenoid: Plunger pushes out when energized. Spring returns when de-energized. Used in locks, dispensers, and pushbutton mechanisms.
  • Linear Pull Solenoid: Plunger retracts into the coil when energized. More common than push types. Used in door strikes, latches, and valve actuation.
  • Rotary Solenoid: Converts electrical pulse into a limited-angle rotation (typically 25–95°). Used in vending machines, textile machinery, and sorting systems.
  • Tubular Solenoid: Cylindrical design with plunger moving along the central axis. High force density, compact form factor.
  • Latching Solenoid: Uses a permanent magnet to hold position without power. A reverse pulse releases. Saves energy in battery-powered applications.
  • Electromagnet: Designed to attract and hold ferromagnetic objects rather than move a plunger. Used in magnetic locks, lifting, and clamping.

Working Principle

All solenoids work on Ampère’s law: current flowing through a coil creates a magnetic field concentrated inside the coil bore.

Linear Solenoid Operation

  1. De-energized: The spring holds the plunger in its rest position (extended for pull, retracted for push).
  2. Energized: Current flows through the coil, creating a magnetic field. The field pulls the ferromagnetic plunger to center of the coil (minimum reluctance position).
  3. Force Profile: Force increases as the plunger moves deeper into the coil (air gap decreases). Maximum force at full stroke.
  4. De-energized Again: Magnetic field collapses, spring returns plunger to rest position. The collapsing field generates a large back-EMF voltage spike.

Force Equation

F = (N² × I² × μ0 × A) / (2 × g²)

Where: N = number of turns, I = current (A), μ0 = permeability of free space, A = cross-sectional area of the plunger (m²), g = air gap (m).

Key takeaway: Force is proportional to I² and inversely proportional to g². Solenoids are strongest at the end of their stroke.

Electromagnet Operation

Electromagnets work identically but are designed to attract external objects rather than move an internal plunger. The holding force follows: F = B²A / (2μ0) where B is the magnetic flux density at the contact surface.

Electrical & Mechanical Specifications

ParameterSmall SolenoidMedium SolenoidElectromagnet
Voltage3.3–12 V DC12–48 V DC5–24 V DC
Current150–500 mA0.5–3 A200 mA – 2 A
Force (start stroke)0.5–5 N5–50 NN/A
Holding Force2–15 N15–200 N20–1000 N
Stroke3–10 mm10–30 mmN/A (contact)
Response Time5–15 ms15–50 ms10–30 ms
Duty Cycle10–100%10–50%Up to 100%
Lifespan500,000 – 10,000,000 cycles
Duty Cycle Warning: Most solenoids are rated for intermittent duty (10–25%). Continuous energization causes overheating and coil burnout. For sustained holding, use latching solenoids or reduce voltage after initial pull-in.

Driver Circuits

MOSFET + Flyback Diode (Basic)

The standard solenoid driver: an N-channel MOSFET (logic-level, e.g., IRLZ44N) switches the solenoid, and a flyback diode (1N4007 or Schottky) protects against inductive kickback.

  • MOSFET gate ← GPIO pin (with 10k pull-down resistor)
  • MOSFET drain ← Solenoid terminal 1
  • Solenoid terminal 2 ← V+ supply
  • Flyback diode: cathode to V+, anode to drain (across solenoid)

Darlington / ULN2803

For driving multiple solenoids, the ULN2803 provides 8 Darlington pairs with built-in flyback diodes. Each channel handles up to 500 mA at 50 V.

H-Bridge (for Latching Solenoids)

Latching solenoids need current in both directions (pulse to set, reverse pulse to release). Use an H-bridge (L293D) or two MOSFETs with appropriate switching logic.

PWM Hold-Down

To reduce heat during sustained operation: apply full voltage for pull-in (~50 ms), then reduce to 30–50% PWM for holding. This dramatically reduces power consumption and heating.

Control Methods

Binary On/Off

The simplest approach – HIGH to energize, LOW to de-energize. Suitable for locks, latches, and simple mechanisms.

PWM Pull-in + Hold

Two-phase control: full power pulse for initial actuation (overcoming spring + air gap), then reduced PWM for holding. This extends duty cycle and reduces heating:

  • Pull-in: 100% duty, 30–100 ms
  • Hold: 30–50% duty, continuous

Pulse Control (Latching)

For latching solenoids, send a short pulse (10–50 ms) in one direction to set, and a reverse pulse to release. No power needed between pulses.

Code Example — Arduino & ESP32

Arduino: Solenoid with PWM Hold-Down

// Solenoid door lock with PWM hold-down for heat reduction
// Wiring: MOSFET gate→D9, Solenoid→12V via MOSFET drain

const int SOL_PIN = 9;   // PWM-capable pin
const int BTN_PIN = 2;   // Unlock button

bool locked = true;

void setup() {
    pinMode(SOL_PIN, OUTPUT);
    pinMode(BTN_PIN, INPUT_PULLUP);
    Serial.begin(9600);
    Serial.println("Solenoid Lock Controller");
}

void activateSolenoid(unsigned long holdTimeMs) {
    // Phase 1: Full power pull-in (50ms)
    analogWrite(SOL_PIN, 255);
    delay(50);

    // Phase 2: PWM hold (reduced power)
    unsigned long start = millis();
    while (millis() - start < holdTimeMs) {
        analogWrite(SOL_PIN, 100); // ~40% duty
        delay(10);
    }

    // Release
    analogWrite(SOL_PIN, 0);
}

void loop() {
    if (digitalRead(BTN_PIN) == LOW) { // Button pressed
        delay(50); // Debounce
        if (digitalRead(BTN_PIN) == LOW) {
            locked = !locked;
            if (!locked) {
                Serial.println("UNLOCKING - solenoid energized");
                activateSolenoid(3000); // Hold open 3 seconds
                locked = true;
                Serial.println("RE-LOCKED");
            }
            while (digitalRead(BTN_PIN) == LOW); // Wait release
        }
    }
}

ESP32: Electromagnet Control with Current Monitoring

// Electromagnet with current monitoring on ESP32
// Wiring: MOSFET gate→GPIO25, Current sense→GPIO34(ADC)
// Sense resistor: 0.1 ohm in solenoid ground path

#include <Arduino.h>

#define MAGNET_PIN   25   // PWM output to MOSFET gate
#define CURRENT_PIN  34   // ADC input from sense resistor

const int PWM_CHANNEL = 0;
const int PWM_FREQ = 20000;
const int PWM_RESOLUTION = 8;
const float SENSE_RESISTOR = 0.1;  // ohms
const float CURRENT_LIMIT = 2.0;   // amps

void setup() {
    Serial.begin(115200);
    ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
    ledcAttachPin(MAGNET_PIN, PWM_CHANNEL);
    analogReadResolution(12);
    Serial.println("Electromagnet controller ready");
}

float readCurrent() {
    int raw = analogRead(CURRENT_PIN);
    float voltage = (raw / 4095.0) * 3.3;
    return voltage / SENSE_RESISTOR;
}

void setMagnet(int duty) {
    float current = readCurrent();
    if (current > CURRENT_LIMIT) {
        ledcWrite(PWM_CHANNEL, 0);
        Serial.printf("OVERCURRENT: %.2f A - disabled!\n", current);
        return;
    }
    ledcWrite(PWM_CHANNEL, duty);
    Serial.printf("Duty: %d/255, Current: %.2f A\n", duty, current);
}

void loop() {
    // Full power for 2 seconds
    Serial.println("Engaging electromagnet");
    setMagnet(255);
    delay(100);

    // Reduce to hold power
    setMagnet(120);
    delay(4900);

    // Release
    Serial.println("Releasing");
    setMagnet(0);
    delay(3000);
}

Real-World Applications

Security & Access

  • Electric door strikes and deadbolts
  • Locker and cabinet locks
  • Turnstile gate mechanisms
  • Safe locking bolts

Industrial & Automotive

  • Solenoid valves (water, gas, pneumatic)
  • Fuel injectors (automotive)
  • Vending machine dispensers
  • Sorting and diverting mechanisms

Advantages vs. Alternatives

vs. ActuatorSolenoid AdvantageSolenoid Disadvantage
DC MotorInstant linear motion, no gears needed, simplerNo continuous rotation, limited stroke
Linear ActuatorMuch faster response (5–15 ms), simpler, cheaperVery short stroke (3–30 mm), no precise positioning
Pneumatic CylinderNo air supply needed, electrical control onlyLower force, shorter stroke
RelayPhysical motion for mechanical actuationHigher power consumption for equivalent switching

Limitations & Considerations

  • Short Stroke: Typically 3–30 mm. For longer travel, use a linear actuator instead.
  • Binary Action: Standard solenoids are either on or off with no intermediate positioning (proportional solenoids exist but are specialized).
  • Heat Generation: Continuous energization causes significant heating. Monitor duty cycle limits carefully.
  • Inductive Kickback: De-energizing generates voltage spikes of 10–100× the supply voltage. Always use flyback diodes and suppress with RC snubbers for sensitive circuits.
  • Audible Click: Solenoid actuation produces a loud mechanical click. Not suitable for noise-sensitive environments without dampening.
  • Force Varies with Position: Pulling force increases as the plunger is drawn in. This non-linear force profile must be accounted for in design.