{"id":2396,"date":"2024-03-16T13:25:14","date_gmt":"2024-03-16T13:25:14","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2396"},"modified":"2024-03-16T13:25:17","modified_gmt":"2024-03-16T13:25:17","slug":"getting-started-with-stm32g0-and-stm32cubeide-timer-in-encoder-mode","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2396","title":{"rendered":"Getting Started with STM32G0 and STM32CubeIDE: Timer in Encoder Mode"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"400\" height=\"400\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-2.webp\" alt=\"\" class=\"wp-image-2397\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-2.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-2-300x300.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-2-150x150.webp 150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/pt156578902-100_original_arm_mcu_stm32g070kbt6_stm32g070_stm32g_lqfp_32_microcontroller.jpg-2-2-250x250.webp 250w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall take a look at the encoder types and how to use it with STM32G070.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is encoder.<\/li><li>Connection.<\/li><li>Driver development.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is Encoder:<\/h2>\n\n\n\n<p>A&nbsp;<strong>rotary encoder<\/strong>, also called a&nbsp;<strong>shaft encoder<\/strong>, is an&nbsp;electro-mechanical&nbsp;device that converts the&nbsp;angular&nbsp;position or motion of a shaft or axle to analog or digital output signals.&nbsp;<\/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,&nbsp;robotics,&nbsp;photographic lenses,&nbsp;computer input devices such as optomechanical&nbsp;mice&nbsp;and&nbsp;trackballs, controlled stress&nbsp;rheometers, and rotating&nbsp;radar&nbsp;platforms.<\/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&nbsp;digital multimeters<\/li><li><strong>Optical<\/strong>: This uses a light shining onto a&nbsp;photodiode&nbsp;through 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).&nbsp;Poorly 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<figure class=\"wp-block-image\"><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>\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<figure class=\"wp-block-image\"><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>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">2. Encoder Connection:<\/h2>\n\n\n\n<p>The module is shown below:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"315\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/rotary-decoder-encoder-module-ky-040-for-arduino-tech1197-2665-600x315w.webp\" alt=\"\" class=\"wp-image-2398\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/rotary-decoder-encoder-module-ky-040-for-arduino-tech1197-2665-600x315w.webp 600w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/rotary-decoder-encoder-module-ky-040-for-arduino-tech1197-2665-600x315w-300x158.webp 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/rotary-decoder-encoder-module-ky-040-for-arduino-tech1197-2665-600x315w-400x210.webp 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/rotary-decoder-encoder-module-ky-040-for-arduino-tech1197-2665-600x315w-250x131.webp 250w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<p>The connection is as following<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>Encoder Pin<\/td><td>STM32G070<\/td><\/tr><tr><td>GND<\/td><td>GND<\/td><\/tr><tr><td>+<\/td><td>3V3<\/td><\/tr><tr><td>DT<\/td><td>PA6<\/td><\/tr><tr><td>CLK<\/td><td>PA7<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Driver Development:<\/h2>\n\n\n\n<p>We start off by creating new STM32 Project and name it encoder.<\/p>\n\n\n\n<p>From Pinout and Configuration, select Timer3:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"585\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-1024x585.jpg\" alt=\"\" class=\"wp-image-2399\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-1024x585.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-300x171.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-768x439.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-1536x878.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-2048x1170.jpg 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-1150x657.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-750x429.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-400x229.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-06-39-250x143.jpg 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Select the combined channel and select Encoder Mode.<\/p>\n\n\n\n<p>This will automatically select PA6 and PA7 as encoder mode.<\/p>\n\n\n\n<p>In parameters Settings:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"798\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-1024x798.jpg\" alt=\"\" class=\"wp-image-2400\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-1024x798.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-300x234.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-768x599.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-750x585.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-400x312.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21-250x195.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/2024-03-16_15-37-21.jpg 1116w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Set encoder mode to TI1 and TI2.<\/p>\n\n\n\n<p>Input filler to 10 or increase it according to your application.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Also, we need UART, please refer to this guide <a href=\"https:\/\/blog.embeddedexpert.io\/?p=2316\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=2316\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Save the code project and it will generate the required.<\/p>\n\n\n\n<p>In main.c<\/p>\n\n\n\n<p>In user begin includes, include the following code header file:<\/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;stdio.h&quot;<\/pre><\/div>\n\n\n\n<p>In user begin PV, declare the 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;}\">uint8_t uart_data[100];\nuint16_t encoder_previous;<\/pre><\/div>\n\n\n\n<p>In user code begin 2:<\/p>\n\n\n\n<p>Start the timer 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;}\">HAL_TIM_Encoder_Start(&amp;htim3, TIM_CHANNEL_ALL);<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In user code begin 3:<\/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;}\">  if(TIM3-&gt;CNT != encoder_previous)\n  {\n\n\t  uint32_t len =sprintf(uart_data,&quot;Encoder Reading %d\\r\\n&quot;,TIM3-&gt;CNT);\n\n\t  HAL_UART_Transmit(&amp;huart2, uart_data, len, 100);\n\n\t  encoder_previous=TIM3-&gt;CNT;\n\n  }<\/pre><\/div>\n\n\n\n<p>Check if the current value of the timer is not equal to the store value, if it is not, print the new value and store the current value.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Upload the code to your board and open serial terminal and set the baud rate to 115200. Rotate the encoder and you should get the following:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1005\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-1005x1024.png\" alt=\"\" class=\"wp-image-2401\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-1005x1024.png 1005w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-294x300.png 294w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-768x782.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-1150x1172.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-750x764.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-400x408.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM-250x255.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/03\/Screenshot-2024-03-16-at-3.41.43\u202fPM.png 1168w\" sizes=\"(max-width: 1005px) 100vw, 1005px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy  coding \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at the encoder types and how to use it with STM32G070. In this guide, we shall cover the following: What is encoder. Connection. Driver development. Results. 1. What is Encoder: A&nbsp;rotary encoder, also called a&nbsp;shaft encoder, is an&nbsp;electro-mechanical&nbsp;device that converts the&nbsp;angular&nbsp;position or motion of a shaft or [&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-2396","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\/2396"}],"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=2396"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2396\/revisions"}],"predecessor-version":[{"id":2402,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2396\/revisions\/2402"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}