{"id":1035,"date":"2022-06-19T08:00:07","date_gmt":"2022-06-19T08:00:07","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=1035"},"modified":"2022-06-19T08:05:28","modified_gmt":"2022-06-19T08:05:28","slug":"working-with-stm32-and-pid-controller-part-3-speed-control","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=1035","title":{"rendered":"Working with STM32 and PID Controller Part 3: Speed control"},"content":{"rendered":"\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"364\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1024x364.png\" alt=\"\" class=\"wp-image-961\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1024x364.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-300x107.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-768x273.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1536x546.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-2048x727.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-1150x408.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-750x266.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-400x142.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/05\/PID_en.svg_-250x89.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=968\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=968\" target=\"_blank\">here<\/a>), we took a look at how to implement the PID controller programmatically. In this guide, we shall use simple case study to control the speed of a DC motor.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is speed control.<\/li><li>Code implementation.<\/li><li>Demo.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is speed control:<\/h2>\n\n\n\n<p>Often we want to control the speed of a DC motor on demand. This intentional change of drive speed is known as speed control of a DC motor.<\/p>\n\n\n\n<p>We can control the speed of DC motor manually or through an automatic control device. This is different to speed regulation \u2013 where the speed can regulate against the natural change in speed due to a change in the load on the shaft.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/images.jpg\"><img decoding=\"async\" src=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/images.jpg\" alt=\"practicle of speed control\" class=\"wp-image-745105\" \/><\/a><\/figure><\/div>\n\n\n\n<p>The\u00a0<strong>speed of a DC motor<\/strong>\u00a0(N) is equal to<\/p>\n\n\n\n<p><strong>N = K (V \u2013 IaRa)\/ \u00f8<\/strong>\u00a0Where, K is a constant.<\/p>\n\n\n\n<p><strong>This implies three things:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Speed of the motor is directly proportional to supply voltage.<\/li><li>The Speed of the motor is inversely proportional to armature voltage drop.<\/li><li>The motor speed is inversely proportional to the flux due to the field findings<\/li><\/ol>\n\n\n\n<p>Thus, the speed of a DC motor can control in three ways:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>By varying the flux, and by varying the current through field winding<\/li><li>By varying the armature voltage, and the armature resistance<\/li><li>Through the supply voltage\u00a0 \u00a0<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">1. Flux Control Method<\/h3>\n\n\n\n<p>Due to the field winding, the magnetic flux varies in order to vary the speed of the motor. As the magnetic flux depends on the current flowing through the field winding, it changes by varying the current through the field winding. This can achieve using a variable resistor in a series with the field winding resistor.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Flux-control-method.png\"><img decoding=\"async\" src=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Flux-control-method.png\" alt=\"\" class=\"wp-image-745112\" \/><\/a><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>Initially, when the variable resistor keeps at its minimum position, the rated current flows through the field winding due to a rated supply voltage, and as a result, the speed is kept normal. When the resistance increases gradually, the current through the field winding decreases. This in turn decreases the flux produced. Thus, the speed of the motor increases beyond its normal value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Armature Control Method<\/h3>\n\n\n\n<p>The controlling of armature resistance controls the voltage drop across the armature. With this method, the speed of the DC motor can control. This method also uses a variable resistor in series with the armature.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Armature-control-method.png\"><img decoding=\"async\" src=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Armature-control-method.png\" alt=\"\" class=\"wp-image-745110\" \/><\/a><\/figure><\/div>\n\n\n\n<p>When the variable resistor reaches its minimum value, the armature resistance is at normal one. Therefore, the armature voltage drops. When the resistance value gradually increases, the voltage across the armature decreases. This in turn leads to decrease in the speed of the motor. In this way,\u00a0 this method achieves the speed of the motor below its normal range.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. Voltage Control Method<\/h3>\n\n\n\n<p>Both the above mentioned methods cannot provide speed control in the desirable range. Moreover, the flux control method can affect commutation. Whereas the armature control method involves huge power loss due to its usage of resistor in series with the armature. Therefore, a different method is often desirable \u2013 the one that controls the supply voltage to control the motor speed.<\/p>\n\n\n\n<p>In such a method, the field winding receives a fixed voltage, and the armature gets a variable voltage. One such technique of voltage control method involves the use of a switch gear mechanism to provide a variable voltage to the armature. Another one uses an AC motor driven Generator to provide variable voltage to the armature (named as&nbsp;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Ward_Leonard_control\" target=\"_blank\" rel=\"noreferrer noopener\">Ward-Leonard System<\/a>).<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Speed-contro.png\"><img decoding=\"async\" src=\"https:\/\/robu.in\/wp-content\/uploads\/2020\/04\/Speed-contro.png\" alt=\"\" class=\"wp-image-745325\" \/><\/a><\/figure>\n\n\n\n<p>Apart from these two techniques, the most widely used technique is the use of&nbsp;<a href=\"https:\/\/www.elprocus.com\/pulse-width-modulation-pwm\/\" target=\"_blank\" rel=\"noreferrer noopener\">pulse width modulation<\/a>&nbsp;to achieve speed control of a DC motor. PWM involves application of varying width of pulses to the motor driver to control the voltage applied to the motor. This method proves to be very efficient as the power loss keeps at minimum, and it doesn\u2019t involve the use of any complex equipment.<\/p>\n\n\n\n<p>PWM is achieved by varying the pulses applied to the enable pin of the motor driver IC to control the applied voltage of the motor. The variation of pulses is done by the micro controller, with the input signal from the push buttons.&nbsp;<\/p>\n\n\n\n<p>We hope that we covered all the details and relevant description on DC motor speed control.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Code Implementation:<\/h2>\n\n\n\n<p>First, include the header file of PID control<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;pid.h&quot;<\/pre><\/div>\n\n\n\n<p>Declare the structure of the PID:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">PID_Param_t pid_par;<\/pre><\/div>\n\n\n\n<p>variable to store current rpm and output:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">float rpm;\nuint32_t out;<\/pre><\/div>\n\n\n\n<p>in the main function:<\/p>\n\n\n\n<p>Set the parameters of PID:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">pid_par.Kp=1.4;\n\tpid_par.Ki=0.001;\n\tpid_par.Kd=1;\n\tpid_par.Ts=1;\n\tpid_par.Anti_windup=Anti_windup_disabled;\n\tpid_par.Anti_windup_error=10;\n\tpid_par.Set_point=4000;\n\tpid_par.Outmin=100;\n\tpid_par.Outmax=1000;<\/pre><\/div>\n\n\n\n<p>Initialize PID controller:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">PID_init(&amp;pid_par);<\/pre><\/div>\n\n\n\n<p>In the while loop:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">rpm=get_rpm();\n\t\tif(millis()%(uint64_t)pid_par.Ts==0){\n\t\tout=PID_Calculation(rpm);\n\t\tset_duty_cycle(out);\n\t\t}\n\t\tif(millis()%10==0){\n\t\tprintf(&quot;%0.1f\\t%0.1f\\r\\n&quot;,(float)pid_par.Set_point,rpm);}\n<\/pre><\/div>\n\n\n\n<p>You may download the source code from here:<\/p>\n\n\n\n<div class=\"wp-block-file\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/PID_Controller.zip\">PID_Controller<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2022\/06\/PID_Controller.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Demo:<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"PID demo on STM32F4\" width=\"1170\" height=\"658\" src=\"https:\/\/www.youtube.com\/embed\/w7H46D7wx8M?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Note: the parameters are not tuned at all.<\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look at how to implement the PID controller programmatically. In this guide, we shall use simple case study to control the speed of a DC motor. In this guide, we shall cover the following: What is speed control. Code implementation. Demo. 1. What is speed control: Often [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,2,11,12],"tags":[],"class_list":["post-1035","post","type-post","status-publish","format-standard","hentry","category-data-structures","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1035"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1035"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1038,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/1035\/revisions\/1038"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}