Driving Motors and Actuators
Driving DC motors, steppers, servos, solenoids and relays from a microcontroller: PWM, H-bridges, MOSFETs, flyback protection and current sensing.
On this page
- What a pin cannot drive
- Brushed DC motors
- PWM speed control
- H-bridges and direction
- Choosing and driving the MOSFET
- Inductive loads and flyback protection
- Stepper motors and drivers
- Hobby servos and brushless motors
- Solenoids, valves and relays
- Current sensing and protection
- Power and wiring practice
- Common faults
- FAQ
What a pin cannot drive
A microcontroller pin can supply a few milliamperes to tens of milliamperes. A small motor draws hundreds of milliamperes, and a solenoid or a larger motor several amperes. The pin therefore never drives the load directly: it switches a driver (a transistor, a MOSFET or a driver chip) that carries the current from a separate supply. The driver needs a path for the current that an inductive load will try to keep flowing when it is switched off, and it needs to be able to cope with the stall current.
| Load | Typical current | Typical driver | Notes |
|---|---|---|---|
| LED | 2–20 mA | The pin through a resistor | See Diodes and LEDs |
| Small relay coil, buzzer | 30–100 mA | NPN or logic-level MOSFET, with a flyback diode | See Inductors, Transformers and Relays |
| Small DC motor, fan | 0.1–2 A | MOSFET or a small H-bridge chip | Flyback path, supply decoupling |
| Solenoid, valve | 0.3–3 A | Logic-level MOSFET with a flyback diode | Pull-in current is higher than hold current |
| Stepper motor | 0.3–3 A per phase | Stepper driver chip or module | Set the current limit |
| Hobby servo | 0.1–1 A (more at stall) | The servo’s own electronics: a pulse signal | Power it from its own supply, not the MCU pin |
| Brushless motor | 1–50 A | ESC or a three-phase driver | Needs commutation: use a ready-made controller |
| Large motors, heaters | Over 5 A or mains voltage | Contactor, SSR or a drive | Follow the safety rules |
Brushed DC motors
A brushed DC motor turns faster with more voltage and gives more torque with more current. As it turns it also acts as a generator, producing a back-EMF that opposes the supply and is proportional to speed. The current is therefore (V − back-EMF) ÷ Rwinding: highest when the motor is stopped (the stall current) and lower as it speeds up.
Worked example: stall current and running current
- A motor on 12 V with a winding resistance of 2.4 Ω draws a stall current of 12 ÷ 2.4 = 5 A: the driver, the wiring and the supply must survive that, even if the motor usually runs at far less.
- Running at 0.8 A, the voltage across the winding resistance is 0.8 × 2.4 = 1.92 V, so the back-EMF is 12 − 1.92 = 10.08 V and the winding dissipates 1.54 W.
- A stalled motor gets hot quickly: at 5 A the winding dissipates 60 W. Limit the current or detect the stall (see below).
PWM speed control
Switching the supply on and off quickly (pulse width modulation) gives the motor an average voltage equal to the duty cycle times the supply, without the heat that a linear control would waste. The motor’s inductance smooths the current. Choose a frequency above hearing (about 20 kHz) for quiet running; lower frequencies (1–5 kHz) whine, and very high ones waste energy in switching.
Worked example: the average voltage
A 12 V supply at a duty cycle of 40 percent gives an average of 12 × 0.4 = 4.8 V. Microcontroller PWM channels usually have 8 to 16 bits of resolution: with 8 bits there are 256 steps, so each step is 12 ÷ 256 = 0.047 V of average voltage; at 16 bits the steps are far finer but the maximum frequency falls.
H-bridges and direction
Reversing a motor means reversing the current through it. An H-bridge has four switches arranged round the motor: turning on the diagonal pair Q1 and Q4 sends current one way, Q2 and Q3 the other way. Turning on both switches of one leg (Q1 with Q2, or Q3 with Q4) shorts the supply: the fault called shoot-through. Driver chips prevent it with a dead time (a short pause) when changing state.
| Inputs (typical dual-input driver) | Motor | Notes |
|---|---|---|
| IN1 high, IN2 low | Forward | Speed by PWM on one input |
| IN1 low, IN2 high | Reverse | PWM on the other input |
| Both low | Coast (free-wheel) | The motor spins down by itself |
| Both high | Brake | The motor terminals are shorted together: it stops quickly |
- Use a driver chip or module (for example DRV8871, TB6612, L298N for old designs) for loads up to a few amperes; it contains the switches, dead time, protection and often current limiting.
- Read the current rating as the continuous current with the heat sink or copper area given in the datasheet, not the peak.
- Logic level. Check that the driver accepts the 3.3 V logic from your microcontroller.
- Build your own bridge only if you must. N-channel high-side switches need a gate driver or a bootstrap circuit; shoot-through is a classic way to destroy transistors.
Choosing and driving the MOSFET
For a single direction, one N-channel logic-level MOSFET switching the load to ground is enough (see Transistors). Pick a part whose RDS(on) is specified at your gate voltage (for example 3.3 V or 4.5 V), whose VDS rating is at least twice the supply, and whose current rating covers the stall current. A gate resistor (10–100 Ω) limits the gate charging current; a pull-down (10 kΩ) keeps it off while the microcontroller starts.
Worked example: heat in the MOSFET
- Conduction loss: a MOSFET with RDS(on) = 10 mΩ carrying 5 A dissipates I² × R = 5² × 0.01 = 0.25 W: warm, but fine on a small copper area.
- Switching loss: with 20 kHz PWM and a transition time of 100 ns, each edge dissipates about ½ × V × I × t, so the average is ½ × 12 × 5 × 2 × 100 ns × 20 kHz = 0.12 W. A slow gate drive (or a high PWM frequency) raises this.
- The total is about 0.37 W. With a junction-to-ambient thermal resistance of 60 °C/W the part runs about 22 °C above ambient.
Inductive loads and flyback protection
A coil, a motor or a solenoid stores energy in its magnetic field. When the switch opens, the current cannot stop at once, and the voltage across the coil rises until something conducts: the switch (and it breaks down) or a diode you provided. A flyback diode across the load, with its cathode to the positive side, gives the current a safe loop. It is mandatory with every relay, solenoid and motor switched by a transistor.
Worked example: the energy in a relay coil
- A 12 V relay coil draws 30 mA, so its resistance is 12 ÷ 0.03 = 400 Ω. Suppose its inductance is 0.5 H.
- The stored energy is ½ × L × I² = 0.5 × 0.5 × 0.03² = 0.225 mJ.
- Without a diode this energy goes into a voltage spike of hundreds of volts that destroys the transistor or resets the microcontroller. With a 1N4007 or 1N4148 across the coil the voltage clamps at the supply plus about 0.7 V, the current decays through the diode, and the coil releases in a few milliseconds.
- For faster release add a Zener in series with the diode (a higher clamp voltage) or use a TVS diode; the transistor must then withstand that clamp voltage.
Stepper motors and drivers
A stepper motor moves in fixed steps (typically 1.8°, or 200 steps per revolution) as its coils are energized in sequence. It holds its position without feedback as long as it is not overloaded. A stepper driver chip takes step and direction pulses, produces the coil currents, limits the current and can divide each full step into microsteps for smoother motion.
Worked example: step rate and current setting
- With 200 steps per revolution and 16× microstepping, one revolution is 200 × 16 = 3 200 microsteps (0.113° each).
- To turn at 600 rpm (10 revolutions per second) the controller must send 10 × 3 200 = 32 000 steps per second (32 kHz). Check that the microcontroller can generate it and that the motor can follow: stepper torque falls as speed rises, and a stall means lost steps.
- Current limit: a driver like the A4988 sets the coil current with a reference voltage, I = Vref ÷ (8 × Rsense). With Rsense = 0.068 Ω and Vref = 0.544 V the limit is 1.0 A per coil. Set it to the motor’s rated current or a little below (the formula differs between driver chips: use the datasheet).
- Supply voltage. A higher supply voltage (12–24 V) lets the current rise faster at speed; the driver’s current limit keeps it safe.
- Do not connect or disconnect a motor with the supply on. The voltage spike can destroy the driver.
- Decay mode, microstepping and cooling affect noise and heat: read the driver’s datasheet.
- Resonance. Many steppers vibrate at certain speeds: microstepping, a damper or avoiding the speed helps.
Hobby servos and brushless motors
A hobby servo is a small motor, gearbox, position sensor and control circuit in one case. It takes a pulse every 20 ms (50 Hz) whose width sets the angle: 1 ms is one end, 1.5 ms the center, 2 ms the other end (check your servo: many accept 0.5–2.5 ms).
Worked example: pulse width for an angle
- With 1 ms = 0° and 2 ms = 180°, each millisecond of pulse width is 180°, so 90° needs a pulse of 1.5 ms.
- The duty cycle is 1.5 ÷ 20 = 7.5 percent, so a PWM channel at 50 Hz with an 8-bit counter of 256 steps gives a resolution of about 20 ÷ 256 = 0.078 ms per step, or about 14.1°.
- Power the servo separately (typically 5–6 V at an ampere or more) with the grounds joined; a moving servo draws current spikes that reset a microcontroller on the same supply.
- Add a bulk capacitor (470–1000 µF) across the servo supply.
- Continuous-rotation servos use the pulse for speed and direction, not angle.
- Brushless motors need three-phase commutation. Use an ESC (electronic speed controller, which takes a servo-style pulse) or a motor driver with sensorless or Hall-sensor control; do not try to drive them with an H-bridge.
Solenoids, valves and relays
A solenoid needs a large current to pull in but much less to hold. A PWM economiser drives it at full voltage for the first 50–100 ms, then drops to a duty cycle of about 30 percent, which saves heat and power.
Worked example: economiser savings
A 24 V solenoid draws 1 A, so pulling in dissipates 24 W. Held at 30 percent duty it averages 7.2 W, 70 percent less. The coil runs much cooler, so it can be held on indefinitely. Check the solenoid’s datasheet for the minimum holding current and the duty cycle at which it stays closed.
- Relays switch higher power or mains loads with isolation: see Inductors, Transformers and Relays for coil drive, flyback and contact ratings.
- Solid-state relays have no contacts but drop a volt or so and need a heat sink; use a zero-cross type for resistive AC loads and a suitable type for motors.
- Valves and pumps follow the same rules: a MOSFET, a flyback diode and a separate supply.
Current sensing and protection
Measuring a load’s current protects the driver and the load and detects a stall. The simplest sensor is a shunt resistor in the ground return; the voltage across it is read by an ADC pin or a comparator, or amplified by a current-sense amplifier.
Worked example: a shunt for 5 A
- A 50 mΩ shunt carrying 5 A drops 5 × 0.05 = 0.25 V and dissipates 5² × 0.05 = 1.25 W: use a 2 W part, and keep in mind the voltage lost.
- A smaller shunt wastes less but gives a smaller signal: a 10 mΩ shunt gives only 0.05 V, so amplify it with a current-sense amplifier (gain 20–100).
- With a 3.3 V, 12-bit ADC the resolution is 3.3 ÷ 4096 = 0.8 mV, so the 0.25 V signal resolves about 310 steps, or 16.1 mA per step.
- Stall detection: a current well above the running current for more than a fraction of a second means the motor is blocked: stop the drive.
- Fuse or current-limited supply for the first test of any motor circuit.
- Thermal protection: many driver chips shut down when hot: a motor that stops and restarts every few seconds is a sign of overheating or overcurrent.
- Limit switches and end-stops (with the pull-ups and debounce in Microcontroller Electronics) stop travel before the mechanism is damaged.
Power and wiring practice
- Separate the motor supply from the logic supply where you can, joined at one ground point, or use a regulator with good filtering for the logic.
- Put a bulk capacitor (a few hundred microfarads to a millifarad) near the driver to supply the current spikes and absorb the energy a braking motor sends back to the supply. For example, a 5 A step lasting 100 µs with 0.5 V of allowed droop needs about 1 000 µF.
- Keep high-current loops short and the wires thick enough. Power traces and wires carry amperes: see the trace-width table on PCB Design and Layout.
- Add a small capacitor (10–100 nF) across the motor terminals to cut the brush noise that interferes with radios and sensors.
- Twist the motor wires and keep them away from signal wires; shield if needed.
- A battery that is nearly flat causes brown-outs when the motor starts: use a supply that can deliver the stall current, and a supervisor reset on the microcontroller.
- Regenerative energy from a decelerating motor can raise the supply voltage: a battery absorbs it, a plain power supply does not, so a bulk capacitor or a brake resistor may be needed.
Common faults
| Symptom | Likely cause | Try |
|---|---|---|
| Microcontroller resets when the motor starts | Supply droop from the starting current; noise | Separate supplies, bulk capacitor, decoupling, a thicker supply lead |
| MOSFET gets very hot | Not enough gate voltage (partly on), stall current, too high a PWM frequency | Use a logic-level part with a stated RDS(on) at your gate voltage; check the stall current; slow the PWM |
| Driver dead after the first test | No flyback diode, shoot-through, a spike from connecting the motor live | Add the diode; check the dead time; connect with the power off |
| Motor only runs in one direction | A half-bridge not working, a wrong input state | Check each input with a scope and the truth table |
| Stepper buzzes and does not move | Wrong coil pairing, too high a step rate, too little current | Find the coil pairs with a meter; slow down; raise the current limit |
| Stepper loses steps | Current too low, acceleration too fast, load too high | Ramp the speed, raise the current, use a larger motor |
| Servo jitters | Noisy supply, noisy pulse, power shared with the MCU | Separate supply and a bulk capacitor; stabilize the PWM output |
| Motor runs but weakly | Low supply voltage, voltage drop in thin wire, a weak battery | Measure the voltage at the motor under load |
| Motor noise upsets sensors | Brush noise and switching spikes | Capacitor across the motor, ferrites, separate grounds joined at one point, shielded cable |
Related
- Power and Motor Electronics: the overview of this guide.
- Transistors: the MOSFET and BJT basics behind these drivers.
- Inductors, Transformers and Relays: relays, coils and flyback.
- Batteries and Power Supplies: supplies, batteries and regulators.
- Microcontroller Electronics: pins, PWM and ADC on the controller side.
- Motion Control and Actuators: industrial drives, servos and actuators.
- Ohm’s Law and Power Calculator.
FAQ
Why does my motor driver get hot?
Heat comes from the on-resistance of its switches (I² × R), from switching losses, and from running the motor near stall. Check the current, give the chip copper area or a heat sink, and lower the PWM frequency if the switching loss is large.
Do I always need a flyback diode?
For any inductive load switched by a transistor, yes: relay, solenoid, motor, fan. Many motor driver chips have the diodes built in; check before you rely on that.
What PWM frequency should I use?
About 20 kHz for DC motors to be above hearing, 1–5 kHz for solenoid economisers and LEDs where flicker is not a problem, and 50 Hz for hobby servos.
Can I power a motor from the microcontroller board’s 5 V pin?
Not usually: the board’s regulator is not rated for motor currents, and the starting current resets the microcontroller. Use a separate supply with a common ground.
Stepper or servo?
A stepper is simple, holds position and needs no feedback, but loses steps if overloaded and is noisy at speed. A servo (with feedback) is smoother and stronger for its size, and detects a stall.
References
- Allegro Microsystems, A4988 DMOS microstepping driver datasheet; Texas Instruments, DRV8871 and DRV8825 datasheets; Toshiba TB6612FNG datasheet.
- Infineon, onsemi and Vishay MOSFET datasheets (RDS(on), gate charge and thermal resistance) and application notes on motor drive.
- Horowitz, P. and Hill, W., The Art of Electronics, 3rd ed., Cambridge University Press: chapters on transistors and power control.
- Hobby servo and ESC manufacturers’ documentation for pulse timing.
Last reviewed: October 2026. Values are typical: check the datasheets of the parts you use. Do not work on live mains equipment unless you are qualified; keep fingers away from moving parts.
