{"id":105321,"date":"2021-08-25T15:27:51","date_gmt":"2021-08-25T15:27:51","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=105321"},"modified":"2024-01-15T13:05:53","modified_gmt":"2024-01-15T13:05:53","slug":"esp32-stepper-motor-28byj-48-uln2003","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-stepper-motor-28byj-48-uln2003\/","title":{"rendered":"ESP32 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 ESP32. We&#8217;ll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. The ESP32 board will be programmed using Arduino IDE.<\/p>\n\n\n<div class=\"wp-block-image\">\n<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\/07\/ESP32-Stepper-Motor-Tutorial.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Stepper Motor 28BYJ-48 and ULN2003 Motor Driver\" class=\"wp-image-105399\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Tutorial.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Tutorial.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Tutorial.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Tutorial.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<p>We have a similar tutorial for the ESP8266 board: <a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-stepper-motor-28byj-48-uln2003\/\">ESP8266 NodeMCU with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)<\/a><\/p>\n\n\n\n<p>We have tutorials for other motors with the ESP32:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-dc-motor-l298n-motor-driver-control-speed-direction\/\">ESP32 with <strong>DC Motor<\/strong> and L298N Motor Driver \u2013 Control Speed and Direction<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-servo-motor-web-server-arduino-ide\/\">ESP32 <strong>Servo Motor<\/strong> Web Server with Arduino IDE<\/a><\/li>\n<\/ul>\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\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/28byj-48-stepper-motor-uln2003\/\" target=\"_blank\" rel=\"noreferrer noopener\">28BYJ-48 Stepper Motor<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/28byj-48-stepper-motor-uln2003\/\" target=\"_blank\" rel=\"noreferrer noopener\">ULN2003 Motor Driver<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32 <\/a>(read <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">Best ESP32 Development Boards<\/a>)<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper Wires<\/a><\/li>\n\n\n\n<li>5V Power Supply<\/li>\n<\/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<div class=\"wp-block-image\">\n<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<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<div class=\"wp-block-image\">\n<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-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<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\">\n<li>Rated voltage: 5V DC<\/li>\n\n\n\n<li>Number of phases: 4<\/li>\n\n\n\n<li>Speed variation ratio: 1\/64<\/li>\n\n\n\n<li>Stride angle: 5.625\u00ba\/64<\/li>\n\n\n\n<li>Frequency: 100Hz<\/li>\n<\/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<div class=\"wp-block-image\">\n<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<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 a 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\">\n<li>Total steps per revolution = 2048 steps<\/li>\n\n\n\n<li>Step angle = 0.18\u00ba\/step<\/li>\n<\/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 ESP32, 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<div class=\"wp-block-image\">\n<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<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<div class=\"wp-block-image\">\n<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 IC 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<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=\"wp-block-heading wiring\">Wire Stepper Motor to the ESP32<\/h2>\n\n\n\n<p>In this section, we&#8217;ll connect the stepper motor to the ESP32 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\">19<\/span>, <span class=\"rnthl rntcyellow\">18<\/span>, <span class=\"rnthl rntclgreen\">5<\/span>, and <span class=\"rnthl rntclgray\">17<\/span>. You can use any other suitable digital pins (check our <a href=\"https:\/\/randomnerdtutorials.com\/esp32-pinout-reference-gpios\/\">ESP32 pinout reference guide<\/a>).<\/p>\n\n\n\n<p>You can follow the next schematic diagram.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"631\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Schematic-Diagram-Wiring_bb.png?resize=1200%2C631&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with Stepper Motor 28BYJ-48 and ULN2003A Schematic Diagram Wiring\" class=\"wp-image-105354\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Schematic-Diagram-Wiring_bb.png?w=1296&amp;quality=100&amp;strip=all&amp;ssl=1 1296w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Schematic-Diagram-Wiring_bb.png?resize=300%2C158&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Schematic-Diagram-Wiring_bb.png?resize=1024%2C538&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Schematic-Diagram-Wiring_bb.png?resize=768%2C404&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\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>ESP32<\/strong><\/td><\/tr><tr><td>IN1<\/td><td><span class=\"rnthl rntcblue\">GPIO 19<\/span><\/td><\/tr><tr><td>IN2<\/td><td><span class=\"rnthl rntcyellow\">GPIO 18<\/span><\/td><\/tr><tr><td>IN3<\/td><td><span class=\"rnthl rntcgreen\">GPIO 5<\/span><\/td><\/tr><tr><td>IN4<\/td><td><span class=\"rnthl rntcgray\">GPIO 17<\/span><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Control Stepper Motor with the ESP32 &#8211; Code<\/h2>\n\n\n\n<p>There are different ways to control stepper motors with a microcontroller. We&#8217;ll use the Arduino built-in <span class=\"rnthl rntliteral\">Stepper.h<\/span> library. This library provides an easy way to move the motor by a defined number of steps.<\/p>\n\n\n\n<p>Copy the following code to your Arduino IDE.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp32-stepper-motor-28byj-48-uln2003\/\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n  Based on Stepper Motor Control - one revolution by Tom Igoe\n*\/\n#include &lt;Stepper.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 19\n#define IN2 18\n#define IN3 5\n#define IN4 17\n\n\/\/ initialize the stepper library\nStepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);\n\nvoid setup() {\n  \/\/ set the speed at 5 rpm\n  myStepper.setSpeed(5);\n  \/\/ initialize the serial port\n  Serial.begin(115200);\n}\n\nvoid loop() {\n  \/\/ step one revolution in one direction:\n  Serial.println(&quot;clockwise&quot;);\n  myStepper.step(stepsPerRevolution);\n  delay(1000);\n\n  \/\/ step one revolution in the other direction:\n  Serial.println(&quot;counterclockwise&quot;);\n  myStepper.step(-stepsPerRevolution);\n  delay(1000);\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\/ESP32\/ESP32_Stepper_Motor_Basic.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>We adapted this code from the examples provided by the Stepper library (<strong>File <\/strong>&gt; <strong>Examples <\/strong>&gt; <strong>Stepper <\/strong>&gt;<strong> stepper_oneRevolution<\/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\">Stepper.h<\/span> library.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Stepper.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 19, 18, 5, and 17, but you can use any other suitable GPIOs.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define IN1 19\n#define IN2 18\n#define IN3 5\n#define IN4 17<\/code><\/pre>\n\n\n\n<p>Initialize an instance of the stepper library called <span class=\"rnthl rntliteral\">myStepper<\/span>. Pass as arguments the steps per revolution 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>Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);<\/code><\/pre>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, set the stepper speed using the <span class=\"rnthl rntliteral\">setSpeed<\/span> method. The stepper speed is in rpm.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>myStepper.setSpeed(5);<\/code><\/pre>\n\n\n\n<p>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>In the <span class=\"rnthl rntliteral\">loop()<\/span>, we&#8217;ll rotate the stepper motor clockwise and counterclockwise. You can use the <span class=\"rnthl rntliteral\">step()<\/span> method on the <span class=\"rnthl rntliteral\">myStepper<\/span> object. Pass as an argument the number of steps you want to take. For a full rotation (revolution), you need 2048 steps (<span class=\"rnthl rntliteral\">stepsPerRevolution<\/span> variable).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>myStepper.step(stepsPerRevolution);<\/code><\/pre>\n\n\n\n<p>To rotate the motor counterclockwise, you need to pass the number of steps with the minus &#8220;<span class=\"rnthl rntliteral\">&#8211;<\/span>&#8221; sign.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>myStepper.step(-stepsPerRevolution);<\/code><\/pre>\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.<\/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\/592192741?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\">Other Libraries<\/h2>\n\n\n\n<p>Using the <span class=\"rnthl rntliteral\">Stepper.h<\/span> library is one of the easiest ways to control a stepper motor. However, if you want more control over your stepper motor, there are libraries with more functions like the <a href=\"https:\/\/www.airspayce.com\/mikem\/arduino\/AccelStepper\/classAccelStepper.html\" target=\"_blank\" rel=\"noreferrer noopener\">AccelStepper library<\/a>. This library is well documented, with all the methods described in great detail. <\/p>\n\n\n\n<p>It provides <a href=\"https:\/\/github.com\/waspinator\/AccelStepper\/tree\/master\/examples\" target=\"_blank\" rel=\"noreferrer noopener\">several examples<\/a> that are compatible with the ESP32. Just make sure you initialize a stepper object with the right pins:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>AccelStepper stepper (AccelStepper::FULL4WIRE, 19, 5, 18, 17);<\/code><\/pre>\n\n\n\n<p>This library allows you to control the motors in a non-blocking way and allows you to control more than one motor at a time. But this is a subject for another tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Controlling a Stepper Motor Remotely<\/h2>\n\n\n\n<p>Learn how to control a stepper motor remotely with the ESP32 using a web server. Take a look at the following tutorials:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/stepper-motor-esp32-web-server\/\" title=\"\">ESP32 Web Server: Control Stepper Motor (HTML Form)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/stepper-motor-esp32-websocket\/\" title=\"\">ESP32 Web Server: Control Stepper Motor (WebSocket)<\/a><\/li>\n<\/ul>\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 ESP32. 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>One of the easiest ways to control the stepper motor is to use the built-in Arduino <span class=\"rnthl rntliteral\">Stepper<\/span> library. This library provides an easy way to rotate the motor clockwise or counterclockwise a determined number of steps. If you want more control over your stepper motor, we recommend the <span class=\"rnthl rntliteral\">AccelStepper<\/span> library.<\/p>\n\n\n\n<p>We hope you find this tutorial useful. <\/p>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE (eBook + video course)<\/a><\/strong><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 eBook (2nd Edition)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">More ESP32 Projects and Tutorials \u2026<\/a><\/li>\n<\/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 ESP32. We&#8217;ll use the 28BYJ-48 unipolar stepper motor with the ULN2003 motor driver. The ESP32 board will &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-stepper-motor-28byj-48-uln2003\/#more-105321\" aria-label=\"Read more about ESP32 with Stepper Motor (28BYJ-48 and ULN2003 Motor Driver)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":105399,"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":[281,276,277,299,264],"tags":[],"class_list":["post-105321","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/07\/ESP32-Stepper-Motor-Tutorial.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\/105321","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=105321"}],"version-history":[{"count":29,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/105321\/revisions"}],"predecessor-version":[{"id":145818,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/105321\/revisions\/145818"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/105399"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=105321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=105321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=105321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}