Achievement of high efficiency, low power loss, and real-time response are major challenges in an automated control process. Control processes are often error-prone due to external factors such as abnormal input power, variable temperature, load, and pressure. In a real-time environment, the process must not oscillate for any external forces. Closed-loop systems are designed to include an error compensator to eliminate the error. The PID (Proportional Integral Derivative) controller developed in 1911 by Elmer Sperry is a popular error compensation method that provides good stability, error-free and fast response in a closed loop system. This article explains the PID controller and its performance in DC motor controller using a Raspberry Pi.

PID controller implementation on Raspberry Pi

The traditional closed loop control system includes hardware circuits for the User Interface Unit, Feedback Circuit, Error Detector, PID controller circuit, and process control signal generator. We can integrate all these hardware functions using a single Raspberry Pi. The Raspberry Pi GPIO pins gather the inputs, feedback signals, and provide the output to the process. A  Graphical User Interface (GUI) can also be implemented to monitor and control the process. The logic for the PID controller can be implemented on Raspberry Pi by using any of the supported languages such as C, Java, or Python. A complete process control system can be implemented using Raspberry Pi as shown in Figure 1.

Figure 1: PID based closed-loop system using Raspberry Pi

An application program has an error detector function to obtain the error value by subtracting the process output from the process input value. The following formula shows the error detector output:

Error Value (Ve) = Process Input – Process Output

The error calculation code:

error = int(Set_RPM) -feedback

The PID controller logic produces the compensation output for the error. The process control signal generator produces a suitable control signal for the process using a PID output.

PID controller

The PID controller output is derived using the algebraic sum of a proportional, integral, and derivative controller.

Proportional controller (P)

The proportional controller improves the transient response of the system.

• Transient response: the time taken by a system to reach the steady state from its initial state.
• Steady state: the point at which the system output becomes settled and starts working in normal condition.

The proportional controller adds the product of the proportional gain (Kp) and error value to the process control output. In this case, the system response is directly proportional to the gain hence the error decreases with the increase in proportional gain. The following formula shows the resultant of the proportional controller:

Pout = Kp * Ve

The proportional controller always gives the steady-state error. If we increase the proportional gain more than the critical gain value, the output creates oscillation.

The proportional controller implementation code is:

Pout = Kp * error

Integral controller (I)

The Integral controller is always used with the proportional controller to reduce the steady-state error. The steady state error is the difference between the process output and steady state. The Integral output is proportional to both the magnitude and duration of the error. It accumulates the past error terms until the output value becomes equal to the desired value. This accumulated value is multiplied with Integral gain (Ki) and added to the process control output. The following formula shows the output of the I-controller:

Iout= Ki

The system response is inversely proportional to the integral gain. When a significant error occurs in the system output, the I-controller changes the output rapidly. As the error decreases the rate of change of I-controller output decreases, resulting in damping of the system output. Once the error value becomes zero, the I-controller holds the output value which eliminates the error. If the Integral controller is used alone, the time it takes to settle response becomes higher.

Integral controller implementation code is:

current_time = time.time()              # the execution time

delta_time = current_time - previous_time

Integral += (error * delta_time)

Iout = (Ki * Integral)

Derivative controller

The D controller increases the system stability and reduces the overshoot. The derivative controller output is proportional (multiplying the derivative gain (Kd)) to the rate of change of error over time. The following formula shows the output of the D controller.

Dout = Kd

The derivative controller implementation code is:

delta_error = error - previous_error

Derivative =(delta_error/delta_time)

Dout = (Kd * Derivative)

The derivative controller is always used with P or P&I controller to predict the future error. It is more sensitive to higher frequency terms because it behaves as a high pass filter.

Different combination of error compensation

The PID controller output depends on the input signal, feedback reference signal, and PID gain values.  There are four types of error compensation systems based on P, I, and D controllers:

• P-Controller
• PI- Controller
• PD- Controller
• PID- Controller

We can select any of these error compensation methods depending on the system requirement. Figure 2 illustrates the output of the different combination of error compensation methods Figure 2: PID controller outputs

• The P controller amplifies the error value and reduces the difference between the input and output of the system.
• The combination of P and I controller improves the relative stability and reduces study state error, but increases the overshoot and settling time.
• The combination of P and D controller reduces the overshoot but does not eliminate the study state error in the system.
• The P-I-D combination is a universally acceptable compensation method for closed-loop systems. In this case, P, I, and D output is dependents on one another. If there is a change in any one of the controlled variable, it can affect the other two controller outputs. Hence, perfect tuning is essential to get better performance and stability in the system.

The following formula represents the final output of the PID controller:

PID output = Kp*(Ve) + Ki  + Kd

The PID controller output code is:

Output = Pout + Iout + Dout

Follow the below instructions to achieve the fast and accurate response by adjusting the P-I-D gains.

• Initially keep I and D gains at zero position
• Increase the P gain until output reaches near to the set point, further tuning produces more oscillation.
• Now slowly increase I gain to reduce the steady-state error. It accelerates the process towards the desired output level, but the system generates oscillation.
• Increase the D gain to reduce the overshoot until getting the desired response. The fine adjustment of P, I, and D gains hold the system in a stabilized condition

Project Example: PID based DC Motor Control using Raspberry Pi:

The DC motor controller can be implemented using a Raspberry Pi, DC motor drive, and sensor. Download the Raspbian OS and flash to a micro SD card and insert to the Raspberry Pi. Connect the Raspberry Pi display (7” Touchscreen Display), keyboard and mouse. Now develop a PID based DC motor controller program. Connect the input, output and feedback units to the configured GPIO pins.

The Raspberry Pi produces the Pulse Width Modulation (PWM) signal based on the user inputs and the feedback signal. A driver circuit amplifies the controller output and drives the DC motor depending on the PWM signal.  A speed measurement sensor is used to encode the motor speed and produce the feedback signal in the form of a digital or analog signal. The feedback signal is fed to the Raspberry Pi to repeat the process to maintain the system in an equilibrium state.

PID based DC motor controller is designed to control the DC motor at a constant speed. The input and output connections are specified below:

Required hardware:

Raspberry Pi

Raspberry Pi Display:

• Farnell code: 2473872
• Newark code: 49Y1712

Motor Driver:

• MOTORPLATE:
• Farnell code: 2894843
• Newark code: 31AC5237

RPM sensors:

• Slotted
• Farnell code: 1386613
• Newark code: 21M5159

Or

• PNP type proximity sensor: PR12-4DP
• Farnell code: 1736862
• Newark code:

Procedure:

• RPi.GPIO.22 is configured for the feedback input from RPM sensor.
• RPi.GPIO.23 is configured as an output. It provides PWM pulses for DC motor driver.
• Connect the Raspberry Pi display and Input-Output pins.
• Connect the Power adapter and power up the raspberry pi
• Download and run the DC motor controller code
• GUI will open
• Adjust the required RPM and PID gains using the sliders, now click the Start button.
• The motor
• Change the input RPM, PID gain values between the run state and observe the response of the motor.
• After completion of your work press Stop button. Figure 3: Connection diagram