[STM32] STM32F103C8T6 realizes DC motor speed PID control

Hits: 0

Series Article Directory

·【STM32】New project template and configuration

·【STM32】STM32 communicates with PC, HC-06 and ROS via USART serial port

·[ROS] ROS host computer uses Serial library and boost::asio library to communicate with STM32 for USART

·【STM32】STM32F103C8T6+L298N controls the speed of DC motor through PWM

·【STM32】STM32F103C8T6 uses external interrupt method and input capture method for encoder speed measurement

·【STM32】STM32F103C8T6 realizes DC motor speed [PID control]

Article directory

Series Article Directory

Article directory


First, the basic principle of PID

2. Variant PID

3. Keil5 program



The PWM control based on the STM32F103C8T6+L298N+MG513P30 DC motor and the encoder real-time speed feedback of the two methods have been completed before. After getting this feedback value, we can use the classic PID algorithm to accurately control the speed of the motor. This article mainly shares the basic principles of PID and PID programming of Keil5.

First, the basic principle of PID

The PID algorithm is a control algorithm proposed in the 1930s. The PID algorithm can be used in aerospace, as small as home temperature control, although it has been almost a century since the PID algorithm was proposed, and many others have appeared since then. Modern intelligent algorithms, such as Monte Carlo, intelligent control, etc., but now PID is still enduring, it can be said that more than 80% of the current control still uses the PID algorithm.

PID algorithm is a part of the automatic control principle course, but in the course, the teacher explained the most basic principle, without any expansion, let alone application, first of all, let’s briefly talk about the principle of PID control algorithm.

The above picture is the control block diagram of the PID algorithm. When we control the motor speed, the expected input is the expected speed value of the motor. The expected input is the difference between the actual speed measured by the encoder, and the obtained error value is passed to the control part of the PID , calculate the control signal that needs to be output, and transmit the control signal to the controller, that is, output to the motor drive board L298N, thus forming a cycle, which realizes the precise control of the motor speed.

The continuous type formula of the control part of the intermediate PID is as follows:

However, it is difficult for the computer to realize the integration or differentiation of continuous variables in the computer. Therefore, in the computer, we use discrete integration and differentiation, that is, take the time interval T as 1. The discrete PID formula is as follows:

The main operations and effects of each item are as follows:

  • P: Increase the rapidity, too much will cause oscillation and overshoot, and P alone will always have static errors
  • I: Reduce static error, too much will cause shock
  • D: Reduce overshoot, too much will make the response speed slower

In practical applications, it may not be necessary to use PID at the same time. For example, only PI control is generally used in speed control. For various PI and PD controls, you can go to station B or read blogs of other bloggers. It has been described in great detail.

2. Variant PID

There are many evolutionary versions of the PID algorithm, and the classification is briefly explained.

Incremental PID

In the speed PID control algorithm of the motor, because we generally use the PI algorithm, we can use the incremental PID algorithm, which can make our formulas and codes more concise.

Integral limiter

Because the effect of the integral is accumulation, the value of the integral term will rise to a very high level over time. The original function of the integral is to reduce the static error, but if the integral term is too large, it will cause excessive vibration, so we can add A judgment function if, when the value of the integral term reaches a certain value, the integral term is kept at this value to avoid causing greater shocks.

Integral separation

If the error at the beginning is relatively large, the integral term will accumulate to a large value at the beginning, then when the actual output reaches the expected value for the first time, it will not stop immediately, but will produce a large overrun. rush. At this time, integral separation is needed, that is, when the error value is too large, we do not use the integral term, and only let the PD term work alone. When the error value is small, add the integral term to reduce the static error.

3. Keil5 program

For the convenience of use, we first set a PID structure, which stores the PID parameters, limit value, error and other parameters of the left and right wheels.

typedef  struct 
     //Related speed PID parameters 
    float Velcity_Kp;
     float Velcity_Ki;
     float Velcity_Kd;
     float Ur;               //limit value

    u8 PID_is_Enable;        //PID enable 
    int Un;                  //Expected output value 
    int En_1;                //The last error value 
    int En_2;                //The last error value 
    int PWM;                 //Output PWM value


In the program initialization part, define an initialization function to initialize the parameters in it.

void PID_Init(PID_InitDefStruct* p)
    p->Velcity_Kp = 5;
    p->Velcity_Ki = 0.5;
    p->Velcity_Kd = 0 ;
    p->Ur = 7100;
    p->PID_is_Enable = 1;
    p->Un = 0 ;
    p->In_1 = 0 ;
    p->En_2 = 0 ;
    p->PWM = 0;

When the timer of the encoder feeds back the actual speed measured by the encoder every 10ms, the PID function is called to solve the PWM value output to the motor driver board, and then set through the Set_Pwm function to control the motor speed.

void Velocity_PID(int TargetVelocity,int CurrentVelocity,PID_InitDefStruct* p)
    if(p->PID_is_Enable == 1)
        int En = TargetVelocity - CurrentVelocity; //Error value                                                     

        p->Un += p->Velcity_Kp*(En - p->En_1) + p->Velcity_Ki*En + p->Velcity_Kd*(En - 2 *p->En_1 + p->En_2); //enclosure PID


        p->PWM = p->Un;

        /*Output limit*/ 
        if (p->PWM>p->Ur) p->PWM=p->Ur;
         if (p->PWM<-p->Ur) p->PWM=-p- >Ur;


In the test, the ideal speed input to the motor is 1500mm/s, and a set of PI parameters are randomly set. The experimental results are as follows:

You can look at the waveform through the host computer, you can see that the static difference in the steady state is relatively small


For the PID algorithm, this is the first step in the long march. To call out the perfect control program, complex PID parameter adjustment is also required. Here, it can be debugged with the host computer, and then it can be adjusted and shared later.

The program is here.

The start of the semester is approaching, and the writing is quite simple. If you have any questions, please send a private message or comment, and we will discuss it together.

You may also like...

Leave a Reply

Your email address will not be published.