{"id":1771,"date":"2024-06-28T03:49:38","date_gmt":"2024-06-28T03:49:38","guid":{"rendered":"https:\/\/iotthinghub.com\/?p=1771"},"modified":"2024-08-11T17:03:40","modified_gmt":"2024-08-11T17:03:40","slug":"servo-motor-control","status":"publish","type":"post","link":"https:\/\/iotthinghub.com\/?p=1771","title":{"rendered":"Servo Motor Control"},"content":{"rendered":"\n<p class=\"has-text-color has-link-color wp-elements-ac89d672e07a27745df4df665653d4a6 wp-block-paragraph\" style=\"color:#5c5c5c\">A servomotor is a rotary actuator that allows for precise control of angular position, velocity and acceleration. Most modern servomotor are designed and supplied around a dedicated control module. So that servomotor doesn\u2019t require a driver. A microcontroller PWM pin can be used directly to control the servomotor.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"590\" height=\"188\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/servo-mesanisum.png\" alt=\"\" class=\"wp-image-1773\" style=\"width:785px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/servo-mesanisum.png 590w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/servo-mesanisum-300x96.png 300w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-c259852bdde5c66ec8ec376702941aa0 wp-block-paragraph\" style=\"color:#5c5c5c\">From picture it is seen that the servomotor works at 50Hz frequency. Let\u2019s make a 50Hz frequency using Timer1 with Fast PWM.<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-d9daa69ff657108fb7b33f78c60c187e wp-block-paragraph\" style=\"color:#5c5c5c\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; We know the frequency using Fast PWM<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img decoding=\"async\" width=\"492\" height=\"77\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-15.jpg\" alt=\"\" class=\"wp-image-1778\" style=\"width:339px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-15.jpg 492w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-15-300x47.jpg 300w\" sizes=\"(max-width: 492px) 100vw, 492px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-3b795d255de517e42b5c407fc3804842 wp-block-paragraph\" style=\"color:#5c5c5c\">Let\u2019s look at the Waveform Generation Mode Bit<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"601\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mode-PWM-1024x601.jpg\" alt=\"\" class=\"wp-image-1780\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mode-PWM-1024x601.jpg 1024w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mode-PWM-300x176.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mode-PWM-768x451.jpg 768w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/mode-PWM.jpg 1213w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"963\" height=\"223\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-16.jpg\" alt=\"\" class=\"wp-image-1785\" style=\"width:570px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-16.jpg 963w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-16-300x69.jpg 300w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-16-768x178.jpg 768w\" sizes=\"(max-width: 963px) 100vw, 963px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-aac8679f790cd8cba00168f6929f7a85 wp-block-paragraph\" style=\"color:#5c5c5c\">Let use Fast PWM with TOP value ICR1 with parameter F_CPU=8MHz and prescaler=64, than<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"440\" height=\"169\" src=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-17-1.jpg\" alt=\"\" class=\"wp-image-1784\" style=\"width:260px;height:auto\" srcset=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-17-1.jpg 440w, https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/06\/function-17-1-300x115.jpg 300w\" sizes=\"(max-width: 440px) 100vw, 440px\" \/><\/figure>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-51ce5c2c486dbc9e8d5bc8e3ed853dd0 wp-block-paragraph\" style=\"color:#5c5c5c\">The full program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/************************************************************************************************\n*************************************************************************************************\n*********************************** Servo Motor Control****************************************\n*************************************************************************************************\n\t\tSubeer Kumar Sarkar\n\t\tElectrical &amp; Electronic Engineer\n************************************************************************************************\n************************************************************************************************\n***********************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\nint main(void)\n{\nDDRB|=(1&lt;&lt;DDB1)|(1&lt;&lt;DDB2);\t\t\t\t\t\t\t\t\t\/\/OC1B &amp; OC1A connected\n\/***********************Fast PWM TOP=ICR1 non-inverting mode************************************\/\nTCCR1A|=(1&lt;&lt;WGM11)|(1&lt;&lt;COM1B1)|(1&lt;&lt;COM1A1);\nTCCR1B|=(1&lt;&lt;WGM13)|(1&lt;&lt;WGM12)|(1&lt;&lt;CS11)|(1&lt;&lt;CS10);\t\t    \/\/prescalling 64\nICR1=2499;\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/time preiod 50Hz\n\/***********************************************************************************************\/\nwhile(1)\n{\n\tfor(int i=125;i&lt;=249;i++)\n\t{\n\t\tOCR1B=i;\n\t\t_delay_ms(80);\n\t}\n\n}\nreturn 0;\n}\n<\/pre><\/div>\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-3e41869c wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Servo-control.rar\" style=\"padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-5232fec5577e9eeb6d569b7279794646 wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Servo-control.rar\">Servo control using ATmega8.rar<\/a><\/p>\n<\/div>\n<\/div>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-b700907319ca6336b23c28abf1a8b741 wp-block-paragraph\" style=\"color:#5c5c5c\">Sometime you need to control multiple servomotors at the same time. In this case we need to set I\/O port and clear I\/O port in such meaner that a 50Hz pulse is generated. To generate a 50Hz pulse we use Fast PWM with TOP value ICR1, F_CPU=8MHz and prescaler=64. The coding is same for 50Hz frequency above. The next thing is to set I\/O port at beginning and clear I\/O port when we wanted. The trick is to set I\/O port at interrupt and clear it in main program. In that procedure we can get our desire pulse. Let use Timer1 Compare A vector to set I\/O port<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/*************** Interrupt Enable ****************************\/\n#include&lt;avr\/interrupt.h&gt;\nsei();\t\/\/Global Interrupt Enable\nTIMSK|=(1&lt;&lt;OCIE1A);  \/\/Timer1, Output Compare A Match Interrupt Enable\n\/*********************   Set logic  ****************************\/\nISR(TIMER1_COMPA_vect)\n{\n   \/\/set I\/O port\n}\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-58f53d1043c91b41c8dce762889f71f1 wp-block-paragraph\" style=\"color:#5c5c5c\">Now the clear logic is to check the TCNT1 value and define a TCNT1 condition to clear each I\/O ports. A simple C code as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\nif(TCNT1&gt;=140 &amp;&amp; TCNT1&lt;=249)\n  {\n   if(TCNT1&gt;=140) PORTx &amp;=~(1&lt;&lt;PINx);\n   if(TCNT1&gt;=180) PORTx &amp;=~(1&lt;&lt;PINx);\n-------------------------------------------------------------\n  }\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-251dca38d90fb31a1341c193c8a29868 wp-block-paragraph\" style=\"color:#5c5c5c\">The complete program as follow-<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: cpp; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\n\/****************************************************************************************************\n*****************************************************************************************************\n***************************** Multiple Servo Motor Control ****************************************** \n*****************************************************************************************************\n\t\tSubeer Kumar Sarkar\n\t\tElectrical &amp; Electronic Engineer\n******************************************************************************************************\n******************************************************************************************************\n*****************************************************************************************************\/\n#include&lt;avr\/io.h&gt;\n#include&lt;util\/delay.h&gt;\n#include&lt;avr\/interrupt.h&gt;\nint main(void)\n{\nDDRD|=0x0f;\n\/********************************Fast PWM  prescalling 64*********************************************\/\nTCCR1A|=(1&lt;&lt;WGM11);            \t\t\t\t\t\t   \/\/Normal port operation, OC1A\/OC1B disconnected\nTCCR1B|=(1&lt;&lt;WGM13)|(1&lt;&lt;WGM12)|(1&lt;&lt;CS10)|(1&lt;&lt;CS11);\nTIMSK|=(1&lt;&lt;OCIE1A);\t\t\t\t\t\t\t\t\t   \/\/Timer1, Output Compare A Match Interrupt Enable\t\t\t\t\t\t\nsei();\t\t\t\t\t\t\t\t\t\t\t\t\t   \/\/Globel Interrupt Enable\nICR1=2499;\t\t\t\t\t\t\t\t\t\t           \/\/Time preiod 50Hz\n\/******************************************************************************************************\/\nwhile(1)\n{\n\/**************************************** Clear Logic *************************************************\/\nif(TCNT1&gt;=140 &amp;&amp; TCNT1&lt;=249)\n{\tif(TCNT1&gt;=140) PORTD &amp;=~(1&lt;&lt;PIND0);\n\tif(TCNT1&gt;=180) PORTD &amp;=~(1&lt;&lt;PIND1);\n\tif(TCNT1&gt;=200) PORTD &amp;=~(1&lt;&lt;PIND2);\n\tif(TCNT1&gt;=240) PORTD &amp;=~(1&lt;&lt;PIND3);\n}\n\/*******************************************************************************************************\/\n}\nreturn 0;\n}\n\/*************************************** Set Logic *****************************************************\/\nISR(TIMER1_COMPA_vect)\n{\nPORTD=0x0f;\n}\n\/*******************************************************************************************************\/\n<\/pre><\/div>\n\n\n<p class=\"has-text-color has-link-color wp-elements-af96bb77c5bbd0aad577ac1dc69373e1 wp-block-paragraph\" style=\"color:#5c5c5c\">For crystal setting The FUSE bit as follow-<\/p>\n\n\n\n<p class=\"has-text-color has-link-color wp-elements-80b88c759c09e094f07387e226138d79 wp-block-paragraph\" style=\"color:#252525\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <strong>HFUSE=0xc9<\/strong> &amp; <strong> LFUSE=0xEF<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Multiple-Servo-control.rar\" style=\"border-radius:10px;padding-top:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-bottom:var(--wp--preset--spacing--30);padding-left:var(--wp--preset--spacing--40)\">download<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:66.66%\">\n<p class=\"has-text-color has-link-color has-upper-heading-font-size wp-elements-eba74ed1fb9070dda36a75af052c021f wp-block-paragraph\" style=\"color:#252525\"><a href=\"https:\/\/iotthinghub.com\/wp-content\/uploads\/2024\/08\/Multiple-Servo-control.rar\">Multiple Servo control using ATmega8.rar<\/a><\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A servomotor is a rotary actuator that allows for precise control of angular position, velocity and acceleration. Most modern servomotor are designed and supplied around a dedicated control module. So that servomotor doesn\u2019t require a driver. A microcontroller PWM pin can be used directly to control the servomotor. From picture it is seen that the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-1771","post","type-post","status-publish","format-standard","hentry","category-pwm"],"_links":{"self":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1771"}],"version-history":[{"count":14,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1771\/revisions"}],"predecessor-version":[{"id":2615,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=\/wp\/v2\/posts\/1771\/revisions\/2615"}],"wp:attachment":[{"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iotthinghub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}