skip to content
arjun nair
projects
completed

bare-metal motor controller

ARM Cortex-M3 · completed

why bare metal

An OS is great at hiding hardware — which is exactly what I didn't want. Feedback control depends on sampling and acting at precise, regular intervals. Running directly on the Cortex-M3 meant configuring timers, PWM, and interrupts myself, and knowing exactly when each piece of code runs.

how it works

The controller measures motor speed, compares it to the target, and a PID controller computes a correction. That correction sets the duty cycle of a 20 kHz PWM signal driving the motor.

Target speed passes through PID, PWM, motor driver, and motor, with speed feedback returning to PID.
Conceptual feedback path. The PID runs at 100 Hz and PWM at 20 kHz; the driver and sensor symbols are illustrative.

timing

Two rates matter. The PWM runs at 20 kHz — fast enough that the motor sees a smooth average voltage, and above the range of audible whine. The PID loop runs at 100 Hz, a steady cadence for measuring and correcting. If a loop iteration runs late, the controller is effectively reacting to stale data; across testing there were zero missed real-time deadlines.

results

The controller operated across roughly ±3,000 RPM in both directions, held steady-state error below 1%, and settled in 2.1 seconds. The PWM ran at 20 kHz, the PID loop at 100 Hz, and I recorded no missed deadlines during testing. The settling time reflects the tuning tradeoff between reaching the target quickly and limiting overshoot.

Illustrative motor speed response approaching a target.
Illustrative response curve, not captured test data. The recorded settling time is 2.1 seconds and steady-state error is below 1%; this graphic does not establish a measured rise time.

what I learned

The control math was only part of the job. I also had to make sure each iteration ran when expected, since a correct PID update arriving late still acts on stale data.

arjun nair · stanford