{"id":106084,"date":"2021-08-31T10:54:37","date_gmt":"2021-08-31T10:54:37","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=106084"},"modified":"2021-08-31T11:19:38","modified_gmt":"2021-08-31T11:19:38","slug":"esp8266-nodemcu-stepper-motor-28byj-48-uln2003","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-stepper-motor-28byj-48-uln2003\/","title":{"rendered":"ESP8266 NodeMCU with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to control a stepper motor with the ESP8266 NodeMCU board. We&#8217;ll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. We&#8217;ll program the ESP8266 board using Arduino IDE.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU with Stepper Motor 28BYJ-48 and ULN2003 Motor Driver Arduino IDE\" class=\"wp-image-106093\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p>We have a similar tutorial for the ESP32 board: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-stepper-motor-28byj-48-uln2003\/\">ESP32 with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Parts Required<\/h2>\n\n\n\n<p>To follow this tutorial, you need the following parts:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/makeradvisor.com\/tools\/28byj-48-stepper-motor-uln2003\/\" target=\"_blank\" rel=\"noreferrer noopener\">28BYJ-48 Stepper Motor<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/28byj-48-stepper-motor-uln2003\/\" target=\"_blank\" rel=\"noreferrer noopener\">ULN2003 Motor Driver<\/a><\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/esp8266-esp-12e-nodemcu-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP8266 <\/a>(read <a href=\"https:\/\/makeradvisor.com\/best-esp8266-wi-fi-development-board\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP8266 Development Boards<\/a>)<\/li><li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper Wires<\/a><\/li><li>5V Power Supply<\/li><\/ul>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introducing Stepper Motors<\/h2>\n\n\n\n<p>A stepper motor is a brushless DC electric motor that divides a full rotation into a number of steps. It moves one step at a time, and each step is the same size. This allows us to rotate the motor by a precise angle to a precise position. The stepper motor can rotate clockwise or counterclockwise. <\/p>\n\n\n\n<p>The following picture shows two 28BYJ-48 stepper motors.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-Stepper-Motors.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"28BYJ-48 Stepper Motors\" class=\"wp-image-105431\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-Stepper-Motors.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-Stepper-Motors.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>Stepper motors are made of internal coils that make the motor shaft move in steps in one direction or the other when current is applied to the coils in a specific way.<\/p>\n\n\n\n<p>There are two types of stepper motors: unipolar and bipolar stepper motors.<\/p>\n\n\n\n<p>In this article, we won&#8217;t detail how the stepper motors are made and how they work internally. To learn in more detail how they work and the differences between each type of stepper motor, we recommend reading this article by the <a href=\"https:\/\/dronebotworkshop.com\/stepper-motors-with-arduino\/\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">DroneBotWorkshop blog<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">28BYJ-48 Stepper Motor<\/h3>\n\n\n\n<p>There are several stepper motors with different specifications. This tutorial will cover the widely used <strong>28BYJ-48 unipolar stepper motor<\/strong> with the <strong>ULN2003 motor driver<\/strong>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-connected-to-ULN2003-Motor-Driver-01-module.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"28BYJ-48 connected to ULN2003 Motor Driver 01 module\" class=\"wp-image-105430\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-connected-to-ULN2003-Motor-Driver-01-module.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-connected-to-ULN2003-Motor-Driver-01-module.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">28BYJ-48 Stepper Motor Features<\/h4>\n\n\n\n<p>Features of the stepper motor (for more details, consult the datasheet):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Rated voltage: 5V DC<\/li><li>Number of phases: 4<\/li><li>Speed variation ratio: 1\/64<\/li><li>Stride angle: 5.625\u00ba\/64<\/li><li>Frequency: 100Hz<\/li><\/ul>\n\n\n\n<p>The 28BYJ-48 stepper motor has a total of four coils. One end of the coils is connected to 5V, which corresponds to the motor&#8217;s red wire. The other end of the coils corresponds to the wires with blue, pink, yellow, and orange color. Energizing the coils in a logical sequence makes the motor move one step in one direction or the other.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"704\" height=\"434\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-stepper-motor-coils.png?resize=704%2C434&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"28BYJ-48 stepper motor coils\" class=\"wp-image-105372\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-stepper-motor-coils.png?w=704&amp;quality=100&amp;strip=all&amp;ssl=1 704w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/28BYJ-48-stepper-motor-coils.png?resize=300%2C185&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 704px) 100vw, 704px\" \/><\/figure><\/div>\n\n\n\n<p>The 28BYJ-48 Stepper Motor has a stride angle of 5.625\u00b0\/64 in half-step mode. This means that the motor has a step angle of 5.625\u00ba\u2014so it needs 360\u00ba\/5.625\u00ba = 64 steps in half-step mode. In full-step mode: 64\/2 = 32 steps to complete one rotation.<\/p>\n\n\n\n<p>However, the output shaft is driven via a 64:1 gear ratio. This means that the shaft (visible outside the motor) will complete rotation if the motor inside rotates 64 times. This means that the motor will have to move 32&#215;64 = 2048 steps for the shaft to complete one full rotation. This means that you&#8217;ll have a precision of 360\u00ba\/2048 steps = 0.18\u00ba\/step.<\/p>\n\n\n\n<p>So, in summary:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Total steps per revolution = 2048 steps<\/li><li>Step angle = 0.18\u00ba\/step<\/li><\/ul>\n\n\n\n<p>If you&#8217;re using a different stepper motor, please consult the datasheet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">ULN2003 Motor Driver<\/h2>\n\n\n\n<p>To interface the stepper motor with the ESP8266 NodeMCU board, we&#8217;ll use the ULN2003 motor driver, as shown in the figure below. The 28BYJ-48 stepper motor is many times sold together with the ULN2003 motor driver.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"422\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01-module-board.jpg?resize=750%2C422&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ULN2003 Motor Driver 01 module board\" class=\"wp-image-105429\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01-module-board.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01-module-board.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>The module comes with a connector that makes it easy and simple to connect the motor to the module. It has four input pins to control the coils that make the stepper motor move. The four LEDs provide a visual interface of the coils&#8217; state. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"429\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01.png?resize=750%2C429&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ULN2003 Motor Driver Labels\" class=\"wp-image-105359\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ULN2003-Motor-Driver-01.png?resize=300%2C172&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n\n<p>There are pins to connect <span class=\"rnthl rntcred\">VCC<\/span> and <span class=\"rnthl rntcblack\">GND,<\/span> and a jumper cap that acts as an ON\/OFF switch to power the stepper motor\u2014if you remove the jumper, there is no power reaching the motor. You can use those pins to wire a physical switch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ULN2003 Motor Driver Pinout<\/h3>\n\n\n\n<p>The following table shows the module pinout:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><span class=\"rnthl rntclgray\">IN1<\/span><\/td><td>Control the motor: connect to a microcontroller digital pin<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">IN2<\/span><\/td><td>Control the motor: connect to a microcontroller digital pin<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">IN3<\/span><\/td><td>Control the motor: connect to a microcontroller digital pin<\/td><\/tr><tr><td><span class=\"rnthl rntclgray\">IN4<\/span><\/td><td>Control the motor: connect to a microcontroller digital pin<\/td><\/tr><tr><td><span class=\"rnthl rntcred\">VCC<\/span><\/td><td>Powers the motor<\/td><\/tr><tr><td><span class=\"rnthl rntcblack\">GND<\/span><\/td><td>Common GND<\/td><\/tr><tr><td>Motor connector<\/td><td>Connect the motor connector<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wiring wp-block-heading\">Wire Stepper Motor to the ESP8266 Board<\/h2>\n\n\n\n<p>In this section, we&#8217;ll connect the stepper motor to the ESP8266 via the ULN2003 motor driver.<\/p>\n\n\n\n<p>We&#8217;ll connect IN1, IN2, IN3, and IN4 to GPIOs <span class=\"rnthl rntclblue\">5<\/span>, <span class=\"rnthl rntcyellow\">4<\/span>, <span class=\"rnthl rntclgreen\">14<\/span>, and <span class=\"rnthl rntclgray\">12<\/span>. You can use any other suitable digital pins (check our <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-pinout-reference-gpios\/\">ESP8266 pinout reference guide<\/a>).<\/p>\n\n\n\n<p>You can follow the next schematic diagram.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"646\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Schematic-Diagram-Wiring.png?resize=1200%2C646&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 with Stepper Motor 28BYJ-48 and ULN2003A Schematic Diagram Wiring\" class=\"wp-image-106096\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Schematic-Diagram-Wiring.png?w=1269&amp;quality=100&amp;strip=all&amp;ssl=1 1269w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Schematic-Diagram-Wiring.png?resize=300%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Schematic-Diagram-Wiring.png?resize=1024%2C551&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Schematic-Diagram-Wiring.png?resize=768%2C413&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Note: <\/strong> You should power the motor driver using an external 5V power supply.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Motor Driver<\/strong><\/td><td><strong>ESP8266<\/strong><\/td><\/tr><tr><td>IN1<\/td><td><span class=\"rnthl rntcblue\">GPIO 5<\/span><\/td><\/tr><tr><td>IN2<\/td><td><span class=\"rnthl rntcyellow\">GPIO 4<\/span><\/td><\/tr><tr><td>IN3<\/td><td><span class=\"rnthl rntcgreen\">GPIO 14<\/span><\/td><\/tr><tr><td>IN4<\/td><td><span class=\"rnthl rntcgray\">GPIO 12<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Installing the AccelStepper Library<\/h2>\n\n\n\n<p>There are different ways to control stepper motors with a microcontroller. To control the stepper motor with the ESP8266, we&#8217;ll use the <a href=\"https:\/\/www.airspayce.com\/mikem\/arduino\/AccelStepper\/\" target=\"_blank\" rel=\"noreferrer noopener\">AccelStepper <\/a>library. This library allows you to easily move the motor a defined number of steps, set its speed, acceleration, and much more. <\/p>\n\n\n\n<p>The library has great documentation explaining how to use its methods. You can check it <a href=\"https:\/\/www.airspayce.com\/mikem\/arduino\/AccelStepper\/classAccelStepper.html\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p>Follow the next steps to install the library in your Arduino IDE.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Go to <strong>Sketch<\/strong> > <strong>Include Library<\/strong> > <strong>Manage Libraries&#8230;<\/strong><\/li><li>Search for &#8220;accelstepper&#8221;.<\/li><li>Install the AccelStepper library by Mike McCauley. We&#8217;re using version 1.61.0.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"786\" height=\"443\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/install-accelstepper-library-Arduino-ide.png?resize=786%2C443&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install AccelStepper Library Arduino IDE\" class=\"wp-image-106090\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/install-accelstepper-library-Arduino-ide.png?w=786&amp;quality=100&amp;strip=all&amp;ssl=1 786w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/install-accelstepper-library-Arduino-ide.png?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/install-accelstepper-library-Arduino-ide.png?resize=768%2C433&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Control Stepper Motor with the ESP8266 &#8211; Code<\/h2>\n\n\n\n<p>Copy the following code to your Arduino IDE. In this example, the motor will make one clockwise rotation and a counterclockwise rotation over and over again.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-stepper-motor-28byj-48-uln2003\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n  \n  Based on Stepper Motor Control - one revolution by Tom Igoe\n*\/\n\n#include &lt;AccelStepper.h&gt;\n\nconst int stepsPerRevolution = 2048;  \/\/ change this to fit the number of steps per revolution\n\n\/\/ ULN2003 Motor Driver Pins\n#define IN1 5\n#define IN2 4\n#define IN3 14\n#define IN4 12\n\n\/\/ initialize the stepper library\nAccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);\n\nvoid setup() {\n  \/\/ initialize the serial port\n  Serial.begin(115200);\n  \n  \/\/ set the speed and acceleration\n  stepper.setMaxSpeed(500);\n  stepper.setAcceleration(100);\n  \/\/ set target position\n  stepper.moveTo(stepsPerRevolution);\n}\n\nvoid loop() {\n  \/\/ check current stepper motor position to invert direction\n  if (stepper.distanceToGo() == 0){\n    stepper.moveTo(-stepper.currentPosition());\n    Serial.println(&quot;Changing direction&quot;);\n  }\n  \/\/ move the stepper motor (one step at a time)\n  stepper.run();\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Stepper_Motor_Basic.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>We adapted this sketch from the examples provided by the library (<strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>AccelStepper <\/strong>&gt;<strong> Bounce<\/strong>).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>First, include the <span class=\"rnthl rntliteral\">AccelStepper.h<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;AccelStepper.h&gt;<\/code><\/pre>\n\n\n\n<p>Define the steps per revolution of your stepper motor\u2014in our case, it&#8217;s 2048:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>const int stepsPerRevolution = 2048;  \/\/ change this to fit the number of steps per revolution<\/code><\/pre>\n\n\n\n<p>Define the motor input pins. In this example, we&#8217;re connecting to GPIOs 5, 4, 14, and 12, but you can use any other suitable GPIOs.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define IN1 5\n#define IN2 4\n#define IN3 14\n#define IN4 12<\/code><\/pre>\n\n\n\n<p>Initialize an instance of the AccelStepper library called <span class=\"rnthl rntliteral\">stepper<\/span>. Pass as arguments: <span class=\"rnthl rntliteral\">AccelStepper::HALF4WIRE<\/span> to indicate we&#8217;re controlling the stepper motor with four wires, and the input pins. In the case of the 28BYJ-48 stepper motor, the order of the pins is <span class=\"rnthl rntliteral\">IN1<\/span>, <span class=\"rnthl rntliteral\">IN3<\/span>, <span class=\"rnthl rntliteral\">IN2<\/span>, <span class=\"rnthl rntliteral\">IN4<\/span>\u2014it might be different for your motor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AccelStepper stepper(AccelStepper::HALF4WIRE, IN1, IN3, IN2, IN4);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Serial.begin(115200);<\/code><\/pre>\n\n\n\n<p>Set the stepper motor maximum speed using the <span class=\"rnthl rntliteral\">setMaxSpeed()<\/span> method. Pass as argument the speed in steps per second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stepper.setMaxSpeed(500);<\/code><\/pre>\n\n\n\n<p>As mentioned in the documentation, the <span class=\"rnthl rntliteral\">setMaxSpeed()<\/span> method sets the maximum permitted speed. The <span class=\"rnthl rntliteral\">run()<\/span> function will accelerate up to the speed set by this function.<\/p>\n\n\n\n<p>Set the acceleration using the <span class=\"rnthl rntliteral\">setAcceleration()<\/span> method. Pass as argument the acceleration in steps per second per second.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stepper.setAcceleration(100);<\/code><\/pre>\n\n\n\n<p>Then, use the <span class=\"rnthl rntliteral\">moveTo()<\/span> method to set a target position. Then, the <span class=\"rnthl rntliteral\">run()<\/span> function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. We&#8217;re setting the target position to 2048 (which is a complete turn in case of this motor).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stepper.moveTo(stepsPerRevolution);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we&#8217;ll rotate the stepper motor clockwise and counterclockwise. <\/p>\n\n\n\n<p>First, we check if the motor has already achieved its target position. To do that, we can use the <span class=\"rnthl rntliteral\">distanceToGo()<\/span> function that returns the steps from the current position to the target position. <\/p>\n\n\n\n<p>When the motor reaches its target position, it means that the <span class=\"rnthl rntliteral\">distanceToGo()<\/span> function will return <span class=\"rnthl rntliteral\">0<\/span> and the following if statement will be true.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (stepper.distanceToGo() == 0){<\/code><\/pre>\n\n\n\n<p>When the motor reaches its target position, we set a new target position\u2014which is the same as the current position but in the opposite direction.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stepper.moveTo(-stepper.currentPosition());<\/code><\/pre>\n\n\n\n<p>Finally, call <span class=\"rnthl rntliteral\">stepper.run()<\/span> to move the motor one step at a time in the <span class=\"rnthl rntliteral\">loop()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stepper.run();<\/code><\/pre>\n\n\n\n<p>Here&#8217;s an explanation about the <span class=\"rnthl rntliteral\">run()<\/span> method provided by the library documentation: &#8220;The <span class=\"rnthl rntliteral\">run()<\/span> function polls the motor and step it if a step is due, implementing accelerations and decelerations to achieve the target position. You must call this as frequently as possible, but at least once per minimum step time interval, preferably in your main loop. Note that each call to <span class=\"rnthl rntliteral\">run()<\/span> will make at most one step, and then only when a step is due, based on the current speed and the time since the last step&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Demonstration<\/h2>\n\n\n\n<p>Upload the code to your board. After uploading, the motor will make one clockwise rotation and a counterclockwise rotation over and over again. It starts the rotation at a lower speed until reaching the desired speed before reaching the target position.<\/p>\n\n\n\n<p>You can watch a quick video demonstration:<\/p>\n\n\n<div style=\"text-align:center\"><iframe src=\"https:\/\/player.vimeo.com\/video\/592192911?color=ff9933&title=1&byline=0&portrait=0\" width=\"720\" height=\"405\" frameborder=\"0\" webkitallowfullscreen mozallowfullscreen allowfullscreen><\/iframe><\/div><\/br>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>This tutorial was a getting started guide for stepper motors with the ESP8266. Stepper motors move one step at a time and allow you to position the motor shaft at a specific angle.<\/p>\n\n\n\n<p>In a future tutorial, we&#8217;ll show you how to create a web server to control the stepper motor remotely with the ESP8266. So, stay tuned.<\/p>\n\n\n\n<p>We hope you find this tutorial useful. <\/p>\n\n\n\n<p>Learn more about the ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation Using ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 eBook (2nd Edition)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 Projects and Tutorials\u2026<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to control a stepper motor with the ESP8266 NodeMCU board. We&#8217;ll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. We&#8217;ll program &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-stepper-motor-28byj-48-uln2003\/#more-106084\" aria-label=\"Read more about ESP8266 NodeMCU with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":106093,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[214,265,246,300,240,264],"tags":[],"class_list":["post-106084","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/08\/ESP8266-Stepper-Motor-Tutorial-Arduino-IDE.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/106084","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=106084"}],"version-history":[{"count":11,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/106084\/revisions"}],"predecessor-version":[{"id":106354,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/106084\/revisions\/106354"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/106093"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=106084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=106084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=106084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}