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.

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
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) |
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.

| 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

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.

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.

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:
- 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:
- The first sketch packs this into pullBelt(). The sign of the number is the direction:
- The second sketch gives the same two pins to AccelStepper. DRIVER is the mode for chips with STEP and DIR:
- The library wants a top speed, an acceleration (how fast the speed may change) and a minimum pulse width:
- MoveTo() only writes down the target. run() does the walking, one step at most per call, so it must be called again and again:
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.
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.

- Give the motor its power: plug in the 12V adapter.
- Open the Serial Monitor and select 9600 baud.

- Press the EN button of the ESP32 to run the sketch from its first line.
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 |
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.
- 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:
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.