Close

Project Log #2

A project log for Micro-PID Module

This is a small PID Temperature controller, designed to be a drop-in module on a parent board.

zach-strohmZach Strohm 09/28/2019 at 15:430 Comments

In my previous project log, we discussed what a PID controller is, and the basics of how the project works. In this post, we will look at how to implement a PID controller in a microcontroller. In order to use the PID equation in a digital environment, we first must make a discrete-time model.

We will first look at the integral operator.

This is saying that the integral is equal to the sum of all previous error with respect to a sampling time.

Next, we will look at the derivative operator.

Using an approximation method known as backwards finite differences, the derivative can be approximated as the current error minus the previous error divided by the sampling time.

But how do we implement this in code? This algorithm is surprisingly simple, as can be seen below:

previous_error = 0
integral = 0
loop:
    error = setpoint – measured_value
    integral = current_error + cumulative_error*dt
    derivative = (current_error-previous_error)/dt
    output = Kp + Ki*integral + Kd*derivative
    previous_error = error
    delay(dt)
    return to loop

There will be more updates in the future. I should be getting some hardware to work on soon. As always, feel free to ask questions, and make suggestions.

Discussions