Assume cooler and heater machines are connected via a relay. We can activate cooler and heater machine by sending HIGH signal on a relay.
Introducing PID controller
PID control is the most common control algorithm widely used in industry, and has been universally accepted in industrial control. The basic idea behind a PID controller is to read a sensor, then compute the desired actuator output by calculating proportional, integral, and derivative responses and summing those three components to compute the output.
An example design of a general PID controller is depicted in the following figure:
Furthermore, a PID controller formula can be defined as follows:
Kp, Ki
, Kd represent the coefficients for the proportional, integral, and derivative. These parameters are non-negative values. The variable e represents the tracking error, the difference between the desired input value i, and the actual output y. This error signal e will be sent to the PID controller.
Implementing PID controller in Python
In this section, we will build a Python application to implement the PID controller. In general, our program flowchart can be described by the following figure:
We should not build a PID library from scratch. You can translate PID controller formula into Python code easily. For implementation, I use the PID class from https://github.com/ivmech/ivPID. The following is the content of the PID.py file:
For testing purposes, we create a simple program for simulation. We need required libraries such as numpy, scipy, pandas, patsy, and matplotlib libraries. First, you should install python-dev for Python development. Type the following commands in your Raspberry Pi Terminal:
When done, you can install numpy, scipy, pandas, and patsy libraries. Open your Raspberry Pi Terminal and type the following commands:
The last step is to install the matplotlib library from source code. Type the following commands on your Raspberry Pi Terminal:
Once the required libraries are installed, we can test our PID.py file. Type the following program:
Save this program into a file called test_pid.py. Then, run this program.
This program will generate result.png as a result of the PID process. A sample of the output form, result.png, is shown in the following figure. You can see that the blue line represents desired values and the red line is an output of PID:
How does it work?
First, we define our PID parameters, as follows:
After that, we compute the PID value during sampling time. In this case, we set the desired output value as follows:
- Desired output
1 for sampling from 20 to 60 - Desired output
0.5 for sampling from 60 to 80 - Desired output
1.3 for sampling more than 80
The last step is to generate a report and is saved to a file called result.png:
Controlling room temperature using PID controller
Now we can change our PID controller simulation using the real application. We use DHT-22 to check a room temperature. The output of measurement is used as feedback input for the PID controller.
If the PID output positive value, then we turn on heater. Otherwise, we activate cooler machine. It may not good approach but this good point to show how PID controller work.
We attach DHT-22 to GPIO23 (BCM). Let's write the following program:
Save this program to a file called ch01_pid.py. Now you can this program:
After executing the program, you should obtain a file called pid_temperature.png. A sample output of this file can be seen in the following figure:
If I don't take any action either turning on a cooler or turning on a heater, I obtain a result, shown in the following figure:
How does it work?
Generally speaking, this program combines our two topics: reading current temperature through DHT-22 and implementing a PID controller. After measuring the temperature, we send this value to the PID controller program. The output of PID will take a certain action. In this case, it will turn on cooler and heater machines.