Project 03- STM32F4xx PID controller

CMSIS files from ARM provides ARM Math functions. There are also PID controller functions in different formats for f32, q31 and q7. This tutorial/project will talk about how to implement PID controller on STM32F4xx using PID functions from ARM.

PID Controller

Fast about PID controller. PID stands for Proportional-Integral-Derivative controller. This is a control loop feedback mechanism widely used in industrial control systems. It calculates the error between measured value and the desired setpoint value. According to the error, it then calculates output value to minimize this error.

PID Controller block diagram

PID Controller block diagram

I will not go step-by-step on how PID works. More you can look on the sites below:

ARM PID library

ARM company provides 3 different PID controller functions:

  • f32: float
  • q31: integer
  • q7: char

For each of the three types, you have three functions:

  • f32
    • arm_pid_init_f32
    • arm_pid_reset_f32
    • arm_pid_f32
  • q31
    • arm_pid_init_q31
    • arm_pid_reset_q31
    • arm_pid_q31
  • q7
    • arm_pid_init_q7
    • arm_pid_reset_q7
    • arm_pid_q7

There are also ARM PID structure, where you pass PID parameters. More in project example below. If you need additional info about these functions, you have detailed manual here.

PID Sample project

In the project, 2 DS18B20 temperature sensors are used. They are configured in 12bit resolution, so delay between 2 calculations is about ~750ms. If you read about PID controller, you realize that you cannot calculate PID controller results every x microcontroller, because it has integral part which is used to sum all the errors in period. More calculations you have, more integral error you have.

We have one device connected as “reference” temperature. Second sensor is placed near DC FAN, so when DC fan is turned ON, it is cooling the second DS18B20. The goal is, that both temperatures are the same.

In my case, it was about 23°C on the reference sensor. Then, I touched the second DS18B20 near DC fan and I heated them. DC fan was turned on. Everything is displayed via USART to computer. FAN is controlled via 1kHz PWM output. According to the error between both sensors, more error, more duty cycle to DC fan.

This is just a sample project, so PID parameters are not set optimal. For every project you have to set parameters for specific project.

This example successfully works on all STM32F4xx development board such as Discovery and Nucleo.

In the example, 1 PID controller is used. If you need to control more than just one thing, you can add additional ARM PID instances and you can use more than just one PID controller at a time.


  • Both DS18B20 sensors are connected to the same pin: PA6
  • DC fan PWM pin (controlled via transistor): PA5
  • USART output: PB6, 115200baud


STM32F4 PID controller project setup

STM32F4 PID controller project setup

Download entire project with libraries and USART output log below.


Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...