FORGE
Now, flash the code, from hsmag.cc/lwNQKt, to
your Arduino and if everything goes according to plan,
your motor should rotate alternately 360° clockwise,
then 360° anti-clockwise, with 0.5 second pauses,
until you remove the power. Now, let’s take a closer
look at what’s going on.
CODE CLOSE-UP
const int step_pin = 5;
const int direction_pin = 4;
int delay_time = 500;
Lines 1–2 of the code above set the values of
the ‘step’ and ‘direction’ pins we’re using. Then the
last line of code sets the delay time (in microseconds)
between steps. 50 μs gives maximum motor speed
of ~10 000 steps per second or, to put it another
way, ~180 rpm. It doesn’t work well if you try to go
faster. If you need faster rotation, consider using
fewer microsteps (see the data sheet at hsmag.cc/
nYKMEQ). At the slow end, 5000 μs works perfectly
well too, giving a very usable range.
digitalWrite(direction_pin, HIGH);
for(int x = 0; x < 3200; x++) {
digitalWrite(step_pin, HIGH);
delayMicroseconds(delay_time);
digitalWrite(step_pin, LOW);
delayMicroseconds(delay_time);
}
Looking closer at the code above, the line
digitalWrite(direction_pin, HIGH); sets the
motor rotation clockwise – LOW would be used to set it
to anti-clockwise.
The code then loops 3200 times to generate – you
guessed it – 3200 HIGH/LOW pulses to the step pin.
Each complete HIGH/LOW cycle signals the A4988 to
perform a step. There is a short delay of delay_time μs
between each HIGH or LOW, so each step takes two
times delay_time μs.
Which all means that you set the motor direction
by driving direction_pin HIGH or LOW, and the motor
speed by adjusting the value of delay_time, and the
number of steps by writing pulses to
step_pin. Now you can incorporate either kind of
stepper motor into your projects and, well, take over
the world. Happy making!
SETTING THE
Once your circuit is wired up, you’ll need to set the
value of Vref using the potentiometer.
Examine sense resistors (Rs) marked S1 and S2.
The example board has R100.
Look up the value on hsmag.cc/DlkyOH
R100 = 0.100 Ω
Look up the maximum current rating of your
stepper motor. 17HS08-1004S = 1 A
Now use the equation...
Itripmax * 8Rs = Vref
In this case, Itripmax = 1 A and Rs = 0.100 Ω
So Vref = 1 * 8 * 0.1 = 0.8 V
Now power up your board and motor and adjust
the potentiometer until the voltage between its metal
top and GND equals 0.8 V (or whatever you calculated
if your values are different). This will set the motor
current limit at 1 A.
A4988 CURRENT LIMIT
Arduino Nano powered by USB. Motor
powered by >8 V supply.
3 × 12 kΩ resistors used to ensure 1/16
micro-stepping resolution.
100 μF electrolytic capacitor.
Blue, red, black, green motor wire
colours match those of my motor.
The full code for
these stepper motor
projects can be
found on GitHub:
hsmag.cc/BiqFPe
QUICK TIP
Figure 3
Wiring the NEMA 17
17HS08-1004S is a
bit more involved, but
isn’t difficult if you are
methodical and take
your time
Left
Wiring for the unipolar
28BYJ-48 circuit (left)
is very simple. The
bipolar NEMA 17
17HS08-1004S (right)
is a bit more complex,
and requires separate
motor power