Controlling stepper motors with Arduino
SCHOOL OF MAKING
Steps_travel = Steps_per_output_revolution;
small_stepper.setSpeed(500);
small_stepper.step(Steps_travel);
The Stepper command creates an instance of
the Stepper class, sets up the number of steps per
revolution, and the ‘firing order’ of the Arduino pins.
Steps_travel specifies how many steps we want
the motor to travel. In our case that’s 2048 steps =
+360° (-2048 would be -360° anti-clockwise). Then
small_stepper.setSpeed() sets the motor speed in
rpm. A practical maximum speed for this kind of
motor is 500 rpm. Note that this does not consider
the gearbox, so our real shaft output speed will be
64 times lower. Also small_stepper.step() rotates
the stepper motor until it has completed the requisite
number of steps. So you can control the speed,
direction, and number of steps. That is all you need to
control this kind of stepper motor.
NEMA 17 17HS08-1004S USAGE
The 17HS08-1004S is a small 1 A bipolar motor, costing
around £6. It has a 1.8° full step angle (200 steps per
revolution). Using sixteenth step microstepping on the
Allegro A4988 driver, this motor has 3200 microsteps
per revolution. It’s a much more ‘industrial’ motor than
the 28BYJ-48, which is why larger versions are used in
CNC machines and 3D printers.
Connect up your wiring according to the Figure 3
circuit diagram. The Allegro A4988 driver requires a
separate motor power supply of at least 8 V, 1 A.
CIRCUIT SPECIFICS
The degree of microstepping is selectable either with
hard-wired resistors or using software and Arduino
pins driven HIGH/LOW. Here, three 12 kΩ resistors
are used to pull Microstep Select pins MS1-3
HIGH to 5 V permanently, setting microstepping to
sixteenth steps.
The enable pin (ENA) can also be used to
disconnect the motor power instantly when driven
HIGH. This is useful if you want to incorporate end-
stop switches into your project, to prevent it from
destroying itself by travelling too far.
STEPPER MOTOR DRIVER BOARDS
Generally you should not connect an
inductive load (e.g. motor or solenoid)
directly to sensitive electronics like a
microcontroller or computer. This is
because, when a coil is de-energised,
it can try to push a high voltage spike of
‘back EMF’ (electromotive force) back
along the power lines. This can damage
or destroy sensitive microcontrollers
and other components. The problem is
overcome with diodes and transistors.
Diodes prevent electrical flow in the
‘wrong’ direction. Transistors act like relay
switches, allowing a low-power device to
switch a much higher-power device safely
and without damage.
The ULN2003 chip in the unipolar
stepper driver (see Figure 2 on the
previous page) has both transistors and
a diode built in. When ‘In1’ is connected
to a signal greater than 2 V, a pair of
transistors in the chip connect motor
wire A to GND, making a circuit in that
coil phase. The unipolar stepper motor is
rotated by controlling the ‘firing order’ and
speed of the four coil phases using four
Arduino outputs.
The Allegro A4988 chip in the bipolar
stepper driver (see Figure 2 again,
left-hand side) is a lot more sophisticated
and is able to do far more than just
switching the coils on and off. It is able to
do micro-stepping in half, quarter, eighth,
and sixteenth steps by clever modulation
of the output voltage and current. You can
find out more about this in the data sheet
at: hsmag.cc/Cgomvr.
It is also able to ‘chop’ the current
flowing through the coils to prevent
overloading and damage. It requires a
separate power supply >8 V for the motor.
Only two Arduino connections are needed
- direction and pulse. The direction pin is
driven either HIGH or LOW to determine
motor direction. The pulse pin is pulsed in
real time to generate steps.
The number of
magnets, and the
way the coils are
wound, determines
how many full
steps there are per
revolution. 28BYJ-48
is 32 steps. NEMA 17
is typically 200 steps.
QUICK TIP
Figure 2
Stepper motors
require a driver
board to control the
flow of electricity
through the coils –
bipolar on the left,
unipolar right