{"id":594,"date":"2021-11-25T05:14:05","date_gmt":"2021-11-25T05:14:05","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=594"},"modified":"2021-11-25T05:14:07","modified_gmt":"2021-11-25T05:14:07","slug":"working-with-stm32-and-motors-part-3-nema-17-stepper-motors","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=594","title":{"rendered":"Working with STM32 and Motors part 3 : NEMA 17 Stepper Motors"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ae01.alicdn.com\/kf\/HTB1c039AL1TBuNjy0Fjq6yjyXXaR\/Wantai-CNC-Nema-17-Stepper-Motor-42BYGHW811-06-0-6N-84oz-in-48mm-2-5A-CE.jpg_Q90.jpg_.webp\" alt=\"\n\" width=\"500\" height=\"500\" \/><\/figure><\/div>\n\n\n\n<p>In the previous guide (<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.embeddedexpert.io\/?p=495\" data-type=\"URL\" data-id=\"https:\/\/blog.embeddedexpert.io\/?p=495\" target=\"_blank\">here<\/a>), we took a look how to control 28BYJ-48 stepper Motor. In this guide, we shall see how control NEMA17 stepper motor which is the most used stepper motor in 3D printers, CNC, robots etc. <\/p>\n\n\n\n<p>In this guide, we will cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>NEMA17 Stepper Motor<\/li><li>A4988 Stepper Motor Driver<\/li><li>Connection diagram<\/li><li>Code<\/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. NEMA17 Stepper Motor:<\/h2>\n\n\n\n<p><strong>NEMA 17<\/strong>\u00a0is a\u00a0<strong>hybrid stepping motor<\/strong>\u00a0with a 1.8\u00b0 step angle (200 steps\/revolution). Each phase draws 1.2 A at 4 V, allowing for a holding torque of 3.2 kg-cm. NEMA 17 Stepper motor is generally used in Printers, CNC machines and Laser Cutters.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Pin Configuration<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td><strong>NO.<\/strong><\/td><td><strong>Pin Name<\/strong><\/td><td><strong>Wire<\/strong><strong>Colour<\/strong><\/td><td><strong>Description<\/strong><\/td><\/tr><tr><td>1<\/td><td>Coil 1<\/td><td> Black<\/td><td>&nbsp;This motor has six wires, connected to two split windings. Black, Yellow, Green wires is part of first winding while Red, White and Blue is part of second winding.<\/td><\/tr><tr><td>2<\/td><td>Coil 2<\/td><td> Yellow<\/td><\/tr><tr><td>3<\/td><td>Coil 3<\/td><td> Green<\/td><\/tr><tr><td>4<\/td><td>Coil 4<\/td><td>\u00a0\u00a0Red<\/td><\/tr><tr><td>5<\/td><td>Coil 5<\/td><td> White<\/td><\/tr><tr><td>6<\/td><td>Coil 6  <\/td><td> Blue<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>NEMA 17 Stepper Motor Technical Specifications<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Rated Voltage: 12V DC<\/li><li>Current: 1.2A at 4V<\/li><li>Step Angle: 1.8 deg.<\/li><li>No. of Phases: 4<\/li><li>Motor Length: 1.54 inches<\/li><li>4-wire, 8 inch lead<\/li><li>200 steps per revolution, 1.8 degrees<\/li><li>Operating Temperature: -10 to 40 \u00b0C<\/li><li>Unipolar Holding Torque: 22.2 oz-in<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"260\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-1024x260.png\" alt=\"\" class=\"wp-image-595\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-1024x260.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-300x76.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-768x195.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-1536x389.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-2048x519.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-1150x292.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-750x190.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-400x101.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.40.53-AM-250x63.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption>Specification<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. A4988 Stepper Motor driver<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/static.cytron.io\/image\/cache\/catalog\/products\/SD-A4988\/SD-A4988-800x800.jpg\" alt=\"A4988 Stepper Motor Driver Module\" width=\"400\" height=\"400\" \/><figcaption>A4988 Stepper Motor driver<\/figcaption><\/figure><\/div>\n\n\n\n<p>The A4988 is a complete microstepping motor driver with built-in translator for easy operation. It is designed to operate bipolar stepper motors in full-, half-, quarter-, eighth-, and sixteenth-step modes, with an output drive capacity of up to 35&nbsp;V and \u00b12&nbsp;A. The A4988 includes a fixed off-time current regulator which has the ability to operate in slow or mixed decay modes.&nbsp;<\/p>\n\n\n\n<p>The translator is the key to the easy implementation of the A4988. Simply inputting one pulse on the STEP input drives the motor one microstep. There are no phase sequence tables, high frequency control lines, or complex interfaces to program. The A4988 interface is an ideal fit for applications where a complex microprocessor is unavailable or is overburdened.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Top Features<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Low R<sub>DS(ON)<\/sub>\u00a0outputs\u00a0<\/li><li>Automatic current decay mode detection\/selection\u00a0<\/li><li>Mixed and Slow current decay modes\u00a0<\/li><li>Synchronous rectification for low power dissipation\u00a0<\/li><li>Internal UVLO\u00a0<\/li><li>Crossover-current protection\u00a0<\/li><li>3.3 and 5\u00a0V compatible logic supply\u00a0<\/li><li>Thermal shutdown circuitry\u00a0<\/li><li>Short-to-ground protection\u00a0<\/li><li>Shorted load protection\u00a0<\/li><li>Five selectable step modes: full, 1\/2, 1\/4, 1\/8, and 1\/16<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/ae01.alicdn.com\/kf\/HTB1uCoiXZfrK1RjSszcq6xGGFXa0.jpg\" alt=\"3D Printer Parts StepStick DRV8825\/A4988 Stepper Motor Driver Carrier  Reprap 4 layer PCB RAMPS replace A4988|Motor Driver| - AliExpress\" \/><figcaption>Typical connection<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">3. Connection Diagram:<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"785\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-785x1024.png\" alt=\"\" class=\"wp-image-596\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-785x1024.png 785w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-230x300.png 230w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-768x1001.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-750x978.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-400x522.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM-250x326.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/Screen-Shot-2021-11-25-at-7.56.15-AM.png 994w\" sizes=\"(max-width: 785px) 100vw, 785px\" \/><\/figure>\n\n\n\n<p>We shall connect STEP pin to PA0 of STM32F411 and the DIR pin to PA1.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Code:<\/h2>\n\n\n\n<p>We shall set PA0 to generate the PWM signal, for how to configure timer 2 of STM32F411 as PWM, please check this topic (<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<p>In this guide, we shall change the PWM frequency while maintaining the duty cycle fixed to 50%.<\/p>\n\n\n\n<p>In order to change the frequency of the PWM, the prescaler can be modified during the running of the code<\/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;stm32f4xx.h&quot;                  \/\/ Device header\n\nint main(void)\n\t{\n\t#define TIM2_AF 0x01\n\tRCC-&gt;AHB1ENR\t|=\tRCC_AHB1ENR_GPIOAEN;\n\tGPIOA-&gt;MODER\t|=\tGPIO_MODER_MODE0_1;\n\tGPIOA-&gt;MODER\t&amp;= ~GPIO_MODER_MODE0_0;\n\tGPIOA-&gt;MODER\t|= GPIO_MODER_MODE1_0;\n\tGPIOA-&gt;AFR[0] |= (TIM2_AF&lt;&lt;0); \n\t\n\tRCC-&gt;APB1ENR  |=  RCC_APB1ENR_TIM2EN;\n\tTIM2-&gt;CR1     &amp;=~ TIM_CR1_CEN;\n\tTIM2-&gt;CCMR1|=TIM_CCMR1_OC1M_2|TIM_CCMR1_OC1M_1;\n\tTIM2-&gt;CCER|=TIM_CCER_CC1E;\n\tTIM2-&gt;PSC=40000-1; \n\tTIM2-&gt;ARR=10; \n\tTIM2-&gt;CCR1=5;\n\tTIM2-&gt;CR1     |= TIM_CR1_CEN;\n\twhile(1)\n\t\t\t\t{\n\t\t\t\t\n\t\t\t\t\t\/*change the PWM frequency*\/\n\t\t\t\tfor(int i=40000;i&gt;1600;i--)\n\t\t\t\t\t{\n\t\t\t\t\tTIM2-&gt;PSC=i;\n\t\t\t\t\tfor(int j=0;j&lt;1000;j++);\n\t\t\t\t\t}\n\t\t\t\t\t\/*delay for a little bit*\/\n\t\t\t\t\tfor (int k=0;k&lt;1000000;k++);\n\t\t\t\t\t\/*disable timer to stop the stepper motor*\/\t\t\t\t\t\n\t\t\t\tTIM2-&gt;CR1     &amp;=~ TIM_CR1_CEN;\n\t\t\t\t\t\/*delay for a little bit*\/\n\t\t\t\tfor (int k=0;k&lt;1000000;k++){;}\n\t\t\t\t\t\/*Change the direction*\/\n\t\t\t\tGPIOA-&gt;ODR^=GPIO_ODR_OD1;\n\t\t\t\t\t\/*Enable timer once again*\/\n\t\t\t\tTIM2-&gt;CR1     |= TIM_CR1_CEN;\n\t\t\t\t}\n\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5.Demo<\/h2>\n\n\n\n<figure class=\"wp-block-video\"><video controls src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/11\/IMG_7213.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In the previous guide (here), we took a look how to control 28BYJ-48 stepper Motor. In this guide, we shall see how control NEMA17 stepper motor which is the most used stepper motor in 3D printers, CNC, robots etc. In this guide, we will cover the following: NEMA17 Stepper Motor A4988 Stepper Motor Driver Connection [&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-594","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\/594"}],"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=594"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/594\/revisions"}],"predecessor-version":[{"id":599,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/594\/revisions\/599"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=594"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=594"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=594"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}