← All projects
ME 333 · Mechatronics

Closed-Loop DC Motor Control

Cascaded current and position control for a brushed DC motor on a Raspberry Pi Pico 2. A 1 kHz current loop runs inside a 200 Hz position loop, and a Python client sends trajectories and plots the results.

C / Pico SDKPythonPID DRV8833INA219Quadrature encoder
Demo · 4:35

Walkthrough & live test

I walk through the position-control code, then run a cubic trajectory on the real motor from the Python client. The reference-versus-measured plot and its score appear at the end of the run.

Tip: skip to about 2:10 to see the motor track the trajectory.

Controller
Raspberry Pi Pico 2 (RP2350)
Loops
PI current @ 1 kHz
PID position @ 200 Hz
Sensing
INA219 current sensor
1336 CPR encoder
Best result
1.98° mean tracking error

01Overview

The goal was to make a DC motor follow a commanded angle trajectory accurately. Motor torque is proportional to current, so the controller is split in two: an inner loop regulates motor current (torque), and an outer loop decides what current is needed to reach the desired angle.

Everything runs on the Pico 2 using hardware-timer interrupts. A menu-driven Python client on the laptop talks to the board over USB serial. It sets gains, loads step or cubic trajectories, runs tests and plots the reference against the measured response with a score (mean absolute error).

02Hardware

  • Pico 2Runs both control loops and the serial command interface
  • DRV8833 H-bridgeTwo PWM channels (GP16/GP17) set direction and duty cycle
  • INA219I²C current sensor in series with the motor (GP14/GP15)
  • Encoder334 PPR × 4 quadrature = 1336 counts/rev (GP18/GP19)
Hand-drawn circuit diagram: Pico 2, INA219, DRV8833 and encoder motor
Circuit diagram. The INA219 sits between the H-bridge output and the motor so it measures the actual winding current. Click to enlarge.

03Control architecture

The firmware is a state machine (IDLE → PWM → ITEST → HOLD → TRACK) driven by two repeating timers:

  • Current loop (1 kHz). Reads the INA219, computes u = Kp·e + Ki·Σe, clamps it to ±100% duty and sets the H-bridge PWM.
  • Position loop (200 Hz). Converts encoder counts to degrees, runs PID against the next trajectory point and outputs the desired current for the inner loop. The D-term acts on the measured angle instead of the error, which avoids derivative kick on step inputs.
  • Logging. Desired and actual values go into arrays during a run and are streamed back to Python afterwards, so serial I/O never interrupts the control timing.
Current loop square-wave test, score 8.51
Inner-loop check (ITEST). A ±100 mA square wave with Kp = 0.095 and Ki = 0.093. The current settles within a few samples of each edge (score 8.51 mA).

04Tuning & results

With the current loop working, I tuned the position gains on step and cubic trajectories (0° → 90° → hold → 0° over 3 s). The score is the mean absolute error in degrees.

19.8°cubic, first gains
3.19°Kp 42 · Kd 28 · Ki 0.02
1.98°Kp 100 · Kd 65 · Ki 0.02

Before → after (drag the slider)

Before · 19.8°After · 1.98° Cubic trajectory before tuning: large lag and overshoot
Cubic trajectory after tuning: near-perfect tracking

Red = reference, blue = measured angle. Both images use the same trajectory.

Step response

Step inputs showed the trade-off clearly. Low gains (Kp 5, Kd 2) gave a slow rise and about 25% overshoot. Raising Kp without enough damping made the overshoot worse. Increasing Kd together with Kp is what finally removed the oscillation on the cubic trajectory.

Step response with Kp 5 Kd 2
Step, Kp 5 · Kd 2 · Ki 0 (score 17.7°)
Cubic trajectory with Kp 42 Kd 28
Cubic, Kp 42 · Kd 28 · Ki 0.02 (score 3.19°)
View all test plots (PDF) ↗

05Try it: PID playground

This runs a simplified motor model in your browser using the same control law as my firmware. Move the sliders and the response updates live. The presets use the gains I tested on the real motor.

Position loop simulator

Score —
referencesimulated angleerror

The model uses a first-order current loop, viscous damping and Coulomb friction, with constants fit to match the trends I measured. It's for intuition, not exact numbers.

06Code

Firmware in C (Pico SDK) and the Python serial client. The highlighted excerpts are the two control-loop interrupt handlers.

Loading…

07Takeaways

  • Tune from the inside out. The position loop only worked well after the current loop was fast and accurate.
  • Damping matters as much as stiffness. Increasing Kd along with Kp brought the error from 19.8° down to 1.98°.
  • Log first, transmit later. Buffering data in arrays kept serial communication from disturbing the 1 kHz loop timing.
  • Next time: I'd add integrator anti-windup, filter the derivative term, and add feedforward from the trajectory velocity.