System Design

Design a Motor Control System

System design walkthrough: BLDC motor controller with closed-loop speed control, FOC algorithm, real-time ISR architecture, and safety interlocks.

The Prompt

"Design a brushless DC motor controller with closed-loop speed control for an industrial fan. Requirements: 1000-10,000 RPM range, speed accuracy within 2%, CAN bus for setpoint commands, emergency stop input."

This is a classic embedded system design question that tests your ability to combine real-time control theory, power electronics awareness, ISR architecture, and safety thinking into a coherent design.


Requirements Clarification

Before sketching any architecture, clarify the requirements with the interviewer. Here is what a thorough requirements table looks like after the clarification phase:

Functional Requirements

RequirementDetail
Speed range1,000 - 10,000 RPM
Speed accuracyWithin 2% of setpoint at steady state
Speed command interfaceCAN bus (CAN 2.0B, 500 kbps)
Emergency stopDedicated hardware input (active-low, normally closed)
Status reportingMotor speed, current, temperature, fault status via CAN
Startup behaviorControlled ramp-up, no inrush current spikes
BrakingControlled deceleration on stop command, immediate coast on E-stop

Non-Functional Requirements

CategoryRequirementRationale
Real-timeControl loop latency under 50 us20 kHz FOC loop for smooth commutation
SafetyE-stop response under 10 us (hardware path)Industrial safety, E-stop must bypass firmware
Power48V DC bus, motor rated at 500WTypical industrial BLDC rating
ReliabilityContinuous operation, 50,000 hour lifetimeIndustrial fan runs 24/7
Temperature-20C to +60C ambient, motor winding limit 120CFan mounted in industrial environment
EMCMust not radiate above EN 55011 Class B limitsPWM switching is a major EMI source
CostBOM target under $25 at 10K volumeCompetitive industrial pricing

Architecture Overview

text
CAN Bus (500 kbps)
|
v
+-------+ +----------+ +-+----------+ +--------+ +-------+ +-------+
| Hall | | Encoder | | | | Gate | | 3-Ph | | |
|Sensors+->+ / Speed +->+ MCU +->+Driver +->+MOSFET +->+ BLDC |
| (3x) | | Calc | | | | (Hi/Lo)| |Bridge | | Motor |
+-------+ +----------+ +-----+------+ +--------+ +-------+ +-------+
^ ^ ^
| | |
+------+ | +--------+
| | |
+----+---+ +---+----+ +-----+-----+
|Current | |Temp | | E-Stop |
|Sense | |Sensor | | (HW latch)|
|(2 ph) | |(NTC) | | |
+--------+ +--------+ +-----------+

Key architectural decisions visible in this diagram:

  • Three Hall sensors for rotor position feedback (6-step commutation or FOC)
  • Two-phase current sensing (third phase calculated by Kirchhoff's law: Ic = -Ia - Ib)
  • Hardware E-stop path that disables the gate driver directly, independent of firmware
  • Temperature monitoring on both the motor (NTC on winding) and the power stage (NTC on MOSFET heatsink)

Component Deep Dive

Position and Speed Sensing

Three approaches exist for BLDC rotor position sensing. The choice affects cost, complexity, and performance:

MethodHow It WorksResolutionCostComplexityBest For
Hall sensors (3x)Detect rotor magnet polarity, produce 6 commutation edges per electrical revolution60 degrees electricalLow ($0.30 each)Low — simple GPIO interrupts6-step commutation, industrial fans
Incremental encoderOptical or magnetic, 100-4096 pulses/rev with quadrature (A/B) + index (Z)0.09 - 3.6 degrees mechanicalMedium ($5-$20)Medium — timer in encoder modeFOC, precision speed control
Sensorless (back-EMF)Detect zero-crossing of undriven phase back-EMFDepends on algorithm, typically 30 degrees electricalZero (no sensor)High — signal processing, fails at low RPMCost-sensitive, high-speed fans

Decision for this design: Hall sensors. They meet the 2% speed accuracy requirement, work across the full RPM range including startup, cost less than a dollar total, and are standard in industrial BLDC fans. If the interviewer asks about higher precision, mention that adding an encoder enables FOC with better torque ripple performance.

Speed calculation from Hall sensors:

text
Speed (RPM) = 60 / (time_between_hall_edges * number_of_pole_pairs * 6)
Example: 7-pole-pair motor, hall edge every 714 us
RPM = 60 / (714e-6 * 7 * 6) = 60 / 0.02999 = 2001 RPM

Use a timer capture on Hall edge interrupts to measure the period between edges. At 1000 RPM with 7 pole pairs, edges arrive every 1.43 ms — easily measurable with a 1 MHz timer.

Control Loop Architecture

The control system uses cascaded loops — an inner current loop for fast torque response and an outer speed loop for setpoint tracking:

text
Speed +-------+ Current +-------+ Duty +-------+
Setpoint ->+ Speed +-> Setpoint ->+Current+-> Cycle ->+ PWM +-> Motor
| | PID | | | PID | | | Gen |
| +---+---+ | +---+---+ | +-------+
| ^ | ^ |
| | | | |
| +----+----+ | +----+----+ |
+--->+ Speed | +---->+ Current | |
| Feedback| | Feedback| |
+---------+ +---------+ |
Hall/Encoder ADC Shunt |
|
20 kHz ISR handles all of this -+

Two control algorithm options:

AlgorithmDescriptionProsCons
6-step (trapezoidal)Energize two of three phases based on Hall position, PWM on one phaseSimple, low CPU load, works with Hall sensors onlyTorque ripple at commutation transitions, audible noise
FOC (field-oriented control)Transform 3-phase currents to rotating d-q frame, control independentlySmooth torque, better efficiency, lower acoustic noiseHigher CPU load (Park/Clarke transforms), needs current sensing

Decision: Start with 6-step commutation — it is simpler to implement, well-proven for industrial fans, and meets the 2% speed accuracy requirement. Mention FOC as an upgrade path if the interviewer pushes for better performance.

PID tuning approach:

  • Speed PID: proportional + integral (PI controller), loop rate 1 kHz (every 20th FOC cycle)
  • Current PID: proportional + integral, loop rate 20 kHz (every PWM cycle)
  • Anti-windup on integrator to prevent overshoot during setpoint changes
  • Derivative term usually not needed for speed control (Hall quantization noise makes derivative noisy)

PWM Generation

ParameterValueRationale
PWM frequency20 kHzAbove audible range (eliminates motor whine), fast enough for current ripple control
AlignmentCenter-alignedReduces harmonic content vs edge-aligned, natural ADC sampling point at center
Dead-time500 nsPrevents shoot-through (simultaneous high-side and low-side conduction)
Resolution12-bit (4096 steps at 168 MHz)Sufficient for smooth duty cycle control

Why center-aligned PWM matters:

text
Edge-aligned:
____ ____ ____
| | | | | |
_| |______________| |______________| |___
Center-aligned:
____ ____
| | | |
_____| |______________| |_____________
^
|
Best ADC sampling point
(current is most stable here)

Center-aligned PWM creates a symmetrical switching pattern. The midpoint of the ON period is the ideal time to sample phase currents because switching transients have settled.

Safety System

Safety is not optional in motor control. The interviewer expects you to discuss these failure modes:

HazardDetectionResponseResponse Time
OvercurrentAnalog comparator on shunt resistor voltageGate driver shutdown (hardware path)Under 1 us (hardware, no firmware delay)
Stall / locked rotorNo Hall edges for more than 100 ms while PWM is activeDisable PWM, report fault on CAN100 ms
Overtemperature (motor)NTC on motor winding, ADC reading every 100 msReduce speed (derating) or shutdown above 120C100 ms
Overtemperature (FETs)NTC on heatsink, ADC reading every 100 msReduce duty cycle or shutdown above 100C100 ms
DC bus overvoltageResistor divider to ADC, threshold at 60VDisable PWM, engage braking resistor if present50 us (ISR)
DC bus undervoltageResistor divider to ADC, threshold at 36VDisable PWM, report fault50 us (ISR)
E-stopDedicated GPIO, hardware-latched gate driver disableImmediate gate driver shutdown, coast to stopUnder 10 us (hardware)

Critical safety design principle: The E-stop and overcurrent protection must have a hardware path that disables the gate driver without firmware involvement. If the MCU firmware hangs, the hardware safety path still functions.

text
E-Stop Pin (Active Low)
|
v
+-----+------+
| Gate | Firmware cannot override
| Driver +---> this hardware shutdown path
| ENABLE pin |
+---------+---+
^
|
Overcurrent Comparator Output

Communication

InterfacePurposeRatePriority
CAN busSpeed setpoint commands from host controller500 kbps, messages every 10-100 msMedium — processed in main loop or low-priority task
CAN busStatus reporting (speed, current, temperature, faults)Transmit every 100 msLow — best-effort telemetry
UARTDebug console during development115200 baudLowest — disabled in production firmware

CAN message format example:

CAN IDDirectionDataDLC
0x200Host to controllerByte 0-1: target RPM (uint16), Byte 2: command (start/stop/e-stop-reset)3
0x201Controller to hostByte 0-1: actual RPM, Byte 2-3: phase current (mA), Byte 4: temp (C), Byte 5: fault code6

MCU Selection

CandidateCoreFlash / RAMKey PeripheralsFPUPrice (10K)
STM32F302Cortex-M4256 KB / 40 KB3x advanced timers, 4x ADC, CAN, comparatorsYes~$3.50
STM32G474Cortex-M4512 KB / 128 KBHRTIM, 5x ADC, 3x CAN-FD, comparators, DACYes~$4.50
TI TMS320F28069C2000256 KB / 100 KB16x PWM, 16x ADC, CAN, CLA (co-processor)Yes~$5.00

Decision: STM32G474. It has integrated analog comparators for hardware overcurrent protection, advanced timers with center-aligned PWM and dead-time insertion, multiple ADCs for simultaneous current sampling, and CAN-FD. The Cortex-M4 FPU handles PID math in single-precision float without performance penalty. The price fits the $25 BOM target.


Key Design Decisions Summary

DecisionChosen OptionAlternativeWhy This Choice
Position sensingHall sensors (3x)Encoder, sensorlessCost-effective, works at all speeds including startup, meets 2% accuracy
Control algorithm6-step trapezoidalFOC (field-oriented)Simpler, lower CPU load, sufficient for fan application. FOC is upgrade path.
PWM schemeCenter-aligned, 20 kHzEdge-aligned, variable freqAbove audible range, optimal ADC sampling point, constant switching frequency
MCUSTM32G474STM32F302, TI C2000Best peripheral integration (comparators + ADC + CAN-FD), good ecosystem, FPU
Safety architectureHardware overcurrent + E-stop bypassSoftware-only protectionHardware path works even if firmware hangs — mandatory for industrial safety
Speed feedbackTimer capture on Hall edgesPolling, DMAPrecise timing measurement, low CPU overhead, interrupt-driven

Timing Analysis

This is where you demonstrate that the design actually works within real-time constraints.

ISR Budget at 20 kHz (50 us period)

OperationTimeNotes
ISR entry (Cortex-M4)0.07 us12 cycles at 168 MHz
ADC result read (2 channels)0.5 usRegisters pre-loaded by hardware trigger
Current scaling + offset compensation1.0 usFixed-point or float multiply
6-step commutation lookup0.5 usTable lookup from Hall state
PID calculation (speed PI)3.0 usRuns every 20th cycle (1 kHz effective rate)
PID calculation (current PI)2.0 usRuns every cycle
PWM duty cycle update0.5 usWrite to timer compare registers
ISR exit0.07 us12 cycles
Total worst case7.6 us15.2% CPU utilization

Margin: 42.4 us remaining per cycle. This leaves substantial headroom for:

  • Future FOC upgrade (Clarke + Park transforms add approximately 5 us)
  • Additional safety checks within the ISR
  • Worst-case interrupt latency from lower-priority tasks

Task Timing (Non-ISR)

TaskPeriodExecution TimeCPU Utilization
CAN receive processing10 ms50 us0.5%
CAN status transmit100 ms30 us0.03%
Temperature ADC read + check100 ms20 us0.02%
Stall detection check100 ms5 us0.005%
Watchdog kick500 ms1 us0.0002%

Total CPU utilization: approximately 16% — well within safe operating margin. The industry guideline is to keep utilization under 70% to allow for worst-case jitter and future growth.

Memory Budget

ComponentRAMFlash
RTOS kernel (FreeRTOS)2 KB10 KB
ISR + control loop variables512 B8 KB
CAN TX/RX buffers256 B4 KB
PID state (2 controllers)64 B2 KB
Fault log (circular buffer, 64 entries)1 KB1 KB
Task stacks (4 tasks)2 KB
Application code32 KB
Lookup tables (sin/cos for FOC upgrade)4 KB
Total~6 KB~61 KB
STM32G474 capacity128 KB512 KB
Utilization5%12%

What Interviewers Evaluate

Real-time thinking: Can you calculate an ISR budget and explain why 20 kHz is the right control loop frequency? Can you explain what happens if the ISR overruns its period?

Safety-first design: Did you discuss overcurrent protection before the interviewer asked? Did you put the E-stop on a hardware path instead of a GPIO polled in firmware?

Concrete numbers: "The PID runs in 3 us" is stronger than "the PID is fast enough." Interviewers want to see that you can estimate cycle counts and timing budgets.

Tradeoff awareness: Explaining why you chose 6-step over FOC (simpler, sufficient for this application, upgrade path exists) is more impressive than just picking FOC because it is more advanced.

System-level perspective: The motor controller does not exist in isolation. You should mention the CAN network, the host controller sending setpoints, electromagnetic compatibility of the switching stage, and thermal management of the power MOSFETs.

Knowing what you do not know: It is perfectly fine to say "I would consult the motor datasheet for the torque constant and back-EMF coefficient before finalizing the current limits" rather than inventing numbers.