Driving Loads

15 min read Last updated Fri Jun 12 2026 06:46:09 GMT+0000 (Coordinated Universal Time)

Driving LEDs

An LED is a current-driven device. Applying voltage without limiting current destroys the LED and can damage the GPIO pin.

VfV_f is the forward voltage: the minimum voltage required for conduction. Below VfV_f the LED is off. Above VfV_f, current rises steeply with any small voltage increase.

Typical forward voltages by color:

  • Red / yellow: 1.8 V\approx 1.8\ \text{V}2.2 V2.2\ \text{V}
  • Green / blue / white: 3.0 V\approx 3.0\ \text{V}3.5 V3.5\ \text{V}

Current-Limiting Resistor

A series resistor between the GPIO pin and the LED limits current to a safe level.

R=VCCVfIfR = \frac{V_{CC} - V_f}{I_f}

Here:

  • VCCV_{CC}: supply voltage
  • VfV_f: LED forward voltage
  • IfI_f: desired forward current

Typical IfI_f for indicator LEDs is 5 mA5\ \text{mA}20 mA20\ \text{mA}. MCU GPIO pins are rated for 8 mA8\ \text{mA}25 mA25\ \text{mA} depending on the device. IfI_f must stay within this limit.

Transistor Driver

When IfI_f exceeds the GPIO current rating, or when driving multiple LEDs, a transistor is placed between the GPIO pin and the LED. The pin drives the transistor base. The transistor supplies current from VCCV_{CC} directly, bypassing the pin’s drive limit.

The GPIO pin sources only IB=If/hFEI_B = I_f / h_{FE}, well within pin limits.

Driving 7-Segment Displays

A 7-segment display is 7 individual LEDs (segments a–g) arranged to form digits. Each segment is an independent LED requiring its own current path.

2 variants exist:

  • Common cathode
    All segment cathodes tied to GND. MCU drives a segment HIGH to light it.
  • Common anode
    All segment anodes tied to VCCV_{CC}. MCU drives a segment LOW to light it (active-low logic).

Per-Segment Resistor

Placing a single resistor in the common line (shared by all segments) causes the current through it to change with the number of active segments.

When nn segments are on, total current through the shared resistor is:

Itotal=n×IfI_{total} = n \times I_f

The voltage drop across the resistor:

VR=n×If×RV_R = n \times I_f \times R

Voltage remaining for each segment:

Vsegment=VCCVR=VCCn×If×RV_{segment} = V_{CC} - V_R = V_{CC} - n \times I_f \times R

As nn increases, VsegmentV_{segment} falls. With enough segments active, VsegmentV_{segment} drops below VfV_f, extinguishing segments entirely.

Each segment gets its own series resistor to fix IfI_f independently of how many other segments are active.

Multiplexing Multiple Digits

Driving each segment of each digit directly requires 7×N7 \times N GPIO pins for NN digits. Multiplexing reduces this to 7+N7 + N pins.

Each digit’s corresponding segment pins are wired together across all digits. A separate digit-select line controls a transistor that connects one digit’s common line to GND (common cathode) or VCCV_{CC} (common anode). Only the selected digit’s transistor is active at any time.

Segment pins (a–g) ──────────────────────────────┐

         Digit 1 common ── transistor ── GND      │
         Digit 2 common ── transistor ── GND      ├── shared segments
         Digit 3 common ── transistor ── GND      │
         Digit 4 common ── transistor ── GND      │
                               ↑                  │
                    digit-select GPIO              │

Scan sequence per cycle:

  1. Disable all digit-select lines (all digits off).
  2. Write segment pattern for digit kk to the 7 segment GPIO pins.
  3. Enable digit kk‘s select line.
  4. Hold for the slot duration tslott_{slot}.
  5. Advance kk to the next digit and repeat.

For NN digits, each digit is on for 1/N1/N of the total cycle. Refresh rate per digit must exceed 50 Hz50\ \text{Hz} to avoid visible flicker:

frefresh=1N×tslot50 Hzf_{refresh} = \frac{1}{N \times t_{slot}} \geq 50\ \text{Hz}

The scan loop runs from a timer ISR.

Ghosting

Ghosting occurs when residual segment current lights the wrong digit during a select transition. The cause: segment pins still hold the previous digit’s pattern at the moment the next digit’s select line activates.

Fix: always blank all segment pins (drive all LOW) before enabling the next digit-select line. The blank period adds dead time per slot but eliminates ghost images.

Driving Inductive Loads

Inductive loads (motors, relays, solenoids) store energy in a magnetic field while current flows. When the driving transistor switches off, the magnetic field collapses and induces a voltage spike (back-EMF) in the opposite polarity to the supply.

Vspike=LdIdtV_{spike} = -L \frac{\text{d}I}{\text{d}t}

Spike amplitude depends on LL and dI/dt\text{d}I/\text{d}t. A fast switch-off generates spikes of tens to hundreds of volts, above the MCU’s rated supply. Without protection, the spike destroys the transistor and can permanently damage the MCU.

Flyback Diode

A diode placed in parallel with the inductive load, reverse-biased under normal operation. When the switch opens and back-EMF reverses polarity, the diode becomes forward-biased and provides a recirculating current path. The stored energy dissipates through the coil resistance rather than spiking onto the supply rail.

V_CC ──┬──────────────┐
       │              │ ← flyback diode (cathode to V_CC)
   [inductor]        ─┼─
       │              │
     [transistor]     │
       │              │
GND ───┴──────────────┘

The diode must be placed physically close to the load. A long lead between the load and the diode allows the spike to propagate before the diode clamps it.

Schottky diodes are preferred over silicon for fast loads (PWM motor control). Lower forward voltage and faster recovery reduce power dissipation during high-frequency switching.

Transistor Driver Requirement

MCU GPIO pins cannot drive inductive loads directly. 2 reasons:

  • Current
    Relay coils and motor windings draw 50 mA50\ \text{mA}500 mA500\ \text{mA}, far above GPIO limits.
  • Voltage
    Many inductive loads operate at 12 V12\ \text{V} or 24 V24\ \text{V}. GPIO pins are rated to 3.3 V3.3\ \text{V} or 5 V5\ \text{V}.

An NPN transistor or N-channel MOSFET is used as the switch. The GPIO pin drives the base or gate. The transistor switches the full load current from the external supply.

DC Motors

A DC motor converts electrical energy to rotational motion. Current through the armature winding produces a magnetic field that reacts against the stator field, generating torque.

Speed is proportional to average voltage. PWM controls speed by varying duty cycle. Motor inertia smooths the switching pulses.

Direction is controlled by reversing current polarity. An H-bridge arranges 4 switches to connect the motor in either supply polarity.

V_CC ──┬─────────────┬──
       Q1(high-left) Q3(high-right)
       │             │
       ├────[motor]──┤
       │             │
       Q2(low-left)  Q4(low-right)
GND ───┴─────────────┴──

Operating Modes

  • Forward
    Q1 + Q4 on, Q2 + Q3 off. Current flows left to right through the motor.
  • Reverse
    Q2 + Q3 on, Q1 + Q4 off. Current flows right to left through the motor.
  • Brake
    Q2 + Q4 on, Q1 + Q3 off. Both motor terminals shorted to GND. The motor’s back-EMF drives current through the winding, producing a braking torque that resists rotation.
  • Coast
    All switches off. Motor terminals float. Motor spins freely with no braking force.

Shoot-Through

Turning on Q1 and Q2 simultaneously (or Q3 and Q4) shorts VCCV_{CC} to GND and instantly destroys the switches.

Shoot-through occurs during switching transitions. If Q1 turns on before Q2 fully turns off, both conduct briefly. A dead time (both switches off) is inserted between turning one switch off and the other on. Dead time is typically 100 ns100\ \text{ns}1 μs1\ \mu\text{s} depending on transistor turn-off speed.

PWM Speed Control

PWM is applied to one or both switches on the active side while the opposite low-side switch stays on. The motor sees the average voltage, which scales with duty cycle.

A flyback diode across each transistor is required. Every PWM transition collapses the motor’s inductive field, generating back-EMF. H-bridge ICs include these diodes internally.

Dedicated H-bridge ICs (L298N, DRV8833, TB6612) integrate the 4 transistors, flyback diodes, dead-time control, and logic-level inputs in a single package, accepting direct GPIO drive.

Tachometer

A tachometer measures rotational speed and converts it to an electrical signal the MCU can read. Common implementations:

  • Optical encoder
    A slotted disc attached to the motor shaft interrupts a light beam. Each slot produces 1 pulse per revolution. A disc with NN slots produces NN pulses per revolution.
  • Hall effect sensor
    A magnet on the shaft triggers a Hall sensor on each pass. Contactless. Tolerates dust and oil.
  • Back-EMF sensing
    A spinning DC motor generates a voltage proportional to speed. Measured during PWM off-time when the driver is not switching.

The MCU measures tachometer pulse frequency using a timer in counter or input-capture mode. Speed in RPM:

RPM=fpulseN×60\text{RPM} = \frac{f_{pulse}}{N} \times 60

Here:

  • fpulsef_{pulse}: pulse frequency in Hz
  • NN: pulses per revolution

Closed-Loop Speed Control

Open-loop control sets PWM duty cycle without measuring actual speed. Load changes, supply voltage variation, and motor temperature alter speed with no correction.

Closed-loop control feeds the tachometer reading back to the MCU. The MCU compares measured speed to the setpoint and adjusts the PWM duty cycle to reduce the error.

setpoint ──► [+] ──► controller ──► PWM ──► H-bridge ──► motor
              │                                              │
              └──────────────── tachometer ◄────────────────┘

A PID controller computes the correction from 3 terms:

u(t)=Kpe(t)+Kie(t)dt+Kddedtu(t) = K_p\, e(t) + K_i \int e(t)\, \text{d}t + K_d \frac{\text{d}e}{\text{d}t}

Here:

  • e(t)e(t): error (setpoint minus measured speed)
  • KpK_p: proportional gain (correction proportional to current error)
  • KiK_i: integral gain (correction for accumulated error, eliminates steady-state offset)
  • KdK_d: derivative gain (correction proportional to rate of error change, damps overshoot)

The controller output u(t)u(t) maps to PWM duty cycle. On each control loop iteration (driven by a timer ISR), the MCU reads the tachometer, computes the error, updates the PID terms, and writes the new duty cycle to the PWM register.

Stepper Motors

A stepper motor moves in discrete angular steps. Each pulse advances the shaft by a fixed step angle. Position is controlled open-loop with no encoder required.

Steps per revolution=360°θstep\text{Steps per revolution} = \frac{360°}{\theta_{step}}

A common step angle is 1.8°1.8°, giving 200 steps per revolution.

Types

Stepper motors are classified by 2 independent axes: rotor construction and winding configuration.

By Rotor Construction
  • Permanent Magnet (PM)
    Rotor is a permanent magnet. Stator coils attract and repel the rotor poles. Large step angle: typically 7.5°7.5° or 15°15° (48–24 steps per revolution). Low cost. Used in low-precision applications such as paper feed in printers.
  • Variable Reluctance (VR)
    Rotor is a toothed soft-iron cylinder with no permanent magnet. Torque is produced by the rotor teeth aligning with the energised stator poles to minimise magnetic reluctance. Step angle as small as 1.8°1.8°. No detent torque when unpowered. Rotor spins freely.
  • Hybrid
    Combines a permanent magnet rotor with toothed stator and rotor poles. The magnet provides detent torque and enhances holding torque. The teeth provide fine angular resolution. Step angle: 0.9°0.9°1.8°1.8°. Highest torque and precision of the 3 types. Most common in industrial and 3D printing applications.
By Winding Configuration
  • Unipolar
    Each coil has a center tap, splitting it into 2 half-windings. Current flows through one half-winding at a time, always in the same direction. The center tap connects to the supply. A single transistor switches each half-winding to GND. 5 wires (4 half-winding ends + 1 common center tap) or 6 wires (separate center taps per coil). A transistor array such as the ULN2003 suffices as the driver. Torque is lower than bipolar because only half the winding is active per step.
  • Bipolar
    2 coils with no center tap. Current direction is reversed through the full winding to produce each magnetic pole. 4 wires (2 per coil). Full winding is active at every step, giving higher torque per frame size than unipolar. Requires an H-bridge per coil to reverse current direction.

Excitation Sequences

The coils must be energised in a specific sequence to advance the rotor. For a 2-coil bipolar motor with coils A and B:

StepCoil ACoil B
1++
2+
3
4+
  • Full-step (2-phase on)
    Both coils energised at each step as above. Maximum torque.
  • Full-step (wave drive)
    1 coil energised at a time. Lower torque. Used to reduce power consumption.
  • Half-step
    Alternates between 1-coil and 2-coil states. Doubles angular resolution to 0.9°0.9° on a 1.8°1.8° motor at the cost of varying torque per step.
  • Microstepping
    Driver varies current amplitude in both coils sinusoidally. Rotor positions between full steps are held by the ratio of currents. Greatly reduces vibration and increases smoothness. Common ratios are 1/8, 1/16, 1/32 step.

Driver Circuits

  • Unipolar driver
    A transistor array (e.g. ULN2003) switches each half-winding to GND. The center tap connects to the supply. 4 GPIO pins control 4 half-windings directly.
  • Bipolar driver
    Requires 1 H-bridge per coil (2 H-bridges total for a standard 2-coil motor). A dedicated stepper driver IC (A4988, DRV8825) integrates both H-bridges, microstepping current control, and overcurrent protection. The MCU sends only a STEP pulse and a DIR level. The IC handles the excitation sequence internally.

Step Rate and Stall

Speed is proportional to step pulse frequency:

ω=θstep×fstep\omega = \theta_{step} \times f_{step}

Stepping too fast from standstill stalls the rotor. Step frequency must ramp up from a low starting rate.

Electromagnetic Relays

An EM relay uses a current-driven coil to mechanically actuate a switch. The coil and the switched contacts are electrically isolated. The MCU circuit and the switched load share no common ground.

Structure:

  • Coil
    Energised by the MCU via a transistor driver. Draws 10 mA10\ \text{mA}100 mA100\ \text{mA} at 5 V5\ \text{V} or 12 V12\ \text{V}. Coil current must be high enough to generate sufficient magnetic force to pull the armature, and low enough to prevent resistive heating in the coil winding. Both bounds are specified in the relay datasheet as the pull-in current and the maximum continuous coil current.
  • Armature
    Ferromagnetic plate pulled toward the coil core when energised.
  • Contacts
    Mechanically linked to the armature. Rated independently of the coil, typically 5 A5\ \text{A}30 A30\ \text{A} at 250 V250\ \text{V} AC or 30 V30\ \text{V} DC. The voltage rating is the maximum voltage the open contacts can block without arcing across the gap. Exceeding it causes the contacts to arc on open, eroding the contact surface and eventually welding them shut.

The coil is an inductive load requiring a flyback diode across its terminals. The diode clamps back-EMF on switch-off and dissipates stored energy through the coil’s own resistance.

V_CC ──┬──────────┐
       │          │ (cathode)
    [coil]       ─┼─  shunt diode
       │          │ (anode)
    [transistor]  │
       │          │
GND ───┴──────────┘

Place the diode physically close to the coil. Parasitic inductance in long PCB traces allows the spike to propagate before clamping. A Schottky or fast-recovery diode is preferred.

Single Transistor Driver

An NPN transistor switches the relay coil. The GPIO pin sources a small base current. The transistor sinks the full coil current through its collector.

V_CC ── [coil] ──── collector
                    NPN
GPIO ── [R_B] ───── base
                    emitter ── GND

Base resistor RBR_B limits base current to a safe level while keeping the transistor in saturation:

RB=VGPIOVBEIBVGPIO0.7Icoil/hFER_B = \frac{V_{GPIO} - V_{BE}}{I_B} \approx \frac{V_{GPIO} - 0.7}{I_{coil} / h_{FE}}

Choosing IBI_B to overdrive the transistor (hFEh_{FE} derated by a factor of 5–10) ensures it stays in saturation across temperature and device variation.

Simple and low-cost. Suitable when the GPIO can source sufficient base current and the coil voltage equals the MCU supply.

Dual Transistor Driver

2 NPN transistors in a Darlington configuration. The emitter of the first (driver) transistor feeds the base of the second (output) transistor. The combined current gain is the product of the 2 stages:

hFE(total)=hFE1×hFE2h_{FE(total)} = h_{FE1} \times h_{FE2}
GPIO ── [R_B] ── base(Q1)
                 NPN Q1
                 emitter(Q1) ── base(Q2)
                                NPN Q2
                 collector(Q2) ── [coil] ── V_CC
                 emitter(Q2) ── GND

A very small GPIO current drives a relay coil that a single transistor could not switch reliably. Used when:

  • The coil current is high relative to the GPIO drive capability
  • The MCU operates at a lower voltage than the coil supply, reducing available base drive

Trade-off: the 2 VBEV_{BE} drops in series raise the saturation voltage to 1.4 V\approx 1.4\ \text{V}, slightly reducing the voltage across the coil.

Darlington arrays such as the ULN2003 integrate 7 Darlington pairs with built-in base resistors and shunt diodes in a single IC, replacing discrete transistor circuits for multi-relay designs.

Contact types:

  • NO (Normally Open)
    Open when coil is off. Closes when energised.
  • NC (Normally Closed)
    Closed when coil is off. Opens when energised.
  • SPDT
    Provides both NO and NC on 1 common terminal.
Was this helpful?