DBWS - Stepper Motors With Arduino - Getting Started With Stepper Motors
DBWS - Stepper Motors With Arduino - Getting Started With Stepper Motors
Motors
https://dronebotworkshop.com
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Table of Contents
Introduction 2
Stepper Motors 2
How Stepper Motors Work 4
Microstepping 6
Types of Stepper Motors 7
Bipolar Stepper Motors 8
Unipolar Stepper Motors 9
Reading Stepper Motor Specifications 13
NEMA Motor sizes 15
Experimenting with Stepper Motors 16
Demo 1 – 28BYJ-48 Unipolar Stepper with ULN2003 16
Demo 2 – Two 28BYJ-48 Unipolar Steppers with ULN2003 22
Demo 3 – Bipolar Stepper with L298N H-Bridge 27
Demo 4 – Bipolar Stepper with A4988 32
A4988 Current Adjustment 35
Conclusion 39
https://dronebotworkshop.com 1
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Learn how to control bipolar and unipolar stepper motors with an Arduino using drivers
like ULN2003, L298N and A4988. In this article I’ll show you all you need to know to get
started with stepper motors.
Introduction
Stepper Motors are used in a wide variety of devices ranging from 3D printers and CNC
machines to DVD drives, heating ducts and even analog clocks. Yet despite their
popularity many experimenters shy away from using stepper motors as they seem to
require complex hookups and code.
In this article I hope to dispel that myth by showing you just how easy it is to use a
stepper motor with an Arduino. So follow along, I promise to take you through all of this
“complex” stepper theory one step at a time!
Stepper Motors
Stepper motors are DC motors that rotate in precise increments or “steps”. They are
very useful when you need to position something very accurately. They are used in 3D
printers to position the printhead correctly and in CNC machines where their precision is
used to position the cutting head. If your digital camera has an autofocus or remote
zoom feature chances are a stepper motor is being employed to do that.
https://dronebotworkshop.com 2
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Some users confuse stepper motors with servo motors but they are actually two
different beasts. A servo motor is unique in that it’s motor shaft can be moved to a
precise angle, most servos only rotate 180 or 270 degrees although there are modified
servos that can spin a full 360 degrees. A servo motor is “aware” of its position and can
be moved to a specific angle even if an external force moves the motor shaft.
Steppers, on the other hand, are “unaware” of their position. They can be moved to an
exact position in reference to where they start stepping (i.e 36 degrees clockwise) but
unlike servos they can be misaligned if their shaft is moved by an external force. In
many applications a servo is first moved to a “homing” or reference position before
being controlled, printers commonly do this when they are first initialized.
https://dronebotworkshop.com 3
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Because the move in discrete steps a stepper motor is not often used where a smooth
continuous rotation is required, However with the use of gearing and microstepping they
can approach a smooth rotation and their ability to be very accurately positioned often
outweighs the roughness of their movement.
Another advantage stepper motors have over DC motors is the ability to move art very
slow speeds without stalling, in fact stalling really isn’t a concept with stepper motors.
They also pack a lot of torque into a comparably small package.
By precisely controlling the current in the coils the motor shaft can be made to move in
discrete steps, as illustrated in the following diagrams:
https://dronebotworkshop.com 4
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
In the first diagram the coil at the top is energized by applying electricity in the polarity
shown. The magnetized shaft is attracted to this coil and then locks into place.
Now look what happens when the electricity is removed from the top coil and applied to
the other coil. The shaft is attracted to the second coil and locks into place there.
https://dronebotworkshop.com 5
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
The jump between the two positions is one step (in this illustration a step is 90 degrees,
in actual fact a stepper motor usually steps just a fraction of this. The diagrams are
simplified for clarity).
Microstepping
We have seen how the motor shaft moves to lock itself into place in front of an attracting
electromagnet, each magnet represents one step. It is, however, possible to move the
motor shaft into positions between steps. This is known as “microstepping”.
https://dronebotworkshop.com 6
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
In this illustration the current has been applied to BOTH coils in an equal amount. This
causes the motor shaft to lock into place halfway between the two coils. This would be
known as a “half step”.
The principle can be extended to include quarter steps, eight steps and even sixteenth
steps. This is done by controlling the ratio of the current applied to both coils to attract
the motor shaft to a position between the coils but closer to one coil than the other.
https://dronebotworkshop.com 7
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
For most experimenters these differences will be merely academic but if you are
choosing a stepper motor for a very specific design you may want to look into this more.
For most users the main difference between stepper motor design boils down to the
way the coils are wired within the motor. There are two methods employed – Bipolar
and Unipolar. These two types of stepper motors are not interchangeable (although it is
possible to “hack” a Unipolar motor to create a Bipolar motor).
Bipolar stepper motors consist of two coils of wire (electrically, actually split into several
physical coils) and generally have four connections, two per coil. The simplified
diagrams of stepper operation that you just looked at in the previous section are all
bipolar stepper motors.
https://dronebotworkshop.com 8
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
An advantage of bipolar stepper motors is that they make use of the entire coil winding
so they are more efficient. However they require a more complex controller or driver to
operate as to reverse direction the polarity of the voltage applied to the coils needs to
be reversed.
A unipolar stepper motor also consists of two coils (electrically) but each coil has a
center tap so there are three connections on each coil. This results in six connections,
however many unipolar stepper motors have only five connections as the two center
taps are internally connected.
In a unipolar stepper motor only half of each coil is used at one time. In most
configurations a positive voltage is applied to the center tap and left there. A negative
https://dronebotworkshop.com 9
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
voltage is then applied to one side of the coil to attract the motor shaft, as illustrated
below:
As with the bipolar motor the unipolar stepper motor can be made to advance one step
when current is removed from the top coil and applied to one side of the second coil:
https://dronebotworkshop.com 10
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
You can also microstep a unipolar stepper motor by using the same technique that we
used with bipolar steppers, applying current to both coils.
Now to reverse the direction of a unipolar motor you don’t need to reverse polarity.
Instead the negative voltage is applied to the OTHER side of the coil. This causes the
current to flow in the opposite direction within the coil and this in turn moves the motor
shaft in the opposite direction.
https://dronebotworkshop.com 11
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
We will work with both unipolar and bipolar stepper motors in the experiments we are
about to do.
It should be noted that there are also stepper motors that can be wired as both bipolar
and unipolar. These motors have four coils which can be joined to make either two
center tapped coils (for a unipolar configuration) or just two big coils (in a bipolar
configuration). These stepper motors will have eight wires, two per coil.
https://dronebotworkshop.com 12
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Here are a few of the key specifications you’ll find included with stepper motors, along
with a short definition of them:
Phase: This refers to the groupings of the individual coils in the stepper motor. A
stepper motor may have several coils but they are wired together and controlled in
phases. Two, Four and Five phase stepper motors are common. There will often be a
phase diagram included with a stepper motor that indicates the sequence that the motor
phases are driven in.
Step Angle: This is the amount that the shaft of the motor will spin for each individual
full step, measured in degrees, In some stepper motors this is referred to as Steps Per
Revolution and the two figures are just different ways of expressing the same thing.
As an example a common rating for a stepper motor is a 1.8 degree step angle. As
there are 360 degrees in a full rotation this is equivalent to 200 steps per revolution (1.8
x 200 = 360).
Voltage: Simply the voltage rating of the motor coils. It is also a function of the current
rating and the coil resistance and you can use Ohm’s Law to calculate one from the
other.
Current: The maximum current at the rated voltage. This is a useful specification as it
will allow you to select a suitable driver and power supply for your stepper motor.
https://dronebotworkshop.com 13
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Holding Torque: This will be the amount of force that is created when the stepper
motor is energized.
Detent Torque: This is the amount of holding torque that can be expected when the
motor is NOT energized.
Shaft Style: The physical shape of the motor shaft. You will need to know this in order
to mate your stepper motor with gears, pulleys and other external connections such as
shaft couplers. There are several common shapes used, in addition the shaft length can
be important for obvious reasons.
Another obvious specification of a stepper (or any motor) is its physical size. There are
a group of stepper motors that have standard sizes, we will look at these now.
https://dronebotworkshop.com 14
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
In 1984 the NEMA committee set out some standards for motor sizes, based upon the
face plate size of the motor. This standard is still in use today and results in motors
designated “NEMA 17” or “NEMA 23”.
The NEMA 17 sized stepper motor has become extremely popular, especially in the
construction of 3D printers. It also creates a lot of confusion as you often hear people
refer to a motor simply as a “NEMA 17”, which really only designates the size of the
motor and not it’s other specifications such as voltage, current, step angle or even if it is
bipolar or unipolar.
The “17” in “NEMA 17” is the face plate size, in the NEMA standard the face plate is the
NEMA “number” divided by 10 in inches. So a NEMA 17 motor has a face plate
approximately 1.7 inches wide while a NEMA 23 is 2.3 inches wide.
https://dronebotworkshop.com 15
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
There are four experiments we will do today, two of them using a unipolar stepper motor
and two of them with the unipolar variety. In addition we will make use of a couple of
Arduino libraries, one of which is already included in the Arduino IDE.
Although these experiments have been illustrated using an Arduino Uno any Arduino
will work. You can also feel free to change the pin numbers if you need to as there are
no special requirements there, just be sure to alter the sketch to reflect those changes if
you decide to do that.
The 28BYJ-48 is a 5-wire unipolar stepper motor that moves 32 steps per rotation
internally but has a gearing system that moves the shaft by a factor of 64. The result is
a motor that spins at 2048 steps per rotation. It should be noted that some of these
https://dronebotworkshop.com 16
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
motors may have a different gearing system so the number of steps per rotation of your
motor may not be the same. The 28BYJ-48 runs on 5 volts.
The motor is commonly packaged with a tiny driver board based around the ULN2003
darlington transistor array. The board has a connector that mates perfectly with the
motor wires so it is very easy to use. There are also connections for four 5-volt digital
inputs as well as power supply connections.
On the subject of power supplies one very important thing to note is that you should
NEVER use the 5-volt power from your Arduino to power this (or any) stepper motor no
matter how tempting it is. Even though the 28BYJ-48 doesn’t draw much current it will
induce electrical “noise” onto its power supply lines and this could damage your
Arduino. Always use a seperate power supply to power your stepper motors!
https://dronebotworkshop.com 17
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Now that we have everything hooked up we will need to program the Arduino. Here is
the sketch that we will use to do that:
https://dronebotworkshop.com 18
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
1 /*
3 Stepper-Demo1.ino
8 https://dronebotworkshop.com
9 */
10
12 #include <Stepper.h>
13
14 // Define Constants
15
18
21
24
25 // Define Variables
26
28 int StepsRequired;
29
https://dronebotworkshop.com 19
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
35
37
38 void setup()
39 {
41 }
42
43 void loop()
44 {
46 steppermotor.setSpeed(1);
47 StepsRequired = 4;
48 steppermotor.step(StepsRequired);
49 delay(2000);
50
52 StepsRequired = STEPS_PER_OUT_REV / 2;
53 steppermotor.setSpeed(100);
54 steppermotor.step(StepsRequired);
55 delay(1000);
56
58 StepsRequired = - STEPS_PER_OUT_REV / 2;
https://dronebotworkshop.com 20
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
59 steppermotor.setSpeed(700);
60 steppermotor.step(StepsRequired);
61 delay(2000);
62
63 }
In this sketch we make use of the Arduino Stepper Library which comes packaged with
your Arduino IDE. The stepper library takes care of sequencing the pulses we will be
sending to our stepper motor and it can be used with a wide variety of motors, both
unipolar and bipolar.
The 28BYJ-48 stepper motors have internal gearing which reduces the output rotation
by a factor of 64 (as noted above some are different). So we define three constants to
handle motor rotation:
● STEPS_PER_REV is the number of steps the actual motor takes per revolution.
This is set at 32.
● GEAR_RED is the amount of gear reduction. I set mine to 64 but you may need
to adjust this if your motor is different
● STEPS_PER_OUT_REV is the final output of the motor shaft after gear
reduction. It is the multiple of the above two numbers.
In case you are wondering why a float was used instead of an integer for the above
constants it’s because the gear reduction is sometimes a number like 63.5. If yours is
64 you could always use integers.
The variable “StepsRequired” will be used to define the number of steps we want our
motor to rotate.
https://dronebotworkshop.com 21
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
The 28BYJ-48 Unipolar stepper motor has a step sequence as follows: 1-3-2-4 . This
information will be used to drive the motor by creating an instance of the Stepper class
called “steppermotor” with the pin sequence of 8,10, 9, 11 . Make sure you get this right
or the motor will not operate properly.
There is nothing to set in the setup routine as the Arduino Stepper library class already
sets the four I/O pins as outputs.
In the loop we have three demonstration runs, you can feel free to add as many more
as you wish or to change the existing ones.
The first run steps the motor four steps very slowly. It is interesting to observe the LEDs
on the UNL2003 as this runs.
The second run turns the motor clockwise a half turn very slowly. And the final run
returns the motor a half turn at a much faster speed. You can experiment with the
“setSpeed” parameter to determine what the top speed for your motor is.
Before we get to the code let’s hook up an additional motor and driver to our Arduino.
Leave the connections you made in Demo 1 as they are and wire the new devices as
follows:
https://dronebotworkshop.com 22
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Once again we will power the driver and motor from an external 5-volt power supply.
This sketch will make use of the AccelStepper library which is an advanced library
written by Mike McCauley. You will need to install this library using the Arduino IDE
Library Manager as it is not included in the Arduino IDE.
https://dronebotworkshop.com 23
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
1 /*
3 Stepper-Demo2.ino
8 https://dronebotworkshop.com
9 */
10
12 #include <AccelStepper.h>
13
14 // Define Constants
15
17 #define FULLSTEP 4
18 #define HALFSTEP 8
19
21
26
27
https://dronebotworkshop.com 24
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
32
37
38 void setup()
39 {
40 // 1 revolution Motor 1 CW
41 stepper1.setMaxSpeed(1000.0);
42 stepper1.setAcceleration(50.0);
43 stepper1.setSpeed(200);
44 stepper1.moveTo(2048);
45
47 stepper2.setMaxSpeed(1000.0);
48 stepper2.setAcceleration(50.0);
49 stepper2.setSpeed(200);
50 stepper2.moveTo(-2048);
51
52 }
53
54
55 void loop()
56 {
58 if (stepper1.distanceToGo() == 0)
https://dronebotworkshop.com 25
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
59 stepper1.moveTo(-stepper1.currentPosition());
60 if (stepper2.distanceToGo() == 0)
61 stepper2.moveTo(-stepper2.currentPosition());
62
63 stepper1.run();
64 stepper2.run();
65
66 }
In this demonstration we will drive one motor at full steps and the second one at half
steps. We will define two constants at the beginning of the sketch for this. We’ll also
define eight constants, one for each motor output pin on the Arduino.
Next we setup two motor objects, one for each motor. We use our pin definitions and
the step definitions to set these up.
In the setup routine we setup the maximum speed, acceleration factor, initial speed and
the number of steps we will move to (I used 2048 as the motor spins 2048 steps per
rotation). Note that the second motor has a negative number of steps, this indicates it is
to move counterclockwise when it is initialized.
In the loop we use an If statement to check how far the motors need to travel until they
get to the “moveTo” position using the AccelStepper library “distanceToGo” parameter.
Once that reaches zero we change the “moveTo” position to the negative of the current
position, which will result in the motor moving in the opposite direction to the other end
of travel.
The motors are actually set into motion using the AccelStepper library “run” function.
https://dronebotworkshop.com 26
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
The movement of the two motors is interesting to watch as they accelerate and
decelerate in opposite directions. Note that one motor is running at full steps while the
other uses half steps, observe the lights on the UNL2003 controller when the motors
start and stop and you’ll notice a difference in the step patterns.
As you recall a bipolar stepper motor requires a driver that can reverse polarity to the
motor coils in order to reverse the motor direction. A good component to accomplish
this with is an “H-Bridge”.
We have discussed using an H-Bridge before when we talked about controlling brushed
DC Motors. Essentially this is a device that contains four internal power transistors that
allow control of the direction of current through a motor coil.
We will be using the same H-Bridge controller that we used in the previous article, the
L298N module. These modules are very inexpensive and are very reliable and they can
be used to control either two DC motors or one stepper motor. Obviously we will be
using it to control a stepper motor today, the L298N is a dual H-Bridge so each H-Bridge
will drive one of the coils in our bipolar stepper motor.
Here is how I have hooked up my L298N H-Bridge, bipolar stepper and Arduino Uno:
https://dronebotworkshop.com 27
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Note that you may not need to make all of these connections, this depends upon how
you configure your L298N module. And also note that the motor power supply you use
should match your motor requirements.
The L298N module has a jumper to set its internal 5-volt logic circuits to use either an
external power supply (jumper off) or to use a built-in voltage regulator and derive the
5-volts from the motor power supply (jumper on). If you choose to use your motor
supply make sure it is at least 7.5 volts and eliminate the power connection from the
Arduino 5 volt output.
Some L298N modules also have a set of jumpers that allow you to tie the two Enable
lines high so the the motors are always enabled, which is what we want here. If you
have these you can also eliminate the connection from the Arduino 5-volt output to ENA
and ENB and just set the jumpers instead.
https://dronebotworkshop.com 28
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
We will also use a potentiometer to act as a speed control. Any value from 10k up will
work, lower values will put a lot of load onto the Arduino 5-volt output.
Once you get everything hooked up it’s time to load the code up to the Arduino. Here is
the sketch:
1 /*
3 Stepper-Demo3.ino
9 https://dronebotworkshop.com
10 */
11
13 #include <Stepper.h>
14
15 // Define Constants
16
20
https://dronebotworkshop.com 29
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
26
28
29 void setup() {
31 }
32
33 void loop() {
39 if (motorSpeed > 0) {
40 stepper_NEMA17.setSpeed(motorSpeed);
42 stepper_NEMA17.step(STEPS_PER_REV / 100);
43 }
44 }
The sketch uses the Arduino Stepper library again. If you feel like a challenge you can
rewrite it to use the AccelStepper library instead.
● STEPS_PER_REV is the number of steps per revolution that our motor is rated
at. Mine was rated at 200, which is the same as 1.8 degrees per step. Change
this to match your motor.
https://dronebotworkshop.com 30
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
The sequence for our bipolar stepper is 1-2-3-4 so we create our instance of the stepper
class with this in mind. Our L298N is connected to pins 8, 9, 10 and 11.
Again the stepper library sets up the pins as outputs so there is no need to do that in the
setup routine.
In the loop we read the potentiometer position by measuring the input voltage on the
analog pin using the Arduino analogRead function. We then map it to a range of 0 to
100 using the useful Arduino map function.
The value derived from the map function is then used to set the motor speed. As long
as it is over zero we set the motor speed and then step it one one hundredth of a
revolution, which in the case of my motor will move it two steps or 3 degrees.
After that we do it all again. The result is that the motor speed will be controlled by the
potentiometer.
Note that no attempt has been made to control the motor direction in tis design. If you
wish you can do this by setting the motor speed to a negative number to spin the motor
counterclockwise. The H-Bridge will do the job of reversing the motor voltage polarity to
reverse the motor.
As you can see an L298N makes a great stepper motor controller as well as a DC motor
controller. But like the UNL2003 it still require the Arduino to do all the motor
sequencing. In our next experiment we will use a dedicated motor controller.
https://dronebotworkshop.com 31
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
● The logic for stepping the motor is all contained in the controller, freeing up the
Arduino (or other microcontroller or microcomputer) to do other things.
● The number of connections to the microcontroller or microcomputer is reduced,
making it much easier to control multiple stepper motors
● Doing advanced things like microstepping is easy, without using any special
library or tying up computing resources
● You actually can control the motor without a microcontroller, a simple square
wave oscillator can suffice in many situations.
The A4988 is a very common and inexpensive stepper motor controller that is used a lot
in 3D printers and CNC machines where several stepper motors need to be managed.
Other than the controller and motor it only requires one other part, a decoupling
capacitor that is mounted physically close to the controller. With a heatsink the device
can handle up to 2 amperes.
Let’s take a look at the pinout of the A4988 module before we put it to use:
https://dronebotworkshop.com 32
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Starting from the top right and working down we see the following pins:
● ENABLE – This is an active low connection, when brought low (ground) the
A4988 module is enabled. By default this is pulled low so the module is always
enabled unless you apply a logic high here.
● MS1, MS2, MS3 – These three connections determine the microstepping mode
of the A4988 module. By setting the logic levels here you can set the motor to
Full, Half, Quarter, Eighth or Sixteenth steps. See the chart on the connection
diagram for details.
https://dronebotworkshop.com 33
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
● RESET – This is an active low line that will reset the module. By default it is
pulled high.
● SLEEP – If this line is set low the module will enter a low-powered sleep mode
and consume minimal current. By tying this line to the Reset pin the module will
always be on at full power consumption.
● STEP – This is how you drive the motor from an external microcontroller or
square wave oscillator. Each pulse sent here steps the motor by whatever
number of steps or microsteps that has been set by MS1, MS2 and MS3 settings.
The faster you pulse this the faster the motor will travel.
● DIR – The direction control A high input here drives the motor clockwise, a low
will drive it counterclockwise.
The key thing to note here is that the A4988 only requires two inputs from the Arduino to
control the stepper motor and does not need the Arduino to “figure out” the stepping
logic. This makes it a lot easier to control multiple stepper motors for advanced projects.
Now that we have seen how the A4988 is laid out we will connect one to our Arduino.
https://dronebotworkshop.com 34
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Note that there is an additional component, a 100uf capacitor, in this circuit. This is
essential to decouple the power supply. Any value from 47uf up will suffice, try and
mount the capacitor as close to the A4988 VMOT and GND pins as possible.
Otherwise the connections are quite simple. Make sure to observe the motor
connections, the A4988 is conveniently laid out to match the 4-pin connector that is
common on several bipolar motors but you should check your motor connections to be
sure they are correct.
Before we load our sketch there is one thing that needs to be done. We need to set the
current that flows through our motor coils using a small potentiometer on the A4988
module.
One method of doing this is to measure the voltage at a testpoint (labeled “+”) near the
potentiometer while you adjust it . Use the following formula to derive the current:
Another way is to tie the “STEP” input to high (5-volts) and place an ammeter in series
with one of the motor coils. This Is the method I used in the video.
Once you have the motor current adjusted it’s time to load the sketch:
https://dronebotworkshop.com 35
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
1 /*
3 Stepper-Demo4.ino
7 https://dronebotworkshop.com
8 */
10 // Define Constants
11
12 // Connections to A4988
15
18 void setup() {
19
21 pinMode(stepPin,OUTPUT);
22 pinMode(dirPin,OUTPUT);
23 }
24 void loop() {
25
27 digitalWrite(dirPin,HIGH);
28
https://dronebotworkshop.com 36
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
31 digitalWrite(stepPin,HIGH);
32 delayMicroseconds(2000);
33 digitalWrite(stepPin,LOW);
34 delayMicroseconds(2000);
35 }
36
38 delay(1000);
39
41 digitalWrite(dirPin,LOW);
42
45 digitalWrite(stepPin,HIGH);
46 delayMicroseconds(1000);
47 digitalWrite(stepPin,LOW);
48 delayMicroseconds(1000);
49 }
50
52 delay(1000);
53 }
54
In this sketch we won’t be using any stepper libraries as all we need to do is send a
pulse out to the A4988 and let it do all the “heavy lifting”.
https://dronebotworkshop.com 37
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
We start by defining constants to represent the pins we have connected the A4988
STEP and DIR pins to. We also define STEPS_PER_REV as we did in the previous
sketch, the number of steps our motor needs to complete one rotation. Again you
should set this to match your stepper motor specifications.
Now to the loop. We will do two things here, spin the motor slowly clockwise one turn
and then spin it counterclockwise two turns. We will insert a one second delay between
each spin.
To set the direction of the motor we set the DIR pin either HIGH or LOW depending
upon which way we want to go. A HIGH here will cause the motor to spin clockwise.
The speed is set by the frequency of the pulses we send on the STEP pin. The pulses
are manually generated in a very similar fashion as the Arduino Blink sketch, by
bringing the output HIGH, waiting a bit then Bringing it LOW and waiting again. This is
repeated as many times as necessary to rotate our motor oin the amount we desire,
one full rotation for the first routine and two rotations for the second one.
Of course you can add as many routines as you wish to make your motor move in the
speed and direction you like.
As you can see the A4988 makes it very easy to drive a bipolar stepper motor with a
minimum of code. You can also get a shield for your Arduino that allows you to drive
multiple A4988 modules, which would be great if you are building a CNC machine or a
3D printer.
https://dronebotworkshop.com 38
For more projects and tutorials visit the DroneBot Workshop - https://dronebotworkshop.com
Conclusion
Hopefully this article and the accompanying video have shown you that stepper motors
are not really that hard to work with after all. If you are designing a project that requires
you to be able to position something precisely a stepper motor is an ideal choice.
Please let me know in the comments about any problems or observations you
encounter using stepper motors. I’d really love to hear how you incorporate them into
your own designs.
Now get out there and start building with stepper motors!
https://dronebotworkshop.com 39