{"id":430,"date":"2021-09-20T05:30:48","date_gmt":"2021-09-20T05:30:48","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=430"},"modified":"2021-09-20T05:30:50","modified_gmt":"2021-09-20T05:30:50","slug":"working-with-stm32-and-timers-part-5-encoder-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=430","title":{"rendered":"Working with STM32 and Timers part 5: Encoder Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-medium is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-300x300.jpg\" alt=\"\" class=\"wp-image-431\" width=\"300\" height=\"300\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-300x300.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-150x150.jpg 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-768x768.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-750x750.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-400x400.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1-250x250.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/rotary-encoder-module-800x800-1.jpg 800w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><figcaption>Encoder<\/figcaption><\/figure><\/div>\n\n\n\n<p>In previous guides, we looked at TIM2 of STM32 to generate delay, interrupts,  PWM and how to use PWM to fade LED and control servo motors. In this guide, we shall cover how to use TIM2 of STM32 to read shaft encoder.<\/p>\n\n\n\n<p>We will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is encoder<\/li><li>Required parts and connection<\/li><li>Configure pins and timer to read encoder<\/li><li>Download the code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.1 What is encoder:<\/h2>\n\n\n\n<p>A\u00a0<strong>rotary encoder<\/strong>, also called a\u00a0<strong>shaft encoder<\/strong>, is an\u00a0electro-mechanical\u00a0device that converts the\u00a0angular\u00a0position or motion of a shaft or axle to analog or digital output signals. <\/p>\n\n\n\n<p>Rotary encoders are used in a wide range of applications that require monitoring or control, or both, of mechanical systems, including industrial controls,\u00a0robotics,\u00a0photographic lenses,\u00a0computer input devices such as optomechanical\u00a0mice\u00a0and\u00a0trackballs, controlled stress\u00a0rheometers, and rotating\u00a0radar\u00a0platforms.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Encoder types:<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Mechanical<\/strong>: Also known as conductive encoders. A series of circumferential copper tracks etched onto a PCB is used to encode the information via contact brushes sensing the conductive areas. Mechanical encoders areeconomical but susceptible to mechanical wear. They are common in human interfaces such as\u00a0digital multimeters<\/li><li><strong>Optical<\/strong>: This uses a light shining onto a\u00a0photodiode\u00a0through slits in a metal or glass disc. Reflective versions also exist. This is one of the most common technologies. Optical encoders are very sensitive to dust.<\/li><li><strong>On-Axis Magnetic<\/strong>: This technology typically uses a specially magnetized 2 pole neodymium magnet attached to the motor shaft. Because it can be fixed to the end of the shaft, it can work with motors that only have 1 shaft extending out of the motor body. The accuracy can vary from a few degrees to under 1 degree. Resolutions can be as low as 1 degree or as high as 0.09 degree (4000 CPR, Count per Revolution).\u00a0Poorly designed internal interpolation can cause output jitter, but this can be overcome with internal sample averaging.<\/li><li><strong>Off-Axis Magnetic<\/strong>: This technology typically employs the use of rubber bonded ferrite magnets attached to a metal hub. This offers flexibility in design and low cost for custom applications. Due to the flexibility in many off axis encoder chips they can be programmed to accept any number of pole widths so the chip can be placed in any position required for the application. Magnetic encoders operate in harsh environments where optical encoders would fail to work. (from wikipedia)<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1.3 Encoder Output:<\/h2>\n\n\n\n<p>In out case, the encoder shall generate pulses on both DT and CLK lines in a certain manner that can be read by the timer of stm32. In the figure below, is the output from shaft encoder when rotating clockwise. Clk is yellow and DT is Blue.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1.png\" alt=\"\" class=\"wp-image-432\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1.png 800w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1-300x180.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1-768x461.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1-750x450.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1-400x240.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint1-250x150.png 250w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><figcaption>Clockwise rotation<\/figcaption><\/figure><\/div>\n\n\n\n<p>When the shaft encoder rotate clockwise, CLK like pulled high first then DT line which indicates there is rotation.<\/p>\n\n\n\n<p>When the counter clock rotation occurs, the DT line is pulled high first then the CLK line.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"480\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/DS1Z_QuickPrint2.png\" alt=\"\" class=\"wp-image-433\" \/><figcaption>Counter clockwise rotation<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 Required Parts:<\/h2>\n\n\n\n<p>In this guide, we will need the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>STM32F411Re Nucleo-64.<\/li><li>Shaft encoder (4 wire).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 Connection:<\/h2>\n\n\n\n<p>The connection is as following<\/p>\n\n\n\n<figure class=\"wp-block-table aligncenter\"><table><tbody><tr><td>Encoder Pin<\/td><td>STM32F411-Nucleo64 Pin<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>+<\/td><td>3V3<\/td><\/tr><tr><td>DT<\/td><td>PA0<\/td><\/tr><tr><td>CLK<\/td><td>PA1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"768\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-768x1024.jpg\" alt=\"\" class=\"wp-image-434\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-768x1024.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-225x300.jpg 225w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-1152x1536.jpg 1152w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-1536x2048.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-1150x1533.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-750x1000.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-400x533.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-250x333.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6504-scaled.jpg 1920w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><figcaption>actual connection<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.1 Pin Configuration:<\/h2>\n\n\n\n<p>For pin configuration, it will be the same as timer guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=363\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=363\" target=\"_blank\">here<\/a>).<\/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;}\">RCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOAEN;\nGPIOA-&gt;MODER|=(1&lt;&lt;1)|(1&lt;&lt;3);\nGPIOA-&gt;AFR[0]|=(1&lt;&lt;0)|(1&lt;&lt;4);<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3.2 Timer Configuration:<\/h2>\n\n\n\n<p>In order to config the timer to read encoder, the following steps are required:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"370\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-1024x370.png\" alt=\"\" class=\"wp-image-436\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-1024x370.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-300x108.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-768x277.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-1536x555.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-1150x415.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-750x271.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-400x145.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM-250x90.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.11.24-AM.png 1910w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>steps<\/figcaption><\/figure><\/div>\n\n\n\n<p>First we start by enabling clock access to TIM2 of stm32<\/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;}\">RCC-&gt;APB1ENR|=RCC_APB1ENR_TIM2EN;<\/pre><\/div>\n\n\n\n<p>Then set the maximum value for timer to count as following<\/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;}\">TIM2-&gt;ARR = max_value;<\/pre><\/div>\n\n\n\n<p>then set bot CC1S and CC2S as following<\/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;}\">TIM2-&gt;CCMR1 |= (TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0 ); <\/pre><\/div>\n\n\n\n<p>Set both channel to input as following<\/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;}\">TIM2-&gt;CCER &amp;= ~(TIM_CCER_CC1P | TIM_CCER_CC2P);<\/pre><\/div>\n\n\n\n<p>Now, we tell the TIM to operate in encoder mode as following<\/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;}\">TIM2-&gt;SMCR |= TIM_SMCR_SMS_0 | TIM_SMCR_SMS_1;<\/pre><\/div>\n\n\n\n<p>Finally enable timer as following<\/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;}\">TIM2-&gt;CR1 |= TIM_CR1_CEN ;<\/pre><\/div>\n\n\n\n<p>To read the encoder just read the current timer counter as following<\/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;}\">uint32_t read_encoder(void)\n\t{\n\t\n\t\treturn TIM2-&gt;CNT;\n\t\n\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4.1 Code:<\/h2>\n\n\n\n<p>You can download the entire project from here (it uses UART to display the results)<\/p>\n\n\n\n<div class=\"wp-block-file aligncenter\"><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/encoder.zip\">encoder<\/a><a href=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/encoder.zip\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p>After you compile the code, upload to your board. Open your favourite terminal (TeraTerm in my case) and set the baud rate to 9600 rotate the encoder and you shall get this<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"533\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-1024x533.png\" alt=\"\" class=\"wp-image-438\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-1024x533.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-300x156.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-768x400.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-1150x598.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-750x390.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-400x208.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM-250x130.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/Screen-Shot-2021-09-20-at-8.22.19-AM.png 1326w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Results<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5.1 Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/09\/IMG_6505.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In previous guides, we looked at TIM2 of STM32 to generate delay, interrupts, PWM and how to use PWM to fade LED and control servo motors. In this guide, we shall cover how to use TIM2 of STM32 to read shaft encoder. We will cover the following: What is encoder Required parts and connection Configure [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-430","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/430"}],"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=430"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":440,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/430\/revisions\/440"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}