Skip to content

⚡ Closed-loop speed & position control of a DC motor with leadscrew mechanism, implemented on STM32 Blue Pill using PID algorithm.

Notifications You must be signed in to change notification settings

kael2209/-STM32-Speed-Position-Control-Using-PID-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

🚀 STM32 | Speed & Position Control Using PID Algorithm

Author: Kaël Shelby (VTN)

📖 1. Introduction

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.

intro

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.

🛠️ 2. Hardware Requirements

  • 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

🔌 3. Circuit Connections

intro

📌 Encoder (NF5475E → STM32F103C8T6)

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)

📌 UART Communication (PL2303 → STM32)

PL2303 Pin STM32 Pin Function
TXD PA3 (USART2_RX) Receive data
RXD PA2 (USART2_TX) Transmit data
GND GND Common ground

📌 H-Bridge Driver (L298N → STM32)

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

📜 4. User Interface

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.

ui


🎥 5. Demo and Results

👉 Watch the demo video

Watch Demo Video


🚀 6. Getting Started

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.

🚀 7. Future Development

  • 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.

About

⚡ Closed-loop speed & position control of a DC motor with leadscrew mechanism, implemented on STM32 Blue Pill using PID algorithm.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published