Arduino Mega - Stepper Motor A4988 Driver
Look at a 3D printer from the side. Beside the print stands a long threaded rod, the lead screw. After each finished layer, a stepper motor turns this rod a tiny bit, and the height changes by the thickness of one layer. The same trick can move a camera, a microscope table or a small lift. This tutorial instructs you how to use the Arduino Mega 2560 with the A4988 driver to run the motor of such a height axis, a NEMA 17 stepper motor. In detail, we will learn:
- What happens inside the A4988 when a pulse arrives, and where each of its 16 pins belongs
- How steps turn into millimeters on a lead screw, and how the step size and the motor current are set
- Which wires go between the Arduino Mega, the driver, the motor and the 12V adapter, with the expansion board and with the bare module
- How to write two Arduino Mega sketches: one screw turn up and one down with no library, and then smooth trips to heights in millimeters

We suggest:
- Reading the Arduino Mega - Stepper Motor page if the words coil, full-step and half-step are new to you.
- Reading the Arduino Mega - DRV8825 Stepper Motor Driver page if your platform is heavy and its motor asks for more current than an A4988 can give.
- Reading the Arduino Mega - Controls 28BYJ-48 Stepper Motor using ULN2003 Driver page if you own the little 5-wire motor. It belongs to another driver, not to the A4988.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
The expansion board is optional, it just saves you some wires. A second drawing below shows the A4988 on its own. You do not need a lead screw either. For all the tests, the motor may lie on your desk with a small flag of tape on its shaft.
Overview of A4988 Stepper Motor Driver and Its Pinout

An output pin of the Mega can light an LED, but it cannot feed a motor coil. The A4988 does that heavy work. One A4988 runs ONE bipolar stepper motor. The DIYables version is red and small, roughly 20 x 15 mm. It has the StepStick shape, for which many 3D printer boards have sockets. Its 2 x 8 male pins come soldered, and you find a little stick-on heat sink in the package.
Only two signals are needed. DIR tells the chip which way to go, and a pulse on STEP tells it to go one step further. The chip has a built-in "translator", a kind of list with the coil pattern of every step, so the sketch never deals with the coils. At the same time, the chip switches the motor voltage on and off very quickly (this is called chopping) and so keeps the coil current at the limit that you choose. Thanks to this, the label of the motor may say 2V to 4V and a 12V adapter is still fine.
| DIYables A4988 Module | |
|---|---|
| Motor | one bipolar stepper motor with 4 wires (a 6-wire motor works too, with 4 of its wires) |
| VMOT, motor supply | 8V to 35V DC, here 12V |
| VDD, logic supply | 3V to 5.5V, here 5V |
| Coil current, no heat sink | around 1A |
| Coil current, heat sink and good cooling | 2A at most |
| Step sizes | full, 1/2, 1/4, 1/8, 1/16 |
| Shortest STEP pulse | 1 us HIGH plus 1 us LOW |
| Built-in protection | too hot, supply too low, crossover current |
| Error (FAULT) pin | not available |
Left column of the module, starting at the top:
- EN pin: may be left open. The module itself pulls it LOW, and LOW means that the motor outputs are on. If it gets a HIGH, the outputs go off and you can turn the shaft with your fingers.
- MS1, MS2 and MS3 pins: pick the step size. An open pin counts as LOW, so three open pins give full steps.
- RST pin: must be connected to SLP. It is the reset input (active LOW), and nothing on the module holds it at a fixed level.
- SLP pin: must be connected to RST. It is the sleep input (active LOW). The module pulls it HIGH, and through the short wire RST becomes HIGH too.
- STEP pin: must be connected to pin 4 of the Mega. Every change from LOW to HIGH is one step, or one microstep.
- DIR pin: must be connected to pin 3 of the Mega. Its level decides between up and down.
Right column, again from the top:
- VMOT pin: must be connected to + of the 12V adapter, and to nothing else.
- GND pin under VMOT: must be connected to - of the 12V adapter.
- 2B with 2A: must be connected to one motor coil, one end on each pin.
- 1A with 1B: must be connected to the other motor coil in the same way.
- VDD pin: must be connected to the 5V pin of the Mega.
- GND pin under VDD: must be connected to GND of the Mega. On the module, this GND and the one under VMOT are the same point.
※ NOTE THAT:
Turn your module over and read the motor pins. On top they are called B2, A2, A1, B1, and on the underside 2B, 2A, 1A, 1B. Do not let this confuse you, only the order of letter and digit differs. A board from another maker may use other names again, so trust the print on your own module more than any picture.
Why VDD Needs the 5V Wire
Beginners often miss this wire, because some other stepper drivers do without it. The logic part of the A4988 has no power of its own: whatever it needs must come in at VDD.
VDD also sets the border between LOW and HIGH. A signal counts as a sure HIGH from 0.7 x VDD upward. For VDD = 5V, the border is 3.5V. The Mega sends 5V signals, safely above that line, so the 5V pin is the right partner for VDD. The general rule is easy to keep in mind: VDD gets the same voltage as the signal pins of your board, which would be 3.3V on a 3.3V board.
Overview of the NEMA 17 Stepper Motor

The picture shows two motors with different wire colors and the same inside: a bipolar stepper motor hides two coils. Each coil has two ends, so four wires leave the motor: one pair of wires per coil. A step is not a guess but a fixed angle. Our motor moves 1.8 degrees per full step, and 360 / 1.8 = 200 full steps bring the shaft once around.
From Steps to Millimeters
A lead screw changes turning into lifting. The one in our example raises its nut, and the platform on it, by 8 mm in one turn. That makes the math short:
- 200 steps / 8 mm = 25 steps for 1 mm
- 8 mm / 200 steps = 0.04 mm for one step
- A print layer of 0.2 mm = 5 steps
If your screw has another lead (the travel per turn), put your own number in place of 8.
Step Size and the DIP Switches
A full step can be divided into 2, 4, 8 or 16 microsteps. MS1, MS2 and MS3 make the choice:
| Step size | MS1 | MS2 | MS3 | Steps per turn | Steps per mm (8 mm screw) |
|---|---|---|---|---|---|
| Full step | LOW | LOW | LOW | 200 | 25 |
| 1/2 step | HIGH | LOW | LOW | 400 | 50 |
| 1/4 step | LOW | HIGH | LOW | 800 | 100 |
| 1/8 step | HIGH | HIGH | LOW | 1600 | 200 |
| 1/16 step | HIGH | HIGH | HIGH | 3200 | 400 |
With a finer step, the axis runs softer and makes less noise. On the other hand, the Mega must send many more pulses per millimeter, and the sketch has to know the new number: it is the constant MICROSTEPS in both programs of this page.
The expansion board has a 3-way DIP switch for these pins, and ON means HIGH. Read the numbers with care, because they count backwards:
- Switch 3 belongs to MS1
- Switch 2 belongs to MS2
- Switch 1 belongs to MS3
We begin with all three at OFF, that is full step.
Set the Motor Current with VREF
The round trimmer on the module limits the current of each coil. Nobody has to measure that current. There is a small voltage with the name VREF that goes up and down with it, and a multimeter reads it easily. The formula is: current limit = VREF / (8 x Rs). Here Rs means the two small sense resistors. Those on the DIYables module carry the print R100, which is 0.1 ohm, and the formula becomes VREF = 0.8 x current:
| Wanted limit for each coil | VREF |
|---|---|
| 0.3A | 0.24V |
| 0.5A | 0.40V |
| 0.75A | 0.60V |
| 1A | 0.80V |
Above about 1A per coil, the heat sink and good cooling are a must.
How to set it:
- Find the rated current of your motor in its datasheet and choose a limit that is not higher. Begin with a low value.
- Power VDD: the 5V and GND wires of the wiring section must be in place and the Mega must be on USB.
- Set a multimeter to DC volts. Its black probe touches GND, its red probe touches the trimmer itself (the screw part on top is metal).
- Turn the trimmer by a very small angle with a little screwdriver, then read the meter again. The blade must not touch any other part.
If the sense resistors of your A4988 board carry the print R050 (0.05 ohm), the rule changes to VREF = 0.4 x current.
Keep in mind that a platform at rest still needs the motor. Its coils draw a holding current all the time. Expect a warm motor and a warm driver, also when the axis has not moved for minutes.
Wiring Diagram
Two signal wires, 5V and GND are all that the Arduino Mega has to give. The energy for the motor comes from the 12V adapter. Connect every wire first, and let the plug of the 12V adapter be the very last thing you put in.
With the Expansion Board
The expansion board measures 42 x 42 mm and carries the A4988 module in two 8-pin sockets:

This image is created using Fritzing. Click to enlarge image
※ NOTE THAT:
- The module fits both ways, but only one is right. EN, STEP, DIR and their neighbors belong in the socket nearest to the G V S header (the left one). Check it at the other corner: VMOT must sit beside the small "VIN" arrow. Power on a module that sits the wrong way round can destroy it.
- Touch the motor plug only when the power is off. Plugged or unplugged under power, a coil makes a voltage spike that can kill the A4988.
- Slide all three DIP switches to OFF.
The wires from the Mega end at the G V S header (G = ground, V = logic supply, S = signal):
| Arduino Mega 2560 Pin | Expansion Board Pin |
|---|---|
| D4 | S pin in the STEP row |
| D3 | S pin in the DIR row |
| 5V (POWER header) | V pin, any row |
| GND (the one next to pin 13) | G pin, any row |
| nothing | S pin in the EN row (open = driver on) |
The three V pins are one point, and so are the three G pins, so you need no more than one red and one black wire here. The V wire is not a spare part of the drawing: it carries the 5V to VDD, and without it the A4988 stays dead.
The 12V side uses the green screw terminal, which is marked 12-24V:
| 12V Adapter with DC Jack | Expansion Board Terminal |
|---|---|
| plus (+) | VIN |
| minus (-) | GND |
The board brings its own capacitor (100uF / 35V) for the 12V line and its own link between RST and SLP. The ground of the adapter meets the ground of the Mega on this board, so both share one ground. The 12V itself stays at VIN: keep it away from every pin of the Mega.
The Four Motor Wires
The white 4-pin connector takes the motor cable. What counts is that each coil stays together: one coil on 2B and 2A, the other coil on 1A and 1B.

| Driver Pin | Motor Wire | Other Name | Color on Our Motor |
|---|---|---|---|
| 2B | A | A+ | black |
| 2A | C | A- (A with a bar) | green |
| 1A | B | B+ | red |
| 1B | D | B- (B with a bar) | blue |
Brown, yellow, pink and purple in the wiring images are just line colors. Real motors follow the colors of their maker, so look them up in the datasheet. If you have no datasheet, let the motor tell you: a multimeter shows a few ohms between the two wires of one coil. You can also touch two wires together and turn the shaft by hand. When it suddenly becomes harder to turn, you have found a coil.
Exchanging the two wires of ONE coil does no harm, up and down simply change places. A pair made from two different coils, however, gives you a motor that hums and shivers but never turns.
Without the Expansion Board
The bare module does the same job with a few more wires. The pins on the Mega do not change:

This image is created using Fritzing. Click to enlarge image
| A4988 Pin | Where It Goes |
|---|---|
| STEP | D4 of the Mega |
| DIR | D3 of the Mega |
| VDD | 5V of the Mega |
| GND (under VDD) | GND of the Mega, in the drawing the one at the end of the double header |
| VMOT | plus (+) of the 12V supply |
| GND (under VMOT) | minus (-) of the 12V supply |
| 2B and 2A | first coil (motor wires A and C) |
| 1A and 1B | second coil (motor wires B and D) |
| RST and SLP | joined with each other |
| EN, MS1, MS2, MS3 | left open |
This time you have to take care of two details by yourself:
- Join RST and SLP. It is the short orange wire in the drawing. If it is missing, RST floats and the motor does not move at all.
- Add a 100uF capacitor between VMOT and the GND pin right below, with legs as short as possible and the plus leg on VMOT. A type for 35V or higher is best. The drawing does not show this capacitor.
The warning about the motor wires is the same as before: no plugging and unplugging while the driver has power. The adapter and the Mega share their ground through the module, because its two GND pins are one point.
How To Program For A4988 Stepper Motor Driver
The first sketch works without a library. Its building blocks are:
- The first step is to name the pins and both levels of DIR:
- Calculate the steps of one turn. The pulse gets shorter when the steps get smaller, so the speed of the screw does not change:
- Make both pins outputs in the setup() function:
- Set the direction before the first pulse and give it a few microseconds:
- Send one pulse per step. The motor moves on the HIGH edge, and the LOW part prepares the next one:
Arduino Mega Code for A4988 Stepper Motor Driver - One Screw Turn Up and Down
The screw turns once for UP, rests for 1.5 seconds, turns once for DOWN, rests again, and so on. A step takes 2000 + 2000 microseconds, which is 250 steps per second. One turn then lasts 0.8 seconds (75 RPM), and a platform on the screw would climb 10 mm per second. This speed is low enough for the motor to start at once.
Detailed Instructions
- Is it your first project with this board? Install the Arduino IDE on your PC. The Mega is in its board list from the start, so no extra board package is needed.
- Build one of the two wirings above. Keep the 12V adapter out of the socket.
- Plug the USB A/B cable into the Arduino Mega and the PC.
- In the Arduino IDE, choose Arduino Mega or Mega 2560 as the board and select its port.
- Set the VREF of your driver as described above, starting with a low current.
- Paste the code into an empty sketch and press the Upload button.
- When the upload is finished, plug in the 12V adapter.
- Open the Serial Monitor at 9600 baud and compare the text with the tape flag on the shaft:
The left half of a line is printed when the screw begins to turn, and the right half when the turn is over. After each line, the flag must point to exactly the same place. If it lands somewhere else, steps were lost. If your axis goes down during an [UP] line, unplug the 12V adapter and cross the two wires of one coil. Changing the code also works: define UP as LOW and DOWN as HIGH.
Arduino Mega Code for A4988 - Smooth Travel to a Height in Millimeters
Ask a loaded motor to jump straight to a high speed and it skips steps or stalls. The Mega does not notice this, because nothing reports the real position. The cure is to speed up gently and to slow down before the stop. The AccelStepper library by Mike McCauley does both.
In this sketch, the platform visits four heights: 50 mm, 20 mm, 80 mm and back to 0 mm, with a rest of 2 seconds at each one. Speed (28 mm per second) and acceleration (20 mm per second, gained every second) are also written in millimeters, and the sketch converts them to steps.
Detailed Instructions
- The wiring stays as it is. Unplug the 12V adapter.
- With a real lead screw: bring the platform to its lowest point by hand, because the sketch takes the place at power-on as 0 mm. Check that more than 80 mm of free travel is above it.
- Click the Libraries icon at the left edge of the Arduino IDE.
- Enter AccelStepper in the search field and look for the entry by Mike McCauley.
- Press Install on that entry.
- Search for AccelStepper created by Mike McCauley
and click the Install button.
- Load the second code into the IDE and upload it to the Arduino Mega.
- Open the Serial Monitor (9600 baud) before the motor gets power, because opening it restarts the sketch.
- Press the RESET button of the Mega and hold it, plug the 12V adapter in, then let the button go. Now the list of heights starts with the platform at 0 mm:
Listen to the motor: its tone rises at the start and falls before each stop. The trip from 50 mm to 20 mm is so short that braking begins before the top speed is reached, while the long way from 80 mm to 0 mm (about 4 seconds) has a clear steady part in the middle.
Two things to remember when you change this sketch:
- Every step is made inside run(). A delay() in the loop would freeze the motor, so the rest is measured with millis().
- On the Mega, the library manages about 4000 steps per second. 28 mm per second is fine up to 1/4 step (2800 steps per second). For 1/8 or 1/16 step, choose a lower TOP_SPEED, for example 8.
Troubleshooting
Pull the 12V plug before you touch any wire.
| What you see | Where to look |
|---|---|
| Shaft turns freely by hand, nothing moves | The V (VDD) wire is missing, the 12V is missing, or RST and SLP are not joined on a bare module |
| Motor is stiff but stands still | The STEP wire or the G wire of the Mega is loose, or sits in another row |
| Motor hums or shivers | Wires of two coils are mixed, VREF is too low, or the speed / acceleration is too high |
| UP and DOWN are exchanged | Swap the two wires of one coil |
| Driver gets very hot | Lower VREF, stick the heat sink on, let air reach the board |
| Platform travels only a part of the height, or too far | DIP switches and MICROSTEPS do not agree, or your screw has another lead than 8 mm |
| Height is a little off after many trips | Steps get lost, reduce TOP_SPEED or ACCELERATION |
Additional Knowledge
A4988 vs DRV8825
Both modules have the same shape and fit the same expansion board:
| A4988 | DRV8825 | |
|---|---|---|
| Pins used on the Mega | D4 (STEP), D3 (DIR), 5V, GND | D4 (STEP), D3 (DIR), GND |
| Amps per coil without a heat sink | around 1 | around 1.5 |
| Amps per coil, well cooled | 2 | 2.2 |
| Voltage for the motor | from 8V to 35V | from 8.2V to 45V |
| Smallest microstep | 1/16 | 1/32 |
For a light platform on a NEMA 17, the A4988 is all you need. Take the DRV8825 when the motor has to lift more or when you want even finer steps. The code for both is the same, only the list of step sizes differs. The wiring is on the Arduino Mega - DRV8825 Stepper Motor Driver page.
What Holds the Platform Up, and Where Is Zero?
A motor that rests with the driver on keeps its place. If the 12V goes away, or if EN becomes HIGH, the shaft is free. A steep screw like ours (8 mm in one turn) may then be turned by the weight of the platform, and the platform sinks. Put something under a heavy load before you switch the power off.
After a restart, the Mega cannot know the real height. Printers solve this by "homing": the axis moves slowly until it presses a limit switch, and that place becomes 0 mm. The Arduino Mega - Stepper Motor and Limit Switch page shows how a switch stops the motor.
Ideas for Your Axis
- Choose the height with a knob: a rotary encoder click adds or removes 1 mm.
- Show the height in big digits on an OLED display.
- Raise a camera by 0.2 mm after each photo to get a stack of pictures of a tiny object.
- Add a second and a third axis. One more A4988 needs just two more signal pins, and the Mega has dozens of them.