So, you’ve outgrown the 28BYJ-48 stepper motor that came with your Arduino starter kit?
Good news—you’re ready for the next level! The NEMA 17 stepper motor is a fantastic upgrade, offering more power, precision, and control, making it perfect for 3D printers, CNC machines, robotics, and more.
But with great power comes… a bit more wiring. Unlike the plug-and-play 28BYJ-48, the NEMA 17 needs a proper motor driver—and the A4988 is one of the most affordable and beginner-friendly choices out there.
In this tutorial, I’ll show you how to wire up your NEMA 17 to the A4988 and take it for a test drive with some example Arduino sketches. You’ll learn how to control both speed and direction, orchestrating precise movements to fit your project’s needs.
- Build & Code Along With Me
- Why You Need a Motor Driver (and No, It’s Not Just to Look Fancy)
- A4988 Motor Driver Specs
- A4988 Motor Driver Pinout
- Keeping Your A4988 Cool: How to Use the Heatsink
- How to Set the Current Limit on the A4988 Stepper Motor Driver
- How to Wire a A4988 Motor Driver Module and NEMA 17 Stepper to Arduino
- Arduino Code Example 1: Set Direction and Speed for NEMA 17 Stepper Motor
- Arduino Code Example 2: MICROSTEPPING
- Arduino Code Example 3: Change Direction and Speed of a Stepper motor
- Stepping Into Bigger Projects
Why You Need a Motor Driver (and No, It’s Not Just to Look Fancy)
Controlling a stepper motor like the NEMA 17 isn’t just about sending signals from your Arduino—it’s about power. A stepper motor demands more juice than your Arduino can handle. The A4988 motor driver acts as the middleman, taking low-power signals from your Arduino and turning them into the high-power, precisely-timed pulses your motor craves. Think of it as the translator who speaks both “Arduino” and “Motorese.”
But that’s not all! The A4988 isn’t just about passing power. It handles microstepping, allowing you to control the motor with surgical precision. Want smooth motion? Done. Need to adjust speed and direction? Easy. Plus, it protects your stepper motor (and your nerves) by letting you set a current limit so nothing overheats or blows up.
Without a motor driver, trying to run a stepper motor would be like trying to jump-start a car with a pair of AA batteries—spoiler: it won’t end well. So, unless you want your Arduino to throw in the towel, let the A4988 do the heavy lifting.
A4988 Motor Driver Specs
The A4988 isn’t just a favorite for controlling NEMA 17 stepper motors—it’s the go-to choice for beginners stepping up (pun intended) to larger and more powerful motors. Once you’ve mastered this little module, transitioning to other drivers is a easy because the fundamentals stay the same.

- Built for the Job: The A4988 is specifically designed for bipolar stepper motors like the NEMA 17. It can handle up to 2A per coil (with proper cooling) and works with a wide voltage range of 8V to 35V, making it flexible for most hobby projects and small-scale robotics.
- Microstepping Magic: Want smoother, quieter motor operation? The A4988 delivers with 5 microstepping modes (full step, half step, quarter step, eighth step, and sixteenth step). This means your motor moves in smaller, more precise increments—perfect for 3D printing, robotics, or any project where accuracy is key.
- Current Limiting = Motor Safety: The A4988 lets you set a current limit using its onboard potentiometer. This isn’t just a nice-to-have—it’s essential for protecting your motor from overheating while squeezing out maximum performance.
- Compact and Cost-Effective: This little module is tiny, easy to wire, and fits right into your breadboard or PCB setup without taking up valuable real estate. Plus, it’s budget-friendly, so you’re not breaking the bank to keep your projects running smoothly.
- Simple Step and Direction Control: The A4988 uses just two pins—STEP and DIR—to control motion. Want your motor to spin faster? Increase the frequency of the STEP signal. Need it to change direction? Toggle the DIR pin. It’s so straightforward that even Arduino beginners will feel like pros.
- Protection Features Built In: With thermal shutdown, overcurrent protection, and undervoltage lockout, the A4988 is like the bodyguard your motor didn’t know it needed. It keeps your setup running smoothly without drama.
The A4988 is popular because it’s a reliable, feature-packed starting point that grows with your skills. Once you’ve mastered it, moving on to fancier drivers will feel like second nature. It’s the ultimate gateway to a world of stepper motor awesomeness!
A4988 Motor Driver Pinout
The A4988 motor driver has 16 pins—each one designed to play a specific role in taming your NEMA 17 stepper motor.

POWER PINS
- VMOT (Motor Power) & GND (Ground): This is where your stepper motor gets its juice. Connect it to a power supply that matches your motor’s voltage (typically 12V or 24V). Don’t forget to slap a decoupling capacitor (at least 47 µF) across VMOT and GND to smooth out voltage spikes and keep things stable.
- VDD (Logic Power) & GND: Powers the internal logic of the A4988. Connect VCC to your Arduino’s 5V (or 3.3V) pin and GND to one of the Arduino GND (ground) pins.
CONTROL PINS
- STEP: This pin tells the motor to take a step. Each pulse (HIGH signal from the Arduino) moves the motor one microstep or step forward, depending on your settings. The faster you pulse it, the faster your motor spins—simple and elegant.
- DIR (Direction): Flip this pin HIGH or LOW to change the direction of your motor. Want clockwise? Set it one way. Counterclockwise? Flip it. It’s like drive and reverse for your stepper motor.
MICROSTEP SELECTION PINS
- MS1, MS2, MS3: These pins control the microstepping resolution. By setting them HIGH or LOW, you can choose between full step, half step, quarter step, eighth step, or sixteenth step. It’s like choosing between tiptoeing and sprinting.
| MS1 | MS2 | MS3 | RESOLUTION |
|---|---|---|---|
| LOW | LOW | LOW | Full Step |
| HIGH | LOW | LOW | Half Step |
| HIGH | HIGH | LOW | Quarter Step |
| HIGH | HIGH | HIGH | Sixteenth Step |
MOTOR OUTPUT PINS
- OUT1A, OUT1B, OUT2A, OUT2B: These pins connect directly to your stepper motor’s coils. OUT1A and OUT1B handle one coil, while OUT2A and OUT2B handle the other. Make sure you wire the coils correctly, or your motor might act like it’s had too much coffee (i.e., jittery and unpredictable).
POWER STATE PINS
- EN (Enable): The ultimate on/off switch for the motor driver. By default, this pin is pulled LOW which enables the driver to operate normally. Pull it HIGH to disable the motor driver. This pin is often used as an emergency stop. If you connect it to a button, you can stop the stepper motor in an instant if your setup goes haywire.
- RST (Reset): Resets the driver when pulled LOW. While LOW, all STEP inputs are ignored and the internal translator causes the stepper motor to return to a “home” state. It’s the initial position where the stepper motor starts and it varies depending on the microstep selection pin settings. The RST pin is a floating pin so tie this to the SLP pin if you’re not planning on using a separate reset function.
- SLP (Sleep): Pull this pin LOW to put the driver into low-power sleep mode. This is convenient for saving energy when not in use. Keep it HIGH for normal operation. Since this pin is LOW by default, connect it to the RST pin to keep it HIGH if you don’t plan on using it.
If you don’t plan on using the functionality of RST or SLP in your project, connect the RST and SLP pins together to keep them both HIGH.
Keeping Your A4988 Cool: How to Use the Heatsink
The A4988 driver might be small, but when you push it hard, it works up a sweat. If you’re driving your stepper motor with currents above 1A, you’ll need to give your A4988 a little extra help staying cool. That’s where the included heatsink comes in.

WHEN TO USE A HEATSINK
The A4988 has built-in thermal protection, but if it overheats, it’ll automatically shut down to protect itself.
Sounds great, right? Except when it shuts down, your motor also stops mid-spin, which is not ideal when precision or timing is crucial. Keeping the driver cool ensures consistent performance and extends its lifespan, so it doesn’t burn out faster than your excitement for a new project.
- Currents Over 1A: If you’re driving your motor above 1A, the A4988 can get hot enough to fry an egg—or at least feel that way. Slapping on the included heatsink (and maybe a dab of thermal adhesive) helps dissipate the heat and keeps things running smoothly.
- Extended Operation: Running your stepper motor for long periods? Heat buildup is inevitable. A heatsink keeps the driver cool even during marathon sessions, whether it’s printing a 10-hour 3D model or testing your robot until the battery gives out.
- Poor Ventilation: If your project lives in a closed box or tight space with little airflow, the A4988 will thank you for the extra cooling. In those situations, you might even consider adding a small fan for extra airflow.
How to Install the Heatsink onto the A4988
The heatsink included with your A4988 motor driver module usually comes with double-sided tape ready to peel and stick directly onto the black driver chip that sits in the center of your driver module. How convenient! Perhaps a little too convenient.
This was fine for intermittent use while I was testing the stepper motor. But for more continuous use, I found that the adhesive softens as the A4988 chip warms up and the heatsink starts shifting around. If the metal heatsink touches any of the pins of the motor driver, it will cause inadvertent pin connections and all of a sudden your stepper motor will act strangely – from jerky movements to not moving at all.
I recommend using a thermal electronics adhesive (thermal glue) to stick the heatsink onto the A4988 chip rather than relying on the double-sided tape that comes with it.
How to Set the Current Limit on the A4988 Stepper Motor Driver
One of the A4988’s best tricks is its ability to limit the current sent to your stepper motor. Why does this matter? Because too much current is like feeding your stepper motor an all-you-can-eat buffet—it’ll gorge itself and overheat, and nobody wants a burnt-out motor (or driver!).
Don’t skip this step!
- Protect Your Stepper Motor: Too much current can fry the coils in your stepper motor, turning it into an expensive paperweight.
- Protect Your A4988: The driver chip has its own limits, and overheating can cause thermal shutdowns, which make your stepper motor stop at the worst possible time.
By setting the current limit, you’re giving your stepper motor just enough power to perform like a champ—without risking an electronics meltdown.
There are two ways to set the current limit: one involves using a multimeter to directly measure the current flowing through one of the motor coils (a bit hands-on and fiddly), and the other—my preferred method—is to calculate and adjust the reference voltage (Vref) on the motor driver.
The best part? This approach doesn’t require the motor to be connected or powered, making it quicker, cleaner, and just plain easier. Simply turn the little potentiometer on the driver module to the correct Vref for your stepper motor.
What Vref value should you use? Let’s find out!
Step 1: Calculate the Vref for your Stepper Motor’s Current Rating
The NEMA 17 stepper motor comes in different sizes (heights) and therefore different current ratings. They can range anywhere from about .3A to 2.5A. To find the current rating for your stepper motor, check the product listing or the stepper motor’s datasheet.
My NEMA 17 stepper motor is rated for 1.5A. If your current rating is different, you can still follow these exact same steps to limit the current of the A4988 to match your stepper motor.
The A4988 uses Vref (reference voltage) to calculate the maximum current it should allow to flow through the stepper motor coils. The formula is:
Vref = Ilimit x 8 x Rs
Where:
- Ilimit: The maximum current per coil (in amps). In my case, it’s 1.5A.
- Rs: The value of the sense resistors on the A4988 board.
How to Find Your A4988 Motor Driver’s Rs Value
Depending on the manufacturer, the value of the current sensing resistor will be different. Because why not, right? The most common values are:
- 0.05Ω: Units made prior to 2017 by Pololu (green in color).
- 0.068Ω: Units made since 2017 by Pololu.
- 0.1Ω: Most generic units made in China.
The best way to determine the value of your resistor is to take a magnifying glass and look for resistors labeled either R5, S1, or S2. There will be an SMD resistor code printed directly on top of the black resistor. Simply look up the SMD code online and you’ll find your value.

Here are the most popular ones:
- R100 or R10: Resistance value of 0.1Ω
- R050 or R05: Resistance value of 0.05Ω
- R200 or R20: Resistance value of 0.2Ω
- R068: Resistance value of 0.068Ω
Taking a close look at my A4988 motor driver, I have two resistors at the S1 and S2 positions. Both have R100 printed on them so my Rs value is 0.1Ω.
I now have all the information I need to calculate Vref:
Vref = 1.5A x 8 x 0.1Ω = 1.2 Volts
I have to set my reference voltage, or Vref, to 1.2 volts. In order to do that, I need to get this A4988 wired up!
Step 2: Wire the A4988 to Set the Vref
The easiest way to wire up your A4988 motor driver module is to use a breadboard. Insert it such that each set of 8 pins is on opposite sides of the dip running down the middle. Pay close attention to the orientation of the trim pot so you can correctly identify the pins.

Next, we only need to make a few connections – no stepper motor needed!
- RST to SLP: Connect the RST and SLP pins of the A4988 driver module together.
- VDD to Arduino 5V: Run a jumper wire from the VDD pin of the A4988 to the 5V pin of the Arduino. You can either run a jumper wire directly or use the power rails of the breadboard.
- GND to Arduino GND: Connect the GND pin of the A4988 (the pin right next to VDD) to a GND (ground) pin of the Arduino, either directly or via the breadboard power rail.
It’s a good idea to start with the potentiometer turned all the way counter-clockwise. This will output the lowest voltage so you can work up from there.
The potentiometer is fragile! If you feel any resistance at the bottom or top ends while turning, stop. Otherwise, it’s easy to break the little tabs that act as mechanical stops and then the potentiometer won’t work anymore to limit the current.
Power the Arduino In Order to Turn On the A4988 Driver Module
To measure the reference voltage, the driver needs to be powered. Plug in the Arduino and we’re now ready to set the Vref! You’ll need a multimeter set to read DC voltage.
Because you only have two hands, the easiest way to do this is to use alligator clips for both your multimeter leads.
- Positive Connection: Clamp one end of the alligator clip cable to the positive probe of the multimeter and the other end to the metal shaft of the screwdriver you intend to use to turn the potentiometer. This way you can read the Vref values in real time as you turn the potentiometer.
- Negative Connection: Touch the black probe to the GND pin on the A4988 driver module (the one that’s connected to the GND pin of the Arduino).
Alternative: It can sometimes be hard to hold your probe against a tiny pin without it sliding around. To make the negative connection easier. Plug a jumper wire into a spare GND pin on the Arduino. Use an alligator clip cable to connect one end to the black probe of the multimeter and the other end to the jumper wire coming from the spare Arduino GND pin. This is more hands free!
Slowly turn the potentiometer until you get the correct Vref, 1.2 volts in my case. There’s a bit of a delay between turning the potentiometer and getting the corresponding reading on the screen so it’s easy to overshoot. Make small adjustments and pause for the readings to catch up.
With the correct Vref set, we can now finish up the rest of the wiring!
How to Wire a A4988 Motor Driver Module and NEMA 17 Stepper to Arduino
We’ve already started wiring the A4988 to the Arduino so let’s continue making the rest of the connections.

A4988 TO A4988 DRIVER MODULE CONNECTIONS
Take a moment to double check that the RST pin is still connected to the SLP pin on the A4988 from the current limiting step above. This prevents the motor driver from going into sleep mode.
| A4988 DRIVER MODULE | A4988 DRIVER MODULE |
|---|---|
| RST | SLP |
A4988 DRIVER MODULE TO ARDUINO UNO CONNECTIONS
The VDD and GND of the driver module should already be connected to the Arduino’s 5V and GND pins respectively. This means that there are only two more connections to make: the DIR (direction) and STEP pins.
| A4988 DRIVER MODULE | ARDUINO UNO |
|---|---|
| VDD | 5V |
| GND | GND |
| DIR | 2 |
| STEP | 3 |
A4988 DRIVER MODULE TO MOTOR POWER SUPPLY CONNECTIONS
Since the NEMA 17 stepper motor draws more current than the Arduino can handle, you’ll need a separate power supply for your stepper motor. I’m using a 12V power supply. An easy way to connect them to your A4988 driver module is to use a female DC jack adapter. In my case, I have breadboard jumper wires attached to mine to make it easy to use with a breadboard.
| A4988 DRIVER MODULE | POWER SUPPLY |
|---|---|
| VMOT | Positive |
| GND | Negative |
Remember to place a large 47µF or more (preferably 100µF) decoupling electrolytic capacitor across the motor’s power supply pins (VMOT & GND) to prevent large voltage spikes from damaging the A4988. Try and mount the capacitor as close to the A4988 VMOT and GND pins as possible.
A4988 DRIVER MODULE TO STEPPER MOTOR CONNECTIONS
Finally, it’s now time to hook up the NEMA stepper motor to the A4988 driver module. Not all NEMA 17 stepper motors have the same wire colors or order their wires in the same way. The objective is to find out which two wires go together. The wires from one coil get connected to 1A and 1B and the other pair to 2A and 2B. The polarity does matter so long as each wire is grouped with its correct partner.
Identifying NEMA 17 Stepper Motor Wires
With the NEMA 17 stepper motor disconnected, try to spin the shaft by hand. It should be relatively easy to spin. Now let’s start testing out some of the wires.
Since my wires have a plastic connector at the end, I’m going to use a breadboard jumper wire to randomly test wire pairs.

Stick one end of the breadboard jumper wire into one of the wire sockets of the connector and the other end into another wire socket, connecting those to stepper motor wires together. It doesn’t matter which one you choose.
Try spinning the stepper motor shaft. If you feel a lot of resistance, you found a pair of wires that belong to the same coil. If you can still spin the the shaft freely, then the two wires belong to different coils. Try another wire.
By process of elimination, you’ll quickly discover which to wires go together. In my case:
- Red and Green: When connected together, I got more resistance when spinning the stepper motor shaft.
- Blue and Black: The same thing happened when I connected the blue and black wires together. The stepper motor shaft was more difficult to turn.
Now that I’ve identified my wire pairs, let’s connect the stepper motor to the A4988. One wire pair will go into 2A and 2B, and the other pair will go into 1A and 1B. The wire orientation doesn’t matter so long as you keep the pairs together.
| A4988 DRIVER MODULE | NEMA 17 STEPPER MOTOR |
|---|---|
| 2B | Red wire |
| 2A | Green wire |
| 1A | Black wire |
| 1B | Blue wire |
With our circuit complete, let’s take it out for a test drive with a few Arduino code examples. The best part is that we don’t need any libraries and you can get your stepper motor spinning with just a few lines of code.
Arduino Code Example 1: Set Direction and Speed for NEMA 17 Stepper Motor
This simple Arduino sketch controls our NEMA 17 stepper motor using the A4988 driver by setting its direction and making it step at a steady pace:
Breaking Down the Code: Step by Step (Pun Intended!)
If you’re new to stepper motors and code, don’t worry—I’ll explain every line so you can feel confident with how this Arduino sketch works.
FIRST, WE DEFINE OUR PINS
// pin connections
const int dirPin = 2; // direction pin
const int stepPin = 3; // step pin
- We start by declaring two pins:
dirPin(Direction Pin) on pin2– this controls which way the motor spinsstepPin(Step Pin) on pin3– this tells the motor to take a step when pulsed
- Using
const intmakes sure these pin assignments never change accidentally.
SETTING UP THE PINS
void setup() {
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
// set direction of rotation to clockwise
digitalWrite(dirPin, HIGH);
}
- The
setup()function runs once when the Arduino starts. pinMode(dirPin, OUTPUT);– Tells the Arduino thatdirPinwill send (output) signals (not receive them).pinMode(stepPin, OUTPUT);– Same thing forstepPin.digitalWrite(dirPin, HIGH);– Sets the direction clockwise.- The A4988 reads
HIGHorLOWon this pin to determine the motor’s direction. - HIGH usually means clockwise, LOW means counterclockwise, but this depends on how the motor is wired. If it spins the wrong way, just flip it to LOW!
- The A4988 reads
MAKING THE MOTOR STEP
void loop() {
// take one step
digitalWrite(stepPin, HIGH);
delayMicroseconds(2000);
// pause before taking next step
digitalWrite(stepPin, LOW);
delayMicroseconds(2000);
}
- The
loop()function runs over and over forever, so the motor keeps stepping continuously. digitalWrite(stepPin, HIGH);– This sends a pulse to the motor driver, telling it to take one step forward.delayMicroseconds(2000);– Waits 2000 microseconds (2 milliseconds) to let the motor register the step.digitalWrite(stepPin, LOW);– Pulls the step pin LOW, resetting it for the next pulse.delayMicroseconds(2000);– Another short pause before sending the next step pulse.
HOW TO CHANGE THE SPEED?
- Faster movement? Decrease
delayMicroseconds(2000);(e.g.,delayMicroseconds(1000);speeds it up). - Slower movement? Increase the delay.
HOW TO CHANGE DIRECITON?
- Change
digitalWrite(dirPin, HIGH);todigitalWrite(dirPin, LOW);insetup().
This is stepper motor control at its simplest—one step at a time, controlled by pulses.
At slow speeds, it’s easier to see each individual step, especially because we’re operating the stepper motor in full step mode. Next, let’s try a different step mode!
Arduino Code Example 2: MICROSTEPPING
None of the microstep selection pins (MS1 – MS3) on the A4988 motor driver are connected to the Arduino Uno, so by default this will put the stepper motor into full step mode. But if we connect these pins and send different combinations of LOW and HIGH signals to them, it will allow us to try out other modes like half-stepping, quarter-stepping and sixteenth-stepping.
Back at the circuit, let’s get the MS1, MS2 and MS3 pins connected:
| A4988 MOTOR DRIVER | ARDUINO UNO |
|---|---|
| MS1 | 8 |
| MS2 | 9 |
| MS3 | 10 |
Using the same Arduino sketch as before, let’s add some code to operate in quarter-stepping mode:
Additions to the Sketch: Setting the A4988 to Quarter-Step Mode
In this updated sketch, we’re adding microstepping to our stepper motor control. Instead of moving one full step per pulse, we’re dividing each step into four smaller steps (quarter-step mode). This makes the stepper motor move more smoothly and precisely—perfect for applications where accuracy is key.
Let’s go over the additional lines that make this magic happen!
NEW PIN DECLARATIONS
const int ms1 = 8;
const int ms2 = 9;
const int ms3 = 10;
- These are the microstepping control pins on the A4988.
- They allow us to select how many microsteps we want per full step.
- By toggling them HIGH or LOW later on in the sketch, we control the stepping mode.
CONFIGURING THE MICROSTEPPING PINS IN setup()
pinMode(ms1, OUTPUT);
pinMode(ms2, OUTPUT);
pinMode(ms3, OUTPUT);
- Since we’re sending signals to these pins, we declare them as
OUTPUT. - The A4988 will read these signals and adjust the motor’s stepping mode accordingly.
SETTING THE DRIVER TO QUARTER-STEP MODE
digitalWrite(ms1, LOW);
digitalWrite(ms2, HIGH);
digitalWrite(ms3, LOW);
- This configuration enables quarter-step mode based on the A4988’s stepping table posted in the pinout section of this tutorial.
- By setting MS1 = LOW, MS2 = HIGH, and MS3 = LOW, we tell the A4988 to move one-quarter of a full step per pulse.
- This means the motor will require four times as many pulses to complete a full step, making it more precise and smoother.
WHY USE delay() INSTEAD OF delayMicroseconds()?
delay(250);
Instead of using delayMicroseconds() like in the previous example, I changed the timing to use delay() which takes milliseconds. I only did this because the movement would have been too fast for you to notice the quarter-step effect if I had left the timing as is. Normally, you would use delayMicroseconds().
Now your stepper motor moves with four times the precision—one step at a time, just a little smaller!
For the next code example, I put the stepper motor back into full-step mode by removing the 3 wires from the MS1 – MS3 pins. Be sure to unplug the Arduino and stepper motor before making any wiring changes!
Arduino Code Example 3: Change Direction and Speed of a Stepper motor
So far we’ve spun the stepper motor in one direction and speed. But what if you wanted to orchestrate a series of movements that switch directions and change speeds within the same program?
In this Arduino sketch, we’ll spin the stepper motor clockwise at a slower speed to make a full revolution, and then spin it counter-clockwise at a faster speed for a full revolution.
Breaking Down the Code: Changing Stepper Motor Speed & Direction
This sketch takes your stepper motor control to the next level—not only does it make the motor spin, but it also changes speed and direction in a single loop. If you’re ready to level up your stepper motor skills, let’s break it down line by line so you know exactly what’s happening under the hood.
Let’s look at some of the additions and changes that make this sketch work:
DEFINING STEPS PER REVOLUTION
const int stepsPerRevolution = 200;
- A standard NEMA 17 stepper motor has 200 steps per revolution (meaning 1.8° per step).
- If your motor has a different step count, update this value to match it.
MAKING THE MOTOR SPIN CLOCKWISE (Slowly)
digitalWrite(dirPin, HIGH);
- Setting
dirPinto HIGH tells the A4988 to rotate the motor clockwise (CW). - If your motor spins the wrong way, flip HIGH to LOW here.
SPINNING SLOWLY FOR ONE FULL REVOLUTION
for (int step = 0; step < stepsPerRevolution; step++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(5000);
digitalWrite(stepPin, LOW);
delayMicroseconds(5000);
}
This for loop runs 200 times (or whatever value is in stepsPerRevolution), making the motor complete one full turn.
Each iteration does this:
digitalWrite(stepPin, HIGH);→ Sends a pulse to take one step forward.delayMicroseconds(5000);→ Waits 5000 microseconds (5 milliseconds) before the next step (this controls speed!).digitalWrite(stepPin, LOW);→ Resets the pulse for the next step.delayMicroseconds(5000);→ Another short pause for stability.
PAUSE BEFORE CHANGING DIRECTION
delay(1000);
- Waits 1 second so we can clearly see the motor stop before switching directions.
MAKING THE MOTOR SPIN COUNTERCLOCKWISE (Faster)
digitalWrite(dirPin, LOW);
- Now we flip the direction to counterclockwise (CCW) by setting
dirPinLOW.
SPINNING FASTER FOR ONE FULL REVOLUTION
for (int step = 0; step < stepsPerRevolution; step++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
- Same process as before, but now delay is only 1000 microseconds (1 millisecond), making the motor move five times faster.
- Less delay = higher step frequency = faster rotation.
FINAL PAUSE BEFORE REPEATING
delay(1000);
- Another 1-second delay before the loop starts over again, making the motor switch back to clockwise motion.
HOW TO CUSTOMIZE IT?
- Want it even slower? Increase
delayMicroseconds()values. - Want it crazy fast? Reduce
delayMicroseconds()(but don’t go below 500 μs, or the A4988 might not register steps reliably). - Want more precise movement? Enable microstepping by adjusting MS1, MS2, and MS3 pins.
This sketch is a great foundation for more advanced stepper motor control, like acceleration, deceleration, and orchestrating precise routines. But for now, enjoy making your motor change speed and direction to fit your needs!
Stepping Into Bigger Projects
Congrats! You’ve just leveled up from beginner stepper motors to full-fledged NEMA 17 control. You’ve learned how to wire up the A4988 driver, tweak the current limit, and run Arduino code to control both speed and direction. Not too shabby, right?
Now that you’ve got the basics down, the possibilities are endless. Want smoother motion? Try microstepping. Need precise positioning? Add limit switches or an encoder. Building a robot arm, CNC machine, or motorized camera slider? You’ve got the skills to make it happen.
The best way to get comfortable with stepper motors is to experiment—play around with different speeds, change up the step sizes, and see how your motor behaves. If something doesn’t work, don’t panic—it’s all part of the learning process.
So go forth and make things move! And remember: it’s all about one step at a time.
