Author: Kaël Shelby (VTN)
This project implements closed-loop control of a DC Servo Motor NF5475E using a PID algorithm on the STM32F103C8T6 microcontroller.
The system supports both speed control and position control modes.
The hardware integrates an L298N H-Bridge motor driver, a leadscrew with nut (linear slide), and a rigid coupling connecting the motor shaft to the leadscrew.
A PC-based GUI (developed in Visual Studio C# WinForms) provides monitoring and real-time control.
The application provides:
- Configure desired speed setpoint.
- Configure desired position setpoint (in encoder counts or mm).
- Display actual speed and position feedback.
- Real-time charts for speed response and position response.
- Start/Stop motor operation.
- Data logging for PID analysis.
- STM32F103C8T6 (Blue Pill)
- L298N Motor Driver
- DC Servo Motor: NF5475E
- Leadscrew + nut (linear motion conversion)
- Rigid coupling (motor shaft ↔ leadscrew)
- Power Supply: 220V AC → 12V DC
- USB-to-UART Adapter (PL2303)
- Breadboard + jumper wires
Encoder Pin | STM32 Pin | Function |
---|---|---|
VCC (5V) | +5V | Power supply |
GND | GND | Ground |
Channel A | PA0 | TIM2_CH1 (Encoder Input A) |
Channel B | PA1 | TIM2_CH2 (Encoder Input B) |
PL2303 Pin | STM32 Pin | Function |
---|---|---|
TXD | PA3 (USART2_RX) | Receive data |
RXD | PA2 (USART2_TX) | Transmit data |
GND | GND | Common ground |
L298N Pin | STM32 Pin | Function |
---|---|---|
IN1 | PA11 | Motor direction control |
IN2 | PA12 | Motor direction control |
ENA (PWM) | PB8 (TIM4_CH3) | Motor speed (PWM signal) |
VCC (12V) | 12V DC | Motor power |
GND | GND | Common ground |
The Visual Studio (C# WinForms) GUI supports both speed control and position control:
-
Speed Control Mode:
- Enter desired speed setpoint.
- Display current speed (RPM).
- Real-time speed chart.
-
Position Control Mode:
- Enter desired position setpoint.
- Enter max speed and approach speed (for smoother motion).
- Display current position (encoder counts / mm).
- Real-time position chart.
1. Clone the project from GitHub
git clone <url>
2. Open the project with Keil C (uVision)
- Main source file: main.c
3. Connect hardware according to the diagram in Section 3
4. Flash the firmware using ST-Link V2 or USB-UART
5. Run the GUI Application (Visual Studio)
- Select the correct COM port.
- Press Start to enable motor control.
- Set Setpoint to desired speed and desired position
- Observe feedback and charts in real-time.
6. Expected Results:
- Motor speed stabilizes around the setpoint using PID control.
- Smooth response with reduced overshoot and steady-state error.
- GUI shows:
- Setpoint vs. Current Speed and Position graph
- Live motor control (Start/Stop, speed adjust).
- Data logging for PID tuning.
- Add Wi-Fi/IoT monitoring via ESP32 or MQTT broker.
- Integrate touchscreen HMI (TFT LCD) for local control.
- Upgrade driver (e.g., LMD18200, BTS7960) for higher power motors.