Simulink model-based design with real-time Arduino deployment — comparing P, PD, and PID controllers for sinusoidal trajectory tracking.
A closed-loop position control system built around a DC motor with quadrature encoder feedback, deployed in real-time on an Arduino via Simulink code generation.
┌──────────────┐ ┌───────────┐ ┌────────────┐ ┌─────────────┐
│ Sine Wave │───▶│ Error │───▶│ Controller │───▶│ Motor Drive │
│ Reference │ │ e = θref │ │ (P/PD/PID) │ │ PWM + Dir │
│ Generator │ │ − θmeas │ │ │ │ (H-Bridge) │
└──────────────┘ └───────────┘ └────────────┘ └──────┬──────┘
▲ │
│ ┌──────────────┐ │
└─────────│ Quadrature │◀──────────┘
│ Encoder │ DC Motor
└──────────────┘
Sine wave block producing a configurable position trajectory with adjustable amplitude and frequency.
Reads quadrature A/B channels, converts raw counts to degrees or revolutions using gear ratio.
Subtracts measured position from reference: e(t) = θ_ref(t) − θ_meas(t).
Configurable P, PD, or PID with output conditioning (saturation, absolute value) for PWM bounds.
PWM duty cycle controls speed/torque. Two digital direction pins drive the H-bridge for bidirectional control.
Simulink auto-generates code, compiles, and flashes to the Arduino for real-time closed-loop execution.
Nine configurations were tested across P, PD, and PID strategies, varying gains, data types, sample times, and reference frequencies.
| Type | Kp | Kd | Ki | Data | Ts (s) | Freq (Hz) |
|---|---|---|---|---|---|---|
| PD | 5 | 1.5 | — | single | 0.01 | 0.2 |
| PD | 9 | 1 | — | single | 0.01 | 0.2 |
| PD | 9 | 1 | — | double | 0.005 | 0.2 |
| P | 3 | — | — | double | 0.005 | 0.2 |
| P | 4 | — | — | single | 0.01 | 0.1 |
| P | 3 | — | — | single | 0.01 | 0.1 |
| PID | 3 | 1.5 | 0.5 | single | 0.01 | 0.1 |
| PID | 3 | 2 | 1 | single | 0.01 | 0.1 |
| PID | 3 | 2 | 1 | double | 0.001 | 0.1 |
Tracks the general sinusoidal shape but shows significant phase lag and steady-state error, especially at higher reference frequencies.
Derivative action reduces phase lag and sharpens transient response. Higher Kp with moderate Kd produces the tightest trajectory tracking.
Integral action eliminates steady-state error. Best performance at Kp=3, Kd=2, Ki=1 with double precision and Ts=0.001s. Slight overshoot in the negative region.