PID control

Just for fun, i started this project with a demo of a PID control. The proces is simple; take two plastic boxes, half full water and mount 2 pumps and a ultrasonic device for measuring the volume of water in one of the boxes. For a PID control you need at least a setpoint, a procesvalue, the parameters for the controller (P, I, D actions) and the ibrary for the PID control and the "field". The picture below is the P&ID (proces instrumentation diagram):

PID Test V0.0 PID

It is very simple and easy to explain (... and not very usefull but a nice example for a PID control). P02 (pump 2), is pumping the water from Tank 02 into Tank 01 with a predifined speed. P01 (pump 1) has to keep the same volume into Tank 01 by means of the PID-control. P02 can be set on different fixed outputs, so P01 has to control speed to keep te same volume in Tank 01. The volume is measured by the LT called DM1. DM1 is the ultrasonic device above Tank 01 and measures the distance of the sensor to the water surface (and so the volume in tank 01).

Some info about the hardware:

  • For the pumps i used two 12Vdc pumps
  • For regulating the pumps one L298N Dual H bridge stepper motor control
  • The HC SR04 Ultrasonic device
  • Some plastic hoses and 2 plastic boxes of about 2 ltr each.
  • The PID-1.2.0 library and the AFSM standard software
  • A serial to UBS converter to communicate with HMI






PID start


Explanation for "START" state:

In the start state the setpoint and the parameters for the controller are set. An array is filled to get rid of some disruptions from the Ultrasonic device. The average of 5 measurements is used as Process value for the control. Also pay attention for the controller direction. A "reverse" control means; when the input is accending the output of the control has to do same. In this case; when the level in Tank 1 is going up, P01 has to speed up for keeping the setpoint level. The opposite of "reverse" is "direct". For example an autopilot in a car; when the speed is dropping, the motor has to increase power to keep the disired speed.  
When the "START" button is pressed; the state will got to "REGULATE".


PID Regulate

Explanation of "REGULATE" state:

Every 200ms the input is measured and the output value is calculated by the PID control. After computing the ouput, it is set as a PWN value for the H bridge for P01. The output will only be set when it is greater then 155. If the value is lower than 155 the pump is not producing water. EN1 is set to command -start- P01 in the positive direction. If the "STOP" button is pressed, the state is again set to "START" (PID control is stopped).


How does it look on the plotter:

Red is the PV, process value; water level
Blue is the ouput of the controller; P01
Orange is the ouput of the fixed pump; P02

PID Sinus Stap

The plot show a flat line -with some noise- for the water level (as expected for this controller)


... and the installation of it on photo:

 PID foto


Initial HMI screen:



The states 25, 50, 75, 100 are run in a second FSM thread, parallel on the PID control thread. Every 5 minutes P02 is set on a new speed. As you can see in the plotter screen, P01 is following this "disturbance". For example, see the P02 states below. At the end of state "100", the thread is stopt; transition to the "END" state. P02 is stopt.

PID Pomp02