{"id":495,"date":"2021-10-14T03:41:32","date_gmt":"2021-10-14T03:41:32","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=495"},"modified":"2022-06-03T13:46:48","modified_gmt":"2022-06-03T13:46:48","slug":"working-with-stm32-and-motors-part-1-stepper-motor","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=495","title":{"rendered":"Working with STM32 and Motors part 1 : Stepper Motor"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/robu.in\/wp-content\/uploads\/2016\/05\/28BYJ-48-Stepper-Motor-and-ULN2003-Stepper-Motor-Driver-Good-Quality.jpg\" alt=\"Buy 28BYJ-48 Stepper Motor with ULN2003 Motor Driver Online\" width=\"400\" height=\"400\"\/><\/figure><\/div>\n\n\n\n<p>In this guide,, we will learn to interface a stepper motor with STM32F411RE Nucleo-64. There are many options available for stepper motors. But in this tutorial, we will use an inexpensive&nbsp;28BYJ-48 stepper motor&nbsp;for interfacing with STM32F411. Because it comes with a UL2003 driver. Moreover, it is a low-cost solution to learn about stepper motors control and working. But the concepts learned with this type can be easily applied to other high torque industrial stepper motors also.<\/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>28BYJ-48 Stepper Motor<\/li><li>Connection diagram<\/li><li>Code<\/li><li>Demo<\/li><\/ul>\n\n\n\n<h2 class=\"has-text-align-left wp-block-heading\">1.1 28BYJ-48 Stepper Motor pin configuration:<\/h2>\n\n\n\n<p> <\/p>\n\n\n\n<p>The following figure shows the pinout diagram of 28BYJ-48 stepper motor. It consists of 5 pins. Out of these 5 pins, four pins are used to provide sequence logic to the coils and one pin is +5 volts supply pin.&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/microcontrollerslab.com\/wp-content\/uploads\/2020\/12\/28BYJ-48-stepper-motor-pinout-diagram.jpg\" alt=\"28BYJ-48 stepper motor pinout diagram\" class=\"wp-image-20312\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">1.2 Pin Configuration Details<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td class=\"has-text-align-center\" data-align=\"center\">Pin<\/td><td class=\"has-text-align-center\" data-align=\"center\">Name<\/td><td class=\"has-text-align-center\" data-align=\"center\">Color<\/td><td class=\"has-text-align-center\" data-align=\"center\">Functionality<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">1<\/td><td class=\"has-text-align-center\" data-align=\"center\">Coil 1<\/td><td class=\"has-text-align-center\" data-align=\"center\">Orange<\/td><td class=\"has-text-align-center\" data-align=\"center\">These are coils used to control the step sequence of the stepper motor. One end of each coil is connected with +5V and the other end will be connected with ULN2003 driver output.&nbsp;<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">2<\/td><td class=\"has-text-align-center\" data-align=\"center\">Coil 2<\/td><td class=\"has-text-align-center\" data-align=\"center\">Pink<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">3<\/td><td class=\"has-text-align-center\" data-align=\"center\">5V<\/td><td class=\"has-text-align-center\" data-align=\"center\">Red<\/td><td class=\"has-text-align-center\" data-align=\"center\">Used to apply +5 volt supply to the stepper motor. This voltage appears across the coils when a specific coil is ground through a control sequence.&nbsp;<\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">4<\/td><td class=\"has-text-align-center\" data-align=\"center\">Coil 3<\/td><td class=\"has-text-align-center\" data-align=\"center\">Yellow<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><tr><td class=\"has-text-align-center\" data-align=\"center\">5<\/td><td class=\"has-text-align-center\" data-align=\"center\">Coil 4<\/td><td class=\"has-text-align-center\" data-align=\"center\">Blue<\/td><td class=\"has-text-align-center\" data-align=\"center\"><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>The specifications of the S8BYJ-48 stepper motor are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It is a unipolar 5 pin coil with a rated DC voltage of 5V.<\/li><li>It has 4 phases with a stride angle of 5.625\u00b0\/64.<\/li><li>The frequency of this stepper motor is 100Hz and insulated power is 600VAC\/1mA\/1s.<\/li><li>The half-step method is recommended for driving this stepper motor.<\/li><li>The value of pull in torque for a stepper motor is 300 gf.cm.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-uln2003-stepper-motor-driver-module\">1.3 ULN2003 Stepper Motor Driver Module<\/h2>\n\n\n\n<p>Now the first question which comes to your mind is why do we need a ULN2003 driver to drive stepper motors? This is because the current consumption of 28BYJ-48 is around 240mA. That means the current required to drive coils by applying a sequence of control signals is also almost 200mA. GPIO pins of STM32F411 can not provide current of this magnitude. Therefore, we need a ULN2003 driver which translates low current output of TM4C123 GPIO pins into higher current that meets the requirement of stepper motor control signals.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-stepper-motor-driver-module-pinout\">Stepper Motor Driver Module Pinout<\/h3>\n\n\n\n<p>ULN2003 driver IC consists of 7 darlington pair transistor outputs. Each output can drive 500mA and 50V load. The input to each 7 darlington pair transistor can be a signal from the microcontroller such as STM32F411 microcontroller. To drive a stepper motor, this driver board used only four outputs. The following diagram shows the ULN2003 motor driver board and details of their components:&nbsp;<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img decoding=\"async\" src=\"https:\/\/i2.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01.png?resize=750%2C429&amp;quality=100&amp;strip=all&amp;ssl=1\" alt=\"ESP32 Stepper Motor (28BYJ-48 and ULN2003 Driver) | Random Nerd Tutorials\"\/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2.1 Connection:<\/h2>\n\n\n\n<p>So far, we learnt about the stepper motor and it&#8217;s driver. Let&#8217;s continue with the actual connection to STM32F411 and how to code <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"729\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-1024x729.png\" alt=\"\" class=\"wp-image-497\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-1024x729.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-300x213.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-768x546.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-1536x1093.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-2048x1457.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-1150x818.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-750x534.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-400x285.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2021\/10\/Screen-Shot-2021-10-14-at-6.18.21-AM-250x178.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>PC0-&gt;IN1<\/p>\n\n\n\n<p>PC1-&gt;IN2<\/p>\n\n\n\n<p>PC2-&gt;IN3<\/p>\n\n\n\n<p>PC4-&gt;IN4<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2.2 Driving Modes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-full-drive-sequence-mode\">Full Drive Sequence Mode<\/h3>\n\n\n\n<p>In this mode, two coils are energized at a time that means two windings of stepper motor energized together. Therefore, motor runs at full torque. We apply this sequence to input pins of the stepper motor through a UNL2003 driver.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Step<\/th><th>Blue<\/th><th>Pink<\/th><th>Yellow<\/th><th>Orange<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>2<\/td><td>0<\/td><td>1<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>3<\/td><td>0<\/td><td>0<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>4<\/td><td>1<\/td><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-half-drive-sequence-mode\">Half Drive Sequence Mode<\/h3>\n\n\n\n<p>In this mode, we control the phases or coils with alternate one and two phase control as shown in the table below. But, in this mode, motor runs at low torque.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Step<\/th><th>Blue<\/th><th>Pink<\/th><th>Yellow<\/th><th>Orange<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>2<\/td><td>1<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>3<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>4<\/td><td>0<\/td><td>1<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>5<\/td><td>0<\/td><td>0<\/td><td>1<\/td><td>1<\/td><\/tr><tr><td>6<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>7<\/td><td>1<\/td><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><tr><td>8<\/td><td>1<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-wave-drive-mode\">Wave Drive Mode&nbsp;<\/h3>\n\n\n\n<p>Like full-drive mode, only one coil is turned on at a time but with a drive sequence as shown below. The only advantage of this method is that a stepper motor consumes less power.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><thead><tr><th>Step<\/th><th>Blue<\/th><th>Pink<\/th><th>Yellow<\/th><th>Orange<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>1&nbsp;<\/td><td>0<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>2<\/td><td>0<\/td><td>1<\/td><td>0<\/td><td>0<\/td><\/tr><tr><td>3<\/td><td>0<\/td><td>0<\/td><td>1<\/td><td>0<\/td><\/tr><tr><td>4<\/td><td>0<\/td><td>0<\/td><td>0<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>We shall use Full drive sequence mode<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Code:<\/h2>\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\/\/delay in millisecond\nvoid Delay_ms(int delay)\n\n{\n\nint i;\n\nfor(; delay&gt;0 ;delay--)\n\n{\n\nfor(i =0; i&lt;3195;i++);\n\n}\n\n}\n\n\n\nint main(void)\n\t{\n\t\/\/Enable clock access to GPIOC\n\tRCC-&gt;AHB1ENR|=RCC_AHB1ENR_GPIOCEN;\n\t\/\/Set PC0, PC1, PC2 and PC3 to output\n\tGPIOC-&gt;MODER|=GPIO_MODER_MODE0_0|GPIO_MODER_MODE1_0|GPIO_MODER_MODE2_0|GPIO_MODER_MODE3_0;\n\t\n\twhile(1)\n\t\t{\n\t\tfor(int i=0; i&lt;500; i++) \/\/ take 500 steps to complete one revolution \n\t\t{\n\t\t\/\/apply full drive clockwise rotation sequence \n\t  GPIOC-&gt;ODR = 0x08;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x04;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x02;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x01;\n    Delay_ms(4);\n}\nDelay_ms(1000);\nfor(int i=0; i&lt;100; i++) \/\/ rotate 100 steps counter clockwise \n\t\t{\n\t\t\n\t  GPIOC-&gt;ODR = 0x01;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x02;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x04;\n    Delay_ms(4);\n    GPIOC-&gt;ODR = 0x08;\n    Delay_ms(4);\n}\n\n\t\tDelay_ms(1000);\n\t\t}\n\t\n\t}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">4. Demo<\/h2>\n\n\n\n<p>Note: My stepper motor sometimes gets stuck for some reason. <\/p>\n\n\n\n<p>Happy Coding \ud83d\ude00 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide,, we will learn to interface a stepper motor with STM32F411RE Nucleo-64. There are many options available for stepper motors. But in this tutorial, we will use an inexpensive&nbsp;28BYJ-48 stepper motor&nbsp;for interfacing with STM32F411. Because it comes with a UL2003 driver. Moreover, it is a low-cost solution to learn about stepper motors control [&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-495","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\/495"}],"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=495"}],"version-history":[{"count":3,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions"}],"predecessor-version":[{"id":995,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/495\/revisions\/995"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}