element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Automation: PID-based DC motor controller using the Raspberry Pi
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Raspberry Pi Projects to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: rscasny
  • Date Created: 4 Jan 2019 8:11 PM Date Created
  • Views 15965 views
  • Likes 10 likes
  • Comments 7 comments
  • raspberry_pi_projects
Related
Recommended

Automation: PID-based DC motor controller using the Raspberry Pi

rscasny
rscasny
4 Jan 2019

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.

   image

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

image

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-4DPPR12-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.

 

image

 

 

 

 

Figure 3: Connection diagram

  • Sign in to reply

Top Comments

  • Jan Cumps
    Jan Cumps over 6 years ago +4
    For C++ lovers, there's an Arduino PID library that can be ported to the Raspberry Pi: Improving the Beginner’s PID – Introduction « Project Blog Some of us have used this library before. You'd need to…
  • rscasny
    rscasny over 6 years ago in reply to Jan Cumps +3
    I pastyed everything into the blog. There is no attachment. I removed the note. Thanks for pointing this out. Randall
  • genebren
    genebren over 6 years ago +3
    Great write-up. PID controllers do work very well. The issue sometimes is in tuning the P, I and D gains to get the system to behave as expected. Simulators can help if you can model the load or controlled…
  • vsp
    vsp over 6 years ago in reply to michaelkellett

    Hello Michael.

     

    Expected code and the video demonstration is available on PID based DC motor controller Project.

     

    I hope you enjoy that.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • michaelkellett
    michaelkellett over 6 years ago

    I'm missing something here - where is the actual code ?

     

    Any video/picture of it actually working ?

     

    MK

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 6 years ago

    For C++ lovers, there's an Arduino PID library that can be ported to the Raspberry Pi: Improving the Beginner’s PID – Introduction « Project Blog

    Some of us have used this library before. You'd need to come up with a Pi-friendly way to find the milliseconds lapsed (implement a millis() function) and  get a reasonably stable timed loop.

    • Cancel
    • Vote Up +4 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • genebren
    genebren over 6 years ago

    Great write-up.  PID controllers do work very well.  The issue sometimes is in tuning the P, I and D gains to get the system to behave as expected.  Simulators can help if you can model the load or controlled item reasonably well. I have used this type of controller on many projects, mostly dealing with controlling temperature within a zone (or multiple zones).

     

    Gene

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • rscasny
    rscasny over 6 years ago in reply to Jan Cumps

    I pastyed everything into the blog. There is no attachment. I removed the note. Thanks for pointing this out.

     

    Randall

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube