ESP32 - Stepper Motor A4988 Driver

Film a flower with a camera that slowly slides along a rail, and the flower looks like a scene from a movie. Pushed by hand, the camera shakes. Pulled by a motor and a toothed belt, it glides. This project builds the motor part of such a camera slider. The pull comes from a NEMA 17 stepper motor with a belt wheel on its shaft. Each turn of the wheel moves the camera cart 40 mm, always the same 40 mm. The ESP32 is the brain, but its pins cannot feed the coils of a motor. The DIYables A4988 module does that, and it wants only two signals from the ESP32: STEP and DIR. There are two sketches on this page. The first needs no library: plain pulses send the cart 40 mm away and bring it back. The second uses AccelStepper: the cart starts softly, glides along the rail, stops softly, holds still for the camera, and glides home.

ESP32 and A4988 driver pull a camera slider cart with a NEMA 17 motor

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - Stepper Motor A4988 Driver tutorial.

Hardware Used In This Tutorial

1×38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
1×Alternatively, 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
1×Alternatively, ESP32 Uno-form board
1×Alternatively, ESP32 S3 Uno-form board
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (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 Expansion Board for ESP32
1×Recommended: Breakout Expansion Board for ESP32
1×Recommended: Power Splitter for ESP32

Or you can buy the following kits:

1×DIYables ESP32 Starter Kit (ESP32 included)
1×DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links in this section are Amazon affiliate links, meaning we may earn a commission at no additional cost to you if you make a purchase through them. Additionally, some links direct you to products from our own brand, DIYables .

No expansion board at hand? It is optional, and a second wiring diagram shows the A4988 without it. The rail, the cart, the belt and the belt wheel are not in the list, because no two sliders are built alike. You do not need them for a first try: both sketches run with a bare motor on the desk. Fold a bit of tape around the shaft like a flag, and every turn is easy to see.

Introduction to Stepper Motor

A DC motor spins as long as it has power. A stepper motor walks: it makes one small step, then it waits for the next command. The NEMA 17 in this project is a 1.8 degree motor. 360 / 1.8 = 200, so 200 full steps bring the shaft around once. Nobody has to measure where the cart is. The ESP32 just counts the steps it has sent.

Inside the motor are two separate coils, and this is what the word "bipolar" stands for. Each coil has a wire at both of its ends, which makes four wires in total. Six wires are no problem either, as long as you connect only four of them. Five wires mean another kind of motor, and the A4988 is the wrong driver for it: take a look at ESP32 - 28BYJ-48 Stepper Motor ULN2003 Driver.

Coil and wire color chart of a bipolar NEMA 17 stepper motor
Driver pin Motor wire Printed elsewhere as Color on our example motor
2B A A, A+ black
2A C A-, A (bar on top) green
1A B B, B+ red
1B D B-, B (bar on top) blue

Colors are a choice of the maker, not a standard, so look them up in the datasheet of your own motor. If there is no datasheet, test the motor. A multimeter (ohms) on two wires of one coil reads a few ohms. On two wires of different coils, it finds no connection. Or do it without tools, with the motor unplugged: let two bare ends touch while you roll the shaft between your fingers. Does it feel stiff now? Then these two wires are one coil.

What if you get it wrong? A pair that is the wrong way round is harmless: the cart only travels in the opposite direction. One wire of each coil on the same pair of pins is the real mistake. The motor then buzzes or jerks, and the belt stays where it is.

From Steps to Millimeters

The belt of this slider is a GT2 belt, which has one tooth every 2 mm, and the belt wheel (pulley) has 20 teeth. 20 teeth of 2 mm each make 40 mm of belt for one turn. The rest is simple math:

  • 1 full step = 40 / 200 = 0.2 mm on the rail
  • 5 full steps = 1 mm
  • 480 mm of rail = 12 turns = 2400 full steps

Another pulley, another number: count its teeth and multiply by 2.

Introduction to A4988 Stepper Motor Driver

A4988 stepper motor driver pinout: control pins on the left, motor and power pins on the right

This red DIYables board has the StepStick shape, a common shape for small stepper drivers. It measures about 20 x 15 mm, its 2 x 8 male pins are already soldered, and a small heat sink to stick on the chip is part of the package. The A4988 chip runs one bipolar stepper motor.

The coils need current in a certain pattern. Your sketch does not have to know it, because a block of the chip named translator has the pattern stored. Each pulse on STEP moves it to the next place of the pattern, and DIR says if "next" means forward or backward. The chip also guards the current. As soon as a coil reaches the limit, the chip cuts its current for an instant, then lets it flow again, many times per second. This is called chopping. It is also the reason why a 12V adapter does no harm to a motor that is rated for only 2V to 4V.

A4988 Stepper Motor Driver Module
The motor it drives one bipolar stepper motor (4 wires)
The motor voltage (on VMOT) 8V to 35V DC, this page uses 12V
The logic voltage (on VDD) 3V to 5.5V, this page uses 3.3V
The current per coil about 1A with the bare chip, up to 2A with heat sink and good cooling
The step sizes full, 1/2, 1/4, 1/8 and 1/16
The inputs to use STEP and DIR
The protection under-voltage lockout, crossover-current protection, over-temperature shutdown
The fault signal none, the module has no FAULT pin

A4988 Pinout

In the picture above, the left row holds the control pins and the right row holds the power and motor pins.

  • STEP pin: connect this pin to GPIO17. Each rising edge (LOW to HIGH) moves the motor by one step, in the step size you have set. The chip wants to see HIGH for 1 us or more and LOW for 1 us or more, and 1 us is a millionth of a second.
  • DIR pin: connect this pin to GPIO16. Its level picks the direction of the cart.
  • VDD pin: connect this pin to 3V3. The logic part of the chip lives on it.
  • Logic GND pin: connect this pin (the GND next to VDD) to GND of the ESP32.
  • VMOT pin: connect this pin to the + of the 12V supply.
  • Motor GND pin: connect this pin (the GND next to VMOT) to the - of the 12V supply. On the module, it is tied to the other GND pin.
  • 2B, 2A pins: connect these pins to the two ends of coil 2.
  • 1A, 1B pins: connect these pins to the two ends of coil 1.
  • RST, SLP pins: connect these pins to each other. LOW on RST resets the chip, LOW on SLP starts the sleep mode. SLP has a pull-up on the module. RST has none and floats, so it borrows its HIGH from SLP. The expansion board has this link already.
  • MS1, MS2, MS3: these pins set the step size. Without a wire, they count as LOW, which means full steps.
  • EN pin: leave this pin free. The module pulls it LOW, and LOW means "motor outputs on". HIGH switches them off, and then the cart can be pushed by hand.

Do not be surprised by the print on the top of the board: the coil pins are called B2, A2, A1 and B1 there. Turn the board over, and you find 2B, 2A, 1A and 1B for the same pins.

Step Size: Smoother Shots with Microsteps

Full steps are fine for a first test, but a camera can see them as a small shake. Microsteps cut every full step into smaller ones, so the ride gets smoother and quieter.

Step size MS1 MS2 MS3 DIP switch ON Pulses for one turn Cart moves per pulse
full LOW LOW LOW none 200 0.2 mm
1/2 HIGH LOW LOW 3 400 0.1 mm
1/4 LOW HIGH LOW 2 800 0.05 mm
1/8 HIGH HIGH LOW 2 and 3 1600 0.025 mm
1/16 HIGH HIGH HIGH 1, 2 and 3 3200 0.0125 mm

The DIP switch of the expansion board makes the choice, and ON is HIGH. Read its numbers twice, as they run in reverse: MS3 sits on switch 1, MS2 on switch 2 and MS1 on switch 3. Both sketches have a MICROSTEPS line with the number 1, 2, 4, 8 or 16. It must always fit the switches. For the first run, leave every switch OFF and MICROSTEPS at 1.

Current Limit: One Small Trimmer

Too little current, and the cart gets stuck. Too much, and the driver gets hot. The trimmer on the module sets the limit, and a voltage named VREF shows you where it stands. From VREF you get the limit: VREF / (8 x Rs). Rs stands for the two tiny resistors that sense the current. On the DIYables module you can read R100 on both, so Rs is 0.1 ohm. Turned around, the formula is then VREF = 0.8 x current.

Coil current you want Voltage to set (VREF)
0.45A 0.36V
0.65A 0.52V
0.85A 0.68V
1A (no more without the heat sink) 0.80V
  • Do this after the wiring, with the USB cable in and the 12V adapter out. VREF is there as soon as VDD has power.
  • Multimeter on DC volts: the black tip touches GND, the red tip touches the trimmer, whose top is metal.
  • Take a small screwdriver, turn only a little, put it aside and measure again. If the blade slips, it can short parts nearby, so do not hurry.
  • First value: something low such as 0.36V. If the cart slips later, go up a bit. The current on the label of your motor is the maximum.

Not a DIYables module? Look for the print of the resistors. R050 changes the rule to VREF = 0.4 x current.

Between two glides the current does not stop: it holds the cart in place. So it is normal that motor and driver get warm while nothing moves.

Logic Supply: A Wire from 3V3 to VDD

The A4988 has no logic supply of its own. Leave VDD open, and the chip stays off. Which voltage should VDD get? The border for a HIGH signal lies at 0.7 x VDD, and an ESP32 pin gives 3.3V at most:

VDD wired to Border for HIGH Result with the 3.3V pulses of the ESP32
3V3 2.31V every pulse counts
VIN (5V) 3.5V too low, pulses can be missed

So take 3V3 and never VIN. This wire feeds only the logic, the motor gets its power from the 12V adapter.

The Expansion Board

Think of the DIYables expansion board for DRV8825 / A4988 (42 x 42 mm) as a small dock: the A4988 goes into two 8-pin sockets, and all other wires get a comfortable place.

  • G V S header: three rows (EN, STEP, DIR) with three pins each. S is the signal, V is the logic supply on its way to VDD, G is ground. All V pins are the same line, all G pins too, so a single wire for V and a single wire for G do the job, in any row. Do not skip the V wire, an A4988 depends on it.
  • Screw terminal: VIN and GND take the 12V (12-24V is printed there). Next in line is a 100uF / 35V capacitor, already soldered.
  • White XH plug: here comes the motor. From the top, the pins are 2B, 2A, 1A and 1B.
  • DIP switch: step size, as in the table above.
  • No wire at all: the board links RST with SLP, and a free EN row keeps the driver on.

WARNING

The sockets do not stop you from putting the module in backwards, and that can destroy it. Two marks help. The side with EN and DIR goes into the left socket, next to the G V S pins. VMOT belongs to the small "VIN" arrow.

Wiring Diagram

Four wires link the ESP32 to the expansion board, two bring the 12V, and four go to the motor. Look at the zoom in the picture for the pins of the G V S header.

ESP32 A4988 stepper motor driver wiring diagram

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

  • Wire first, power later. The 12V adapter is the very last plug.
  • Motor wires in brown, yellow, pink and purple exist only in the drawing, to tell the lines apart. Use the coil table of this page for your motor.
  • Hands off the motor plug while the 12V is on, for plugging and for unplugging. A coil that is cut off under current makes a voltage spike, and the spike can kill the A4988.
ESP32 Expansion Board
GPIO17 → S pin of the STEP row
GPIO16 → S pin of the DIR row
3V3 → V pin (any row)
GND (any of them) → G pin (any row)
(no wire) S pin of the EN row
12V adapter and motor Expansion Board
DC power jack, plus (+) → VIN screw
DC power jack, minus (-) → GND screw
A (black) → 2B of the motor plug
C (green) → 2A of the motor plug
B (red) → 1A of the motor plug
D (blue) → 1B of the motor plug

Two pins share the name VIN, so be careful. The 12V goes to the VIN screw of the expansion board. The VIN pin of the ESP32 is a 5V pin and stays empty. No pin of the ESP32 may ever touch 12V. The grounds, on the other hand, belong together: the G pin, the GND screw and the GND of the ESP32 form one common ground.

Why GPIO17 and GPIO16? They are neighbors on the ESP32, and their wires reach STEP and DIR without crossing. They do nothing special at power-up either. On ESP32 boards with a WROVER module, GPIO16 and GPIO17 are used by the PSRAM (extra memory). On the WROOM board of this page, they are free. With a WROVER, take two other outputs, for example GPIO18 and GPIO19, and write their numbers in the code. GPIO34, 35, 36 and 39 cannot do this job, as they are inputs only.

Wiring Diagram without the Expansion Board

Every wire now ends on a pin of the module itself. The module has male pins, and so has the ESP32, so jumper wires with a female plug on both ends are the right ones here.

ESP32 A4988 module wiring diagram without the expansion board

This image is created using Fritzing. Click to enlarge image

A4988 Module ESP32 / Other
STEP → GPIO17
DIR → GPIO16
VDD → 3V3
GND next to VDD → GND
VMOT → DC power jack, plus (+)
GND next to VMOT → DC power jack, minus (-)
2B, 2A → motor wires A, C
1A, 1B → motor wires B, D
RST → SLP (the short orange wire)
EN, MS1, MS2, MS3 (left free)

※ NOTE THAT:

  • The orange wire between RST and SLP looks unimportant, but leave it out and the cart may never move.
  • One part is not drawn: a 100uF capacitor for 35V or more. It sits close to the module, with its + leg on VMOT and its - leg on the motor GND. The expansion board has its own.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

How To Program For the A4988 Driver

With the A4988, your code has two jobs: choose a direction, and send the right number of pulses.

  • Name the two pins by their GPIO numbers, and tell the code how many pulses make one turn:
#define PIN_STEP 17 // GPIO17 #define PIN_DIR 16 // GPIO16 #define MICROSTEPS 1 // all DIP switches OFF #define STEPS_PER_TURN (200L * MICROSTEPS) // pulses for one turn
  • One step is one pulse. 2000 us HIGH plus 2000 us LOW is 4 ms, so 250 steps fit into one second. That is 75 turns per minute in full step, slow enough to start at once:
digitalWrite(PIN_STEP, HIGH); // the step happens here delayMicroseconds(2000); digitalWrite(PIN_STEP, LOW); delayMicroseconds(2000);
  • The first sketch packs this into pullBelt(). The sign of the number is the direction:
pullBelt(STEPS_PER_TURN); // one turn, the cart moves out pullBelt(-STEPS_PER_TURN); // one turn, the cart moves back pullBelt(5); // 1 mm of rail in full step
  • The second sketch gives the same two pins to AccelStepper. DRIVER is the mode for chips with STEP and DIR:
AccelStepper slider(AccelStepper::DRIVER, PIN_STEP, PIN_DIR);
  • The library wants a top speed, an acceleration (how fast the speed may change) and a minimum pulse width:
slider.setMaxSpeed(500); // steps per second slider.setAcceleration(250); // steps per second, each second slider.setMinPulseWidth(3); // STEP stays HIGH for 3 us
  • MoveTo() only writes down the target. run() does the walking, one step at most per call, so it must be called again and again:
slider.moveTo(2400); // 480 mm from home slider.run(); // call this in every pass of loop()

As long as AccelStepper moves the motor, keep delay() out of the code. It blocks the pulses, and the cart bumps.

ESP32 Code - Stepper Motor A4988 Driver

Use this sketch as the wiring test. The motor turns the pulley once, waits 1.8 seconds, turns it back, waits again, and so on. A turn lasts 200 x 4 ms = 0.8 seconds.

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-stepper-motor-a4988-driver */ // ESP32 turns a NEMA 17 stepper motor with the A4988 driver. No library is needed. // Picture it as the motor of a small camera slider: // one turn of the belt pulley moves the cart out, one turn back brings it home again. // Every step is one pulse: PIN_STEP goes HIGH, waits, goes LOW, waits. #define PIN_STEP 17 // ESP32 pin GPIO17 connected to the STEP pin of the A4988 #define PIN_DIR 16 // ESP32 pin GPIO16 connected to the DIR pin of the A4988 #define MICROSTEPS 1 // 1, 2, 4, 8 or 16 (the same step size as the DIP switches) #define STEPS_PER_TURN (200L * MICROSTEPS) // a 1.8 degree motor makes 200 full steps per turn #define HALF_PULSE 2000 // microseconds HIGH, and the same time LOW (250 pulses per second) #define REST_TIME 1800 // milliseconds the cart stands still after each turn // Move the belt by a signed number of steps: // steps > 0 -> the cart moves out (DIR HIGH) // steps < 0 -> the cart moves back (DIR LOW) void pullBelt(long steps) { digitalWrite(PIN_DIR, steps > 0 ? HIGH : LOW); delayMicroseconds(10); // DIR is steady before the first pulse comes for (long left = labs(steps); left > 0; left--) { digitalWrite(PIN_STEP, HIGH); // the change from LOW to HIGH is the step delayMicroseconds(HALF_PULSE); digitalWrite(PIN_STEP, LOW); // get ready for the next step delayMicroseconds(HALF_PULSE); } } void setup() { Serial.begin(9600); pinMode(PIN_STEP, OUTPUT); pinMode(PIN_DIR, OUTPUT); digitalWrite(PIN_STEP, LOW); Serial.print("A4988 ready, one pulley turn = "); Serial.print(STEPS_PER_TURN); Serial.println(" pulses = 40 mm of belt"); } void loop() { Serial.println("[out] 1 turn, DIR is HIGH"); pullBelt(STEPS_PER_TURN); delay(REST_TIME); // the coils keep their current, so the cart does not roll away Serial.println("[back] 1 turn, DIR is LOW"); pullBelt(-STEPS_PER_TURN); delay(REST_TIME); }

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Follow the wiring diagram, but leave the 12V adapter unplugged. All DIP switches stay OFF.
  • Take the belt off the pulley for this test, or put the cart in the middle of the rail.
  • Plug the USB Type-C cable into the ESP32 and your PC. VDD is now powered: set VREF as described above.
  • Open Arduino IDE, then choose the right ESP32 board (e.g. ESP32 Dev Module) and COM port.
  • Copy the above code and open with Arduino IDE.
  • Click to the Upload button on Arduino IDE to compile and upload code to the ESP32.
How to upload ESP32 code on Arduino IDE
  • Give the motor its power: plug in the 12V adapter.
  • Open the Serial Monitor and select 9600 baud.
How to open serial monitor on Arduino IDE
  • Press the EN button of the ESP32 to run the sketch from its first line.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
A4988 ready, one pulley turn = 200 pulses = 40 mm of belt [out] 1 turn, DIR is HIGH [back] 1 turn, DIR is LOW [out] 1 turn, DIR is HIGH [back] 1 turn, DIR is LOW [out] 1 turn, DIR is HIGH
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Watch the tape flag. Turn after turn, it stops at the same angle, or steps are lost. Does the cart go the wrong way on [out]? Pull out the 12V adapter, then exchange the two wires of one coil.

Now try something useful for a slider. Put the DIP switches 2 and 3 to ON (1/8 step), set MICROSTEPS to 8, and upload again. The pulses keep their speed, but a turn now needs 1600 of them, so it takes 6.4 seconds. The cart creeps along at about 6 mm per second, and the motor runs more quietly.

ESP32 Code - Camera Slider with a Soft Start and Stop

A cart with a camera on it is too heavy to jump to full speed, and a hard stop makes the picture wobble. AccelStepper builds the speed up and takes it down again. In this sketch, the top speed is 500 steps per second, or 100 mm of rail per second. One glide of 480 mm then looks like this:

Part of the glide On the rail Time
Soft start 0 to 100 mm about 2 s
Steady glide 100 to 380 mm 2.8 s
Soft stop 380 to 480 mm about 2 s
Hold 480 mm 2.5 s
/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-stepper-motor-a4988-driver */ // ESP32 + A4988 + NEMA 17: a camera slider with a soft start and a soft stop // The cart glides to the far end of the rail, the camera holds still for a moment, // then the cart glides back home. Library: AccelStepper by Mike McCauley. // Push the cart to its home end by hand before the 12V is on: that place is 0 mm. #include <AccelStepper.h> #define PIN_STEP 17 // ESP32 pin GPIO17 connected to the STEP pin of the A4988 #define PIN_DIR 16 // ESP32 pin GPIO16 connected to the DIR pin of the A4988 #define MICROSTEPS 1 // 1, 2, 4, 8 or 16 (the same step size as the DIP switches) #define MM_PER_TURN 40 // belt pulled by one turn: 20 teeth x 2 mm (GT2) #define TRAVEL_MM 480 // length of one glide, make it shorter than the free part of your rail #define TOP_SPEED 500 // full steps per second (500 = 100 mm of rail per second) #define SPEED_CHANGE 250 // how much the speed may change in one second, smaller = softer start and stop #define HOLD_TIME 2500 // milliseconds the camera holds still at each end const long STEPS_PER_TURN = 200L * MICROSTEPS; const long HOME_END = 0; const long FAR_END = TRAVEL_MM * STEPS_PER_TURN / MM_PER_TURN; // 2400 steps in full step AccelStepper slider(AccelStepper::DRIVER, PIN_STEP, PIN_DIR); // DRIVER = a chip with STEP and DIR pins bool gliding = false; // true while the cart is on its way unsigned long phaseStart = 0; // when the glide or the hold began // the position of the cart in millimeters long cartMm() { return slider.currentPosition() * MM_PER_TURN / STEPS_PER_TURN; } void setup() { Serial.begin(9600); slider.setMaxSpeed(TOP_SPEED * MICROSTEPS); // never faster than this slider.setAcceleration(SPEED_CHANGE * MICROSTEPS); // how gently the speed changes slider.setMinPulseWidth(3); // STEP stays HIGH for 3 us (the A4988 needs 1 us) slider.setCurrentPosition(HOME_END); // the cart is at home now Serial.print("Camera slider ready at 0 mm, one glide = "); Serial.print(FAR_END); Serial.println(" steps"); phaseStart = millis(); } void loop() { if (slider.run()) // sends a pulse when it is time for one, and is true while the cart moves return; // so call it again at once: no delay(), no printing on the way // from here on the cart stands still if (gliding) { // it has just arrived: report the glide and start the hold float seconds = (millis() - phaseStart) / 1000.0; Serial.print("Hold at "); Serial.print(cartMm()); Serial.print(" mm, the glide took "); Serial.print(seconds, 1); Serial.println(" s"); gliding = false; phaseStart = millis(); } else if (millis() - phaseStart >= HOLD_TIME) { // the hold is over: start the glide to the other end if (slider.currentPosition() == HOME_END) { Serial.println("Glide: home to far end"); slider.moveTo(FAR_END); } else { Serial.println("Glide: far end to home"); slider.moveTo(HOME_END); } gliding = true; phaseStart = millis(); } }

Quick Instructions

  • Unplug the 12V adapter. Without it, the cart can be pushed by hand.
  • Check that the cart can travel 480 mm from its home end. On a shorter rail, make TRAVEL_MM smaller.
  • Click to the Libraries icon on the left bar of the Arduino IDE.
  • Type "AccelStepper" on the search box, then look for the AccelStepper library by Mike McCauley.
  • Click to the Install button and wait until it is done.
  • Search for AccelStepper created by Mike McCauley and click the Install button.
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
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
ESP32 Dev Module on COM15
1
  • Copy the above code and open with Arduino IDE.
  • Click to the Upload button on Arduino IDE.
  • Open the Serial Monitor (9600 baud) now and not later, because opening it can restart the ESP32.
  • Push the cart to its home end. The ESP32 cannot see the cart, so it takes the starting place as 0 mm.
  • Hold down the EN button of the ESP32, plug in the 12V adapter, then let the button go. In this way, the sketch and the motor start together.
  • Compare the Serial Monitor with the cart:
∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32 Dev Module' on 'COM15')
New Line
9600 baud
Camera slider ready at 0 mm, one glide = 2400 steps Glide: home to far end Hold at 480 mm, the glide took 6.7 s Glide: far end to home Hold at 0 mm, the glide took 6.7 s Glide: home to far end
Ln 11, Col 1
ESP32 Dev Module on COM15
2

Stand something light and tall on the cart, for example an empty paper roll, and see if it wobbles at the start and at the end of a glide. Then set SPEED_CHANGE to 100 and watch again: the start takes longer and the roll stands calmer. For a slow film shot, lower TOP_SPEED to 100 (20 mm per second), and add microsteps. The sketch multiplies both numbers by MICROSTEPS for you, so the cart keeps its pace.

The millimeters in the log are counted, not measured. If something blocks the cart, the motor slips, and the ESP32 does not notice.

Line-by-line Code Explanation

The comments in both sketches explain the important lines one by one. Read them together with the code.

Troubleshooting

Roll the shaft gently with your fingers while the 12V is on. Does it turn freely, or is it held? The answer tells you where to search.

  • No movement, shaft free: the A4988 is off. Is the 12V adapter plugged in? Does 3V3 reach the V pin (or VDD)? With the module alone, is the RST to SLP wire there?
  • No movement, shaft held: the A4988 is on, but no pulses arrive. Check that GPIO17 goes to STEP and that the two S wires are not exchanged.
  • Humming or shaking, but no travel: the coil pairs are mixed, VREF is too low, or TOP_SPEED or SPEED_CHANGE is too much for the weight of the cart.
  • Cart travels to the wrong side: power off, then exchange the wires of one coil, only one.
  • Glide too long or too short: MICROSTEPS does not fit the DIP switches, or the pulley does not have 20 teeth. In the second case, correct MM_PER_TURN.
  • Cart stops in the middle of a glide, driver very hot: the chip has protected itself. Set VREF lower, put the heat sink on, and let fresh air reach it.

Swap the A4988 for a DRV8825?

The two modules are close relatives: same shape, same STEP and DIR, and the expansion board accepts both. The DRV8825 is the stronger one: 8.2V to 45V for the motor, about 1.5A per coil (2.2A cooled), and microsteps as small as 1/32. With the A4988, it is 8V to 35V, about 1A (cooled 2A) and 1/16. The wiring differs in one point: a DRV8825 makes its own logic voltage and needs no V wire.

Mind the pins when you change the driver. The ESP32 - Stepper Motor page uses a DRV8825 with STEP on GPIO18 and DIR on GPIO19. To run its sketch on this wiring, change the two numbers to 17 and 16, or move the two wires. More about the chip itself is in ESP32 - DRV8825 Stepper Motor Driver.

What Comes Next

Switches at both ends of the rail give the cart a real home position: ESP32 - Stepper Motor and Limit Switch. A stick can drive the cart by hand: ESP32 - Joystick. And a phone can start a glide or set its speed, over Bluetooth with ESP32 - DIYables Bluetooth App Slider or over WiFi with DIYables ESP32 Web Apps Web Slider. The second sketch is ready for this, because its loop() never waits. Put a short check of a switch or a phone command in front of the slider.run() line.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES