Skip to the content
2 layers47 × 57 mmMotor controller, the printed circuit board in 3D

Closed-loop DC motor speed controller using PID and encoder feedback, tuned via empirical calibration and step-response analysis

Size
47 × 57 mm
Copper layers
2
Thickness
1.6 mm
Parts
9
Nets
14
Pads
52
Tracks, vias
87, 11
Routed
Yes
KiCad
KiCad 10.0 (file 20260206)
Created
30 Jul 2026
Updated
31 Oct 2026

Overview

Motor controller is an open-source printed circuit board by vinaysundar1, from the vinaysundar1/closed-loop-motor-speed-control repository on GitHub. It is a 2-layer board of 47 × 57 mm with 9 parts on 14 nets, routed with 87 tracks and 11 vias.

It is built around the DRV8871DDA. Its parts include 2 capacitors, 1 resistor, 1 IC and 1 connector. It belongs with the robots and motor control boards of this library.

Main components

1

README

README.md

PID Motor Speed Controller

Closed-loop DC motor speed control using a quadrature encoder for feedback and a PID algorithm to hold a target RPM, running on an Arduino Uno. Custom PCB designed in KiCad, hand-soldered and assembled into a complete PCBA. github.com/vinaysundar1/closed-loop-motor-speed-control

Finished PCBA — Arduino, DRV8871 driver, barrel jack, and encoder/OLED/potentiometer headers, hand-soldered and assembled.

Kp=0.5, Ki=0.5, Kd=0.08 — settles within one overshoot/undershoot cycle (~157 RPM peak, ~90 RPM trough) and holds steady at setpoint.

Results

With a live, continuously-varying setpoint (driven by a potentiometer rather than a fixed step), the controller tracks a ramping target with one damped overshoot/undershoot cycle before settling, and holds within roughly ±2 RPM of setpoint at steady state.

Hardware

ComponentDetails
Arduino UnoMicrocontroller
L298N H-BridgeMotor driver
DC motor with rotary encoderGA25-370, 12V, 500 RPM nominal
0.96" OLED display (SSD1306)I2C status display
10K potentiometerSetpoint dial
12V power supply or battery packMotor power
Breadboard + jumper wires—

System Block Diagram

mermaid
flowchart LR
    P[Potentiometer<br/>Setpoint Dial] -->|analog read,<br/>mapped 0-400 RPM| A[Setpoint]
    A --> B((Sum))
    F[Encoder Feedback] --> B
    B -->|error| C[PID Controller]
    C -->|PWM 0-255| D[Motor Driver / H-Bridge]
    D --> E[DC Gear Motor]
    E -->|shaft rotation| F
    E --> G[OLED Display]
    A --> G

How It Works

  1. Speed measurement: A hardware interrupt counts encoder pulses on every rising edge. Every 100 ms, the tick count is converted to RPM: RPM = (ticks / PPR) * (60000 / interval_ms)
  2. Noise filtering: Raw RPM is smoothed with an exponential moving average (EMA) before being used anywhere else in the loop, since tick-count-based measurement over a short window is inherently noisy.
  3. PID calculation: Standard proportional + integral + derivative sum, with:
    • A second EMA filter applied specifically to the derivative term (derivative amplifies noise more than any other term).
    • Conditional-integration anti-windup — the integral only accumulates when doing so wouldn't push the output past the PWM actuator limits (0–255).
  4. Actuation: Final PID output is clamped to 0–255 (the PID math itself has no upper or lower bound, but analogWrite only accepts an 8-bit PWM duty cycle, so the raw sum has to be forced into the range the hardware can actually accept) and written via analogWrite to the motor driver's PWM input.

Tuning Process

Initial attempt — naive PID with unfiltered RPM feedback:

Kp=0.6, Ki=0.1, Kd=0.05 — oscillation amplitude grows over time rather than settling

Diagnosis: the derivative term was amplifying tick-count measurement noise (division by a small dt multiplies noise), and the integral anti-windup only clamped the final value rather than preventing over-accumulation during saturation, both contributing to a sustained/growing limit cycle.

Fix: added EMA filtering on the RPM measurement and on the derivative term, and switched to conditional-integration anti-windup.

Kp=1.0, Ki=0.08, Kd=0.02 — clean damped response, but settles ~30 RPM below setpoint

Diagnosis: proportional control alone cannot fully close steady-state error against real motor friction/load, that gap can only be closed by the integral term, and Ki was too small to close it in a reasonable time.

Increased Ki to close the steady-state gap:

Kp=1.0, Ki=0.75, Kd=0.02 — closes the steady-state gap, but rings through roughly two decaying overshoot/undershoot cycles before settling

Isolating the double-oscillation: to find the cause, Kp was held fixed at 1.0 while Ki and Kd were varied independently across several tests (Ki: 0.75 → 0.35 → 0.5; Kd: 0.02 → 0.2 → 0.08). The first overshoot peak stayed essentially unchanged (~205–233 RPM) across every one of these combinations — strong evidence that Kp, not Ki or Kd, was driving the size of that first overshoot, since neither term had moved it despite multiple independent tests.

Note on Ki tuning: I also tested higher Ki values (up to 1.0) to see if faster settling was worth the tradeoff. Higher Ki consistently reduced settling time but increased overshoot (up to ~172 RPM vs. the original ~157 RPM), and increasing Kd alongside it didn't meaningfully reduce that overshoot. I kept Kp=0.5, Ki=0.5, Kd=0.08 as the final configuration since it produced the smallest overshoot/undershoot swings, which I prioritized over faster settling for this system.

Final tune: lowered Kp itself (1.0 → 0.6 → 0.5), keeping Ki=0.5 and Kd=0.08 fixed. The first overshoot dropped sharply with each reduction (≈205 → 170 → 157 RPM), while the undershoot depth stayed roughly constant (~90–100 RPM) regardless of Kp.

Kp=0.5, Ki=0.5, Kd=0.08 — settles after one overshoot/undershoot cycle (~157 RPM peak, ~90 RPM trough), then a smooth, non-oscillatory approach to setpoint

Live Setpoint via Potentiometer

The initial tuning above used a fixed, hardcoded setpoint (150 RPM) for a clean, repeatable step-response test. Once the gains were validated, the setpoint was switched to a potentiometer read on an analog pin, mapped to a 0–400 RPM range, so the target speed can be changed live by turning a knob.

A moving setpoint introduces failure modes a fixed step test doesn't expose:

  • Small electrical noise on the analog reading caused the setpoint to flicker by a couple RPM every loop, which would look like a disturbance to the derivative term. Fixed with a deadband: the setpoint only updates if the new reading differs from the current one by more than 3 RPM.

Kp=0.5, Ki=0.5, Kd=0.08 — RPM (orange) tracks a setpoint (green) jump, with one overshoot to one undershoot before settling and holding steady-state tracking at the new target

Final PID Gains

GainValuePurpose
Kp0.5Primary response to instantaneous error; lowered from an initial 1.0 after isolating it as the driver of first-overshoot magnitude
Ki0.5Eliminates steady-state error from motor friction/load
Kd0.08Damps overshoot; raised from an initial 0.02 now that the derivative acts on a filtered signal

PCB Design

The breadboard prototype below was translated into a custom PCB in KiCad, consolidating the Arduino, motor driver, and peripheral connections into a single soldered board with defined connector headers.

v1 — breadboard prototype: Arduino Uno, L298N motor driver, GA25-370 encoder motor, potentiometer, and OLED display.

Full schematic — Arduino header, DRV8871 motor driver with support circuitry, barrel jack power input, and connector headers for the encoder, OLED, and potentiometer.

Current-Limit (ILIM) Resistor

The DRV8871 sets its current limit via a resistor from ILIM to GND, per the datasheet relationship ITRIP ≈ 64,000 / R_ILIM(Ω). The GA25-370's rated block/stall current is 1.1A; a 32kΩ resistor was chosen to set a ~2A limit, giving roughly 2x margin above the motor's normal stall current so the limit protects against faults without nuisance-tripping during normal operation.

Decoupling

A 0.1µF ceramic capacitor (high-frequency switching noise) and a 47µF electrolytic capacitor (bulk current supply during motor transients) are placed across VM/GND, matching TI's reference application circuit for this driver.

Power Trace Sizing

Trace widths were calculated using KiCad's built-in IPC-2221 calculator:

Net classCurrent basisCalculated minimumUsed
Power (VMOTOR, GND, motor outputs)2A (ILIM-protected worst case), 10°C rise~30.8 mil32 mil
Signal (I2C, control inputs, ILIM)1A reference, 10°C rise~12 mil12 mil

Routed PCB layout — 0.8mm power traces (VMOTOR/GND/motor outputs), 0.3mm signal traces, ground pour on the bottom layer with thermal vias under the driver IC.

Bench bring-up — the assembled PCBA connected to the motor, encoder, and OLED during initial power-on testing, confirming the board functions as designed before final wiring cleanup.

Lessons Learned

  • Sensor noise, not just gain values, can be the root cause of oscillation. Filtering the measurement is important in the tuning process.
  • Anti-windup implementation matters: clamping the integral's final value is not the same as preventing it from over-accumulating in the first place.
  • Isolating one gain at a time was what actually revealed which gain caused which symptom. Multiple tests where Ki and Kd changed with no effect on the first overshoot were what pointed to Kp as the real cause, rather than guessing.

Future Improvements

  • Reset the integral term on large setpoint jumps, so accumulated history from the old target doesn't bleed into the transient response at a new one
  • Quadrature (2-channel) decoding for direction sensing and 4x resolution
  • Auto-tuning via relay/Ziegler-Nichols method triggered by a button press
  • Using more precise motors and potentiometers to reduce error.

Parts

9
QtyValueFootprintReferences
10.1uFCapacitor_THT:C_Disc_D5.0mm_W2.5mm_P5.00mmC1
132kResistor_THT:R_Axial_DIN0207_L6.3mm_D2.5mm_P10.16mm_HorizontalR1
147uFCapacitor_THT:CP_Radial_D6.3mm_P2.50mmC2
1Barrel_JackConnector_BarrelJack:BarrelJack_Wuerth_694108106102_2.5x5.5mmJ1
1Conn_01x04Connector_PinHeader_2.54mm:PinHeader_1x04_P2.54mm_VerticalOLED1
1Conn_01x06Connector_PinHeader_2.54mm:PinHeader_1x06_P2.54mm_VerticalEncoder1
1Conn_01x09Connector_PinHeader_2.54mm:PinHeader_1x09_P2.54mm_VerticalArduino1
1DRV8871DDAPackage_SO:Texas_HTSOP-8-1EP_3.9x4.9mm_P1.27mm_EP2.95x4.9mm_Mask2.4x3.1mm_ThermalViasU1
1R_PotentiometerPotentiometer_THT:Potentiometer_Bourns_3386P_VerticalRV1

Similar boards

  • 4 layers122.2 × 115.1 mmScan controller kicad, the printed circuit board in 3D

    Scan controller kicad

    fwachsmuth/Filmkorn-Raw-Scanner

    The Filmkorn Raw Scanner, turning 8mm and 16mm films into lossless 12-bit CinemaDNG

    170 parts13MIT
  • 4 layers81.3 × 52.8 mmGlue Board, the printed circuit board in 3D

    Glue Board

    arkorobotics/robocar-pinky

    DIY Robocar Project - Codename Pinky

    87 parts12
  • 2 layers65.6 × 56 mmWw2020, the printed circuit board in 3D

    Ww2020

    dkjonas/WildWillyHardware

    69 parts11
  • 4 layers50 × 50 mmSimpleboard, the printed circuit board in 3D

    Simpleboard

    AlessioMorale/unav2_hardware

    Initial test hardware for the future uNav board version

    122 parts5MIT
  • 2 layers83.3 × 56.5 mmPhone bell, the printed circuit board in 3D

    Phone bell

    purduehackers/phone-bell

    A Rotary Phone Doorbell

    53 parts4
  • 4 layers123 × 56 mmSkyduino Hardware, the printed circuit board in 3D

    Skyduino Hardware

    Skyduino/Skyduino-Hardware

    Hardware design files for the Skyduino roaster

    80 parts4
  • 4 layers31.3 × 34 mmNOGGIN, the printed circuit board in 3D

    NOGGIN

    berkeleyauv/RoboSub_HW

    PCB designs for Underwater Robotics at Berkeley (UR@B)

    60 parts3GPL-3.0
  • 2 layers93.1 × 158.3 mmMegaMouse, the printed circuit board in 3D

    MegaMouse

    KenwoodFox/MegaMouse

    MegaMouse! An atmega 328p powered micromouse!

    136 parts3MIT

Questions

How big is the Motor controller PCB?

It measures 47 × 57 mm and has 2 copper layers, 1.6 mm thick.

What parts does the Motor controller use?

9 parts, built around the DRV8871DDA. The full list of its parts is on this page, with links to each part's symbol and footprint where PCBKit's library has them.

Where can I download the Motor controller KiCad files?

From the vinaysundar1/closed-loop-motor-speed-control repository on GitHub, at the commit this page was made from: the board is KiCad_Files/motor_controller.kicad_pcb and its schematic KiCad_Files/motor_controller.kicad_sch. PCBKit opens the board in the browser, with nothing to install.

Can I use the Motor controller design in my own project?

Its repository states no licence, so its author keeps all rights: ask vinaysundar1 before reusing the design.