Skip to main content
xpp07
Associate III
October 26, 2018
Question

Looking for resources to build a PID controller for dc motor speed control using STM32

  • October 26, 2018
  • 4 replies
  • 1344 views

Hello,

I have a L432KC Nucleo. I would like to build a PID or PI software to control the speed of a DC motor. The closed loop element is a speed sensor to monitor RPM. I would appreciate if anyone has resources to share about developing this controller using STM32.

    This topic has been closed for replies.

    4 replies

    Andrew Neil
    Super User
    October 26, 2018

    Do you understand the principles of PID control?

    They are long-established, well-documented and widely-used - not at all specific to STM32 ...

    https://en.wikipedia.org/wiki/PID_controller

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    T J
    Senior III
    October 26, 2018

    how do you control the speed of the motor ?

    did you decide on a motor ? which type ?

    PID is easy enough to run, a little difficult to get a perfect result...

    the RPM sensor would usually generate and interrupt or increment a counter...

    xpp07
    xpp07Author
    Associate III
    October 26, 2018

    I'm controlling a brushed DC motor via PWM. The hardware actually works, my project is just missing the software PID controller. I can manually control the duty cycle through CCR1 register. I'm measuring RPM using a reflective sensor and PWM input mode.

    T J
    Senior III
    October 26, 2018

    did you calculate the RPM already ?

    PID..

    Current RPM 1000

    Requested RPM 2000

    PID has 3 elements to be added:

    current error is targetRPM / RPM ;; 2000/1000 = 2

    multiply that by a constant to get it to a usable number, lets try 1

    Current Error = 2 ; // first element of PID // (double the RPM requested in this element)

    Historical error, is the sum of all errors in the last 1 minute(ish)

    lets say we use 30 readings for the Historical error;

    Herror = Herror *29/30 + Cerror; // second element Herror

    RateOFChange = Herror - LastHerror; // over the last reading

    // you may want to use a few readings for better stability.

    RateofChange, this is the most difficult to get right. // third element

    if the Current RPM is not changing , the historical error will level out RateofChange will go to zero

    you want to predict the next PWM setting...

    set the PID to be a percentage.. ie, increase speed by 3%

    Next PWM setting = CurrentPWM * ( 1+PID)