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:

Arduino Mega 2560 and A4988 driver lift a platform with a NEMA 17 motor

We suggest:

Hardware Preparation

1×Arduino MEGA
1×Alternatively, DIYables MEGA Development Board
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Stepper Motor Nema 17
1×A4988 Stepper Motor Driver
1×Expansion Board for A4988
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino Uno/Mega
1×Recommended: Sensors/Servo Expansion Shield for Arduino Mega
1×Recommended: Breadboard Shield for Arduino Mega
1×Recommended: Enclosure for Arduino Mega

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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

A4988 stepper motor driver module 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

Bipolar stepper motor pinout

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:

The wiring diagram between Arduino Mega A4988 stepper motor driver expansion board

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.

Stepper motor coil color mapping
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:

The wiring diagram between Arduino Mega A4988 stepper motor driver module

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:
#define STEP_PIN 4 #define DIR_PIN 3 #define UP HIGH #define DOWN LOW
  • Calculate the steps of one turn. The pulse gets shorter when the steps get smaller, so the speed of the screw does not change:
const int MICROSTEPS = 1; const int STEPS_PER_TURN = 200 * MICROSTEPS; const unsigned int HALF_PULSE_US = 2000 / MICROSTEPS;
  • Make both pins outputs in the setup() function:
pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT);
  • Set the direction before the first pulse and give it a few microseconds:
digitalWrite(DIR_PIN, UP); delayMicroseconds(5);
  • Send one pulse per step. The motor moves on the HIGH edge, and the LOW part prepares the next one:
for (int i = 0; i < STEPS_PER_TURN; i++) { digitalWrite(STEP_PIN, HIGH); delayMicroseconds(HALF_PULSE_US); digitalWrite(STEP_PIN, LOW); delayMicroseconds(HALF_PULSE_US); }

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.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-stepper-motor-a4988-driver */ // Arduino Mega 2560 turns a NEMA 17 stepper motor through the A4988 driver // Think of the height axis of a 3D printer: one turn of the lead screw up, one turn down // No library is needed: every step is one pulse made with digitalWrite() // All three step-size switches are OFF (full step) #define STEP_PIN 4 // Arduino Mega pin D4 -> STEP of the A4988 (each rising edge = one step) #define DIR_PIN 3 // Arduino Mega pin D3 -> DIR of the A4988 (selects the direction) #define UP HIGH // level on DIR for the way up #define DOWN LOW // level on DIR for the way down const int MICROSTEPS = 1; // 1, 2, 4, 8 or 16: must match the DIP switches const int STEPS_PER_TURN = 200 * MICROSTEPS; // a 1.8 degree motor has 200 full steps per turn const unsigned int HALF_PULSE_US = 2000 / MICROSTEPS; // STEP is HIGH for this time, then LOW for this time const float MM_PER_TURN = 8.0; // one turn of the lead screw moves the platform by 8 mm const int REST_MS = 1500; // rest between two turns float height = 0.0; // height of the platform in mm, counted from the start position void setup() { Serial.begin(9600); pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); digitalWrite(STEP_PIN, LOW); // STEP waits at LOW between the pulses Serial.print("Height axis ready: "); Serial.print(STEPS_PER_TURN); Serial.print(" steps and "); Serial.print(MM_PER_TURN, 1); Serial.println(" mm per turn"); } // turns the lead screw once, up or down void turnOnce(int direction) { digitalWrite(DIR_PIN, direction); delayMicroseconds(5); // DIR is stable before the first pulse comes for (int i = 0; i < STEPS_PER_TURN; i++) { digitalWrite(STEP_PIN, HIGH); // the motor makes its step on this rising edge delayMicroseconds(HALF_PULSE_US); digitalWrite(STEP_PIN, LOW); // get ready for the next rising edge delayMicroseconds(HALF_PULSE_US); } } // prints the end of the line when a turn is finished void printHeight() { Serial.print("done, platform at "); Serial.print(height, 1); Serial.println(" mm"); } void loop() { // 1. One turn up Serial.print("[UP] one turn ... "); turnOnce(UP); height += MM_PER_TURN; printHeight(); delay(REST_MS); // the motor stands still and holds the platform // 2. One turn down, back to the start Serial.print("[DOWN] one turn ... "); turnOnce(DOWN); height -= MM_PER_TURN; printHeight(); delay(REST_MS); }

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:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Height axis ready: 200 steps and 8.0 mm per turn [UP] one turn ... done, platform at 8.0 mm [DOWN] one turn ... done, platform at 0.0 mm [UP] one turn ... done, platform at 8.0 mm [DOWN] one turn ... done, platform at 0.0 mm [UP] one turn ... done, platform at 8.0 mm
Ln 11, Col 1
Arduino Mega Or... on COM15
2

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.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-stepper-motor-a4988-driver */ // Arduino Mega 2560 + A4988 + NEMA 17: a small platform travels to heights in millimeters // The AccelStepper library (by Mike McCauley) speeds the motor up and slows it down smoothly // Before power-on, put the platform at its lowest point: the sketch calls that place 0 mm #include <AccelStepper.h> #define STEP_PIN 4 // Arduino Mega pin D4 -> STEP of the A4988 #define DIR_PIN 3 // Arduino Mega pin D3 -> DIR of the A4988 const int MICROSTEPS = 1; // 1, 2, 4, 8 or 16: must match the DIP switches const float MM_PER_TURN = 8.0; // lead screw: one turn = 8 mm const float STEPS_PER_MM = 200 * MICROSTEPS / MM_PER_TURN; // full step: 200 / 8 = 25 steps for 1 mm const float TOP_SPEED = 28.0; // mm per second (28 x 25 = 700 steps per second) const float ACCELERATION = 20.0; // mm per second, gained every second (20 x 25 = 500) const float HEIGHTS[] = { 50.0, 20.0, 80.0, 0.0 }; // the stops of the platform, in mm const int NUMBER_OF_HEIGHTS = sizeof(HEIGHTS) / sizeof(HEIGHTS[0]); const unsigned long REST_MS = 2000; // rest at every stop // DRIVER mode: the library makes the STEP pulses and sets the level of DIR AccelStepper lift(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int heightIndex = 0; // which stop is next bool traveling = false; // true while the platform is on its way unsigned long restStart = 0; // when the last rest began void setup() { Serial.begin(9600); lift.setMinPulseWidth(3); // STEP stays HIGH for at least 3 us lift.setMaxSpeed(TOP_SPEED * STEPS_PER_MM); lift.setAcceleration(ACCELERATION * STEPS_PER_MM); lift.setCurrentPosition(0); // here is 0 mm Serial.print("Lift ready at 0.0 mm, "); Serial.print(STEPS_PER_MM, 0); Serial.println(" steps per mm"); restStart = millis(); } // gives the library the next target, the steps themselves are made by run() void startTravel(float mm) { lift.moveTo(lround(mm * STEPS_PER_MM)); // millimeters -> steps Serial.print("To "); Serial.print(mm, 1); Serial.print(" mm: "); Serial.print(lift.distanceToGo() > 0 ? "UP " : "DOWN "); Serial.print(labs(lift.distanceToGo())); Serial.println(" steps"); } void loop() { lift.run(); // makes a step when one is due: call it as often as possible if (traveling && lift.distanceToGo() == 0) { // the platform has arrived traveling = false; restStart = millis(); Serial.print(" arrived, height = "); Serial.print(lift.currentPosition() / STEPS_PER_MM, 1); Serial.println(" mm"); } if (!traveling && millis() - restStart >= REST_MS) { // the rest is over, go to the next stop of the list traveling = true; startTravel(HEIGHTS[heightIndex]); heightIndex = (heightIndex + 1) % NUMBER_OF_HEIGHTS; // after the last stop, start again } }

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.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Library Manager
Type:
All
Topic:
All
AccelStepper by Mike McCauley
Provides an object-oriented interface for 2, 3 or 4 pin stepper motors and motor drivers. More info
1.64
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
Arduino Mega Or... on COM15
1
  • 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:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
Arduino Mega Or...
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Mega Or...' on 'COM15')
New Line
9600 baud
Lift ready at 0.0 mm, 25 steps per mm To 50.0 mm: UP 1250 steps arrived, height = 50.0 mm To 20.0 mm: DOWN 750 steps arrived, height = 20.0 mm To 80.0 mm: UP 1500 steps arrived, height = 80.0 mm To 0.0 mm: DOWN 2000 steps arrived, height = 0.0 mm To 50.0 mm: UP 1250 steps
Ln 11, Col 1
Arduino Mega Or... on COM15
2

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.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!