ESP32 UNO - Stepper Motor A4988 Driver

Rolling cable or 3D-printer filament onto a spool by hand is slow, and around turn eleven you have already lost count. A stepper motor is a good helper here. It moves in small equal steps, and 200 of them make one full turn, so "20 turns" is just a number in your sketch. The DIYables A4988 module sits between the motor and the ESP32 Uno form board: the board sends two small signals, and the module sends the strong current to the motor. At the end of this page you have a winder that starts softly, stops softly and reports how many turns it has wound.

The route from here:

  1. Read the sixteen pins of the A4988 and see what STEP and DIR do
  2. Find the two coils of a NEMA 17 motor
  3. Choose the step size and set the current limit
  4. Connect everything, with the expansion board or with the module alone
  5. Spin the spool without a library, then wind 20 turns with AccelStepper
ESP32 Uno winding cable on a spool with A4988 driver and NEMA 17 motor

Components You Need

1×ESP32 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

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 .

Good to know: the expansion board is optional. The module then needs a few more wires and one extra capacitor, and this page shows that version too. In both versions the 12V adapter feeds only the motor, and the ESP32 Uno runs from USB.

Meet the A4988 Driver

Ten minutes of reading here, and no pin on this little red board will surprise you later.

Its shape is the well-known StepStick: a board the size of a stamp (roughly 20 x 15 mm) on sixteen pins, and the pins come soldered. The A4988 chip in the middle drives one bipolar stepper motor. That is a motor with four wires, or one with six wires when you use only four of them. The small heat sink in the package sticks on top of the chip.

DIYables A4988 module pinout with STEP, DIR, EN, step size pins, coil pins, VMOT, VDD and both GND pins
Item Value
Power for the motor, on VMOT any DC supply from 8V to 35V, 12V in this project
Power for the logic, on VDD 3V to 5.5V, here the 3.3V of the ESP32 Uno
Most current per coil around 1A bare, 2A with the heat sink and a good air flow
Five step sizes from full steps down to 1/16 of a step
Wires for control 2, STEP and DIR
Built-in protection switches off when too hot or when the supply is too low, and prevents crossover current (a short inside its own outputs)
Fault output none, this module has no FAULT pin

The Sixteen Pins

Hold the module so that the pin EN is at the top left. Eight pins run down the left side, eight down the right side.

Pin Side Its Job
EN Left Motor outputs on or off. LOW or no wire = on, HIGH = off and the shaft turns freely
MS1, MS2, MS3 Left Step size. A pin without a wire counts as LOW
RST Left Reset when LOW. Nothing holds this pin, it floats, so join it to SLP
SLP Left Sleep when LOW. The module pulls it HIGH by itself
STEP Left One change from LOW to HIGH = one step
DIR Left Turning direction
VMOT Right Power for the motor
GND, next to VMOT Right Ground of the motor power
2B and 2A Right The two ends of coil 2
1A and 1B Right The two ends of coil 1
VDD Right Power for the logic
GND, next to VDD Right Ground of the logic

The two GND pins are one and the same net, joined on the module. On the top side of the board, the coil pins carry the labels B2, A2, A1 and B1. Turn the module over and you read 2B, 2A, 1A and 1B. Both mean the same pins.

Two Signals Do the Work

Imagine a hand counter with a click button. Every click on STEP moves the shaft by one step, and DIR decides if the clicks go up or down. The "click" is the moment the pin changes from LOW to HIGH. A stepper motor needs its coils switched in a fixed order, and a part of the chip called the translator knows that order by heart. So the sketch never has to think about coils.

The chip watches the coil current as well. It chops the current, which means it switches it off and on very quickly, so the current stays at the limit you set. So do not worry when the label of your motor says 2V to 4V: the chip, not the 12V adapter, decides how much current flows.

Each pulse must be HIGH for at least 1 us (one millionth of a second) and LOW for at least 1 us. An ESP32 can switch a pin faster than that, so both sketches on this page wait a little in each half of the pulse.

VDD Must Be 3.3V on the ESP32 Uno

Some drivers take their logic power from the motor supply. This one does not. With no wire on VDD the motor never moves, even when VMOT has 12V. The second point is the level: seven tenths of VDD (0.7 x VDD) is the border, and a signal has to be higher than that to pass as HIGH.

VDD HIGH Starts at A 3.3V Pulse from the ESP32 Uno
3.3V 2.31V clearly HIGH
5V 3.5V below the line, steps can get lost

So VDD, or the V pin of the expansion board, goes to the 3V3 pin. Never use 5V for it on this board.

What the Expansion Board Adds

The expansion board for DRV8825 / A4988 is a 42 x 42 mm carrier. The module plugs into two 8-pin sockets, and the board brings the pins out in a friendlier way.

  • G V S header: three rows named EN, STEP and DIR. S takes the signal, V leads to VDD and G is ground. V is one shared line for all three rows, and G is another one. Bring a single wire to each of them, in whichever row is easy to reach.
  • VIN and GND screws: the 12V supply goes here (the board is marked 12-24V). A 100uF / 35V capacitor next to the supply is already fitted.
  • White motor plug: a 4-pin XH connector in the order 2B, 2A, 1A, 1B.
  • DIP switch: three small switches for MS1, MS2 and MS3.
  • RST and SLP: joined on the board, nothing for you to do.

A DRV8825 in these sockets works without the V wire. An A4988 does not: the V wire is a must.

WARNING

The module fits in both directions, but only one is right. Look for the small "VIN" arrow on the board: the VMOT pin must end up there. The EN to DIR row is then on the same side as the G V S header. With the module the wrong way round, 12V reaches the wrong pins and may burn it.

The NEMA 17 and Its Two Coils

NEMA 17 is the name of the motor's front plate size. The motor on this page is bipolar: it has two separate coils, and each coil is one pair of wires. Each full step is 1.8°, and 200 of them add up to one turn. On a spool with a 5 cm core, one turn takes up about 15.7 cm of cable, so a single step is less than 1 mm of cable.

NEMA 17 bipolar stepper motor coils with the wire colours black, green, red and blue
Coil Wire You May Also See Colour in the Picture Goes to A4988 Pin
First A A+ black 2B
First C A-, or A with a bar green 2A
Second B B+ red 1A
Second D B-, or B with a bar blue 1B

Wires A and C belong to one coil, wires B and D to the other. Your motor may have other colours, so look at its datasheet first. No datasheet? There are two easy tests:

  • With a multimeter in ohms mode: go through the wires two at a time. A pair that reads a few ohms is a coil.
  • With your fingers only: twist two bare wire ends together and try to turn the shaft. Harder to turn than before? Then you have found a pair.

Good to know: A and C in swapped places do no harm, the spool just winds in the other direction. A wire from each coil on 2B and 2A is worse. Then the motor only hums and trembles, and the spool stays where it is.

Two Settings Before the First Spin

Both settings live on the hardware, not in the code. Do them now, and the first test is calm.

Step Size

MS1, MS2 and MS3 choose how far one pulse moves the shaft. On the expansion board you set them with the DIP switch. A switch that is ON gives HIGH. The order is upside down, so do not trust the numbers: 3 belongs to MS1, 2 to MS2 and 1 to MS3.

MS1 MS2 MS3 DIP Switches ON Step Size Pulses per Turn
LOW LOW LOW none full 200
HIGH LOW LOW 3 1/2 400
LOW HIGH LOW 2 1/4 800
HIGH HIGH LOW 3 and 2 1/8 1600
HIGH HIGH HIGH 3, 2 and 1 1/16 3200

Small steps make the winder quieter and smoother, but every turn needs more pulses. Start with all three switches OFF. When you change them later, change MICROSTEPS in the sketch to the same number (1, 2, 4, 8 or 16).

Current Limit

How much current may each coil take? The answer sits in the tiny trimmer of the module. You do not measure the current itself. You measure a voltage called VREF, and the rule is VREF = 8 x Rs x current. The two small resistors on the DIYables module are printed R100, so Rs is 0.1 ohm and the rule gets short: VREF = 0.8 x current.

Current per Coil VREF with R100
0.4A 0.32V
0.6A 0.48V
0.7A 0.56V
0.9A 0.72V
1A 0.80V
  1. Wire the circuit first (next sections) and plug in only the USB cable. VDD now has power, and the 12V adapter stays out.
  2. Read VREF with a multimeter in DC volts mode. Hold the black probe on a GND pin. The trimmer has a metal top, and the red probe touches it.
  3. Use a small screwdriver and move the trimmer by a hair, then read the voltage again. Keep the tip away from the parts around it.
  4. Begin low, for example at 0.32V. Go up only when the motor slips, and stay at or below the current printed on your motor.

Is your module from another maker? Read its two resistors first. With R050 the rule is VREF = 0.4 x current.

Good to know: a motor that stands still also gets current. For a winder that is useful, because it holds the spool in place. It also means that the motor and the driver become warm while nothing moves.

ESP32 Uno Pinout

Two signal pins are used: D4 and D3. The board has no D labels, so count from the corner where the Uno header begins. RX0, TX0, IO26, IO25 and IO17 sit in the places of D0, D1, D2, D3 and D4. The sketches use the numbers 17 and 25.

ESP32 Uno pinout diagram

Good to know: neither GPIO17 nor GPIO25 takes part in the start of the ESP32. You can leave the driver connected during every upload.

How to Connect

Think of three small jobs: four wires to the ESP32 Uno, two wires to the 12V supply, four wires to the motor. Pick one of the two versions below. The sketches are the same for both.

With the Expansion Board

Plug the A4988 into the sockets, then follow the drawing. GND starts at the GND hole next to D13, and 3V3 comes from the power header. Any other GND hole of the board works too.

The wiring diagram between ESP32 Uno A4988 stepper motor driver

This image is created using Fritzing. Click to enlarge image

※ NOTE THAT:

Finish all the wiring before any power is on, and plug in the 12V adapter last. The four colours between the plug and the motor (brown, yellow, pink, purple) are only drawing colours. Go by the coil table above, not by the colours.

Expansion Board Wire It To
S pin, STEP row D4 (GPIO17)
S pin, DIR row D3 (GPIO25)
V pin, any row 3V3
G pin, any row GND
S pin, EN row no wire, the driver is on
VIN screw DC power jack + (12V adapter)
GND screw DC power jack - (12V adapter)

The four motor wires end in the white plug:

Motor Plug Motor Wire Example Colour
2B A black
2A C green
1A B red
1B D blue

A winding motor makes everything on the table tremble, and a loose jumper wire ruins the turn count. Put a Proto Screw Terminal Block Shield or a Screw Shield V2 on the ESP32 Uno and clamp the four wires in it. You use the same four pins, as the next drawing shows.

The wiring diagram between ESP32 Uno A4988 stepper motor driver Screw Shield

This image is created using Fritzing. Click to enlarge image

WARNING

12V belongs on the VIN screw of the expansion board and nowhere else. The ESP32 Uno also has a hole named VIN: leave it empty. Its GPIO pins are made for 3.3V, and 12V on one of them can destroy the board. The motor plug has a rule too: leave it alone as long as 12V is on. A coil that loses its wire while current flows hits back with a high voltage, and that is how many A4988 chips die.

Without the Expansion Board

The module alone takes the same two signals. What changes: you wire the coils pin by pin, you join RST and SLP yourself, and you add a capacitor.

The wiring diagram between ESP32 Uno A4988 module

This image is created using Fritzing. Click to enlarge image

A4988 Pin Wire It To
STEP D4 (GPIO17)
DIR D3 (GPIO25)
VDD 3V3
GND, next to VDD GND
VMOT DC power jack + (12V adapter)
GND, next to VMOT DC power jack - (12V adapter)
2B motor wire A
2A motor wire C
1A motor wire B
1B motor wire D
RST its neighbour SLP (orange jumper)
EN, MS1, MS2, MS3 no wire

With a screw shield on the ESP32 Uno, the module version looks like this:

The wiring diagram between ESP32 Uno A4988 module Screw Shield

This image is created using Fritzing. Click to enlarge image

Two things are easy to forget here:

  • The short wire from RST to SLP (the orange one in the drawing). RST floats, and without this wire the motor may never move.
  • A 100uF capacitor across VMOT and GND. It is not in the drawing. Choose one for 35V or more, keep it close to the module and put its + leg on VMOT.

Good to know: the ESP32 Uno, the module and the adapter must share one ground. The two GND pins of the module are joined, so the GND wire of the board and the minus of the adapter meet there.

ESP32 Uno Code - One Turn On, One Turn Back

The first sketch proves the wiring and needs no library. It sends 200 pulses, which is one turn, rests 1.5 seconds, changes DIR, and sends 200 pulses the other way. One pulse lasts 4 ms: 2000 us HIGH, then 2000 us LOW. So 200 pulses take 0.8 seconds, and the spool runs at 75 RPM (turns per minute). A motor can start at that speed with no acceleration.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-stepper-motor-a4988-driver */ /* * ESP32 Uno sketch: first spin of a NEMA 17 stepper motor on a DIYables A4988. * The spool makes one turn on, rests, makes one turn back, rests, and so on. * No library is needed: every step is one HIGH/LOW pulse on the STEP pin. */ #define STEP_PIN 17 // D4 (GPIO17) -> STEP, the motor moves when this pin goes HIGH #define DIR_PIN 25 // D3 (GPIO25) -> DIR, HIGH or LOW sets the turning direction #define MICROSTEPS 1 // 1, 2, 4, 8 or 16, the same as the DIP switches #define STEPS_PER_TURN (200 * MICROSTEPS) // a 1.8 degree motor has 200 full steps per turn #define STEP_DELAY (2000 / MICROSTEPS) // us HIGH, then us LOW: 75 turns per minute #define REST_TIME 1500 // ms between two moves // send the given number of pulses, all at the same speed void turnSpool(int direction, int steps) { digitalWrite(DIR_PIN, direction); delayMicroseconds(5); // the direction must be ready before the first pulse for (int i = 0; i < steps; i++) { digitalWrite(STEP_PIN, HIGH); // LOW to HIGH: the motor takes one step delayMicroseconds(STEP_DELAY); digitalWrite(STEP_PIN, LOW); // back to LOW, ready for the next step delayMicroseconds(STEP_DELAY); } } void setup() { Serial.begin(9600); pinMode(STEP_PIN, OUTPUT); pinMode(DIR_PIN, OUTPUT); digitalWrite(STEP_PIN, LOW); Serial.print("A4988 ready, pulses per turn: "); Serial.println(STEPS_PER_TURN); } void loop() { Serial.println("Winding on, 1 turn"); turnSpool(HIGH, STEPS_PER_TURN); delay(REST_TIME); Serial.println("Taking off, 1 turn"); turnSpool(LOW, STEPS_PER_TURN); delay(REST_TIME); }

Upload and Test

  1. Board fresh out of the bag? Prepare the Arduino IDE with ESP32 Uno - Getting Started.
  2. Check the DIP switches: all three OFF, so MICROSTEPS is 1.
  3. Check four wires again: D4 to STEP, D3 to DIR, 3V3 to V (or VDD), GND to G.
  4. Connect the USB Type-C cable. The 12V adapter is still out, and this is the best moment to set VREF.
  5. Choose the board ESP32 Dev Module and the CH340 COM port in Arduino IDE.
  6. Paste the sketch and click Upload.
  7. Stick a piece of tape on the motor shaft as a pointer, then plug in the 12V adapter.
  8. Bring up the Serial Monitor (9600 baud) and tap the reset (EN) button, so that the log begins at 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, pulses per turn: 200 Winding on, 1 turn Taking off, 1 turn Winding on, 1 turn Taking off, 1 turn Winding on, 1 turn
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: watch the tape. After each turn it must point to the same spot as before. If it wanders away, the motor is losing steps.

In full-step mode the motor shakes a little and you can hear it, and that is normal. With 1/4 steps on the DIP switch and MICROSTEPS set to 4, the sketch makes the delay four times shorter. The spool keeps its speed and runs more quietly.

ESP32 Uno Code - Wind 20 Turns, Soft Start and Soft Stop

A spool with filament on it is heavy. When the pulses start at full speed, the motor cannot follow: it hums and loses steps. The second sketch uses the AccelStepper library, which raises the speed bit by bit and lowers it again before the end. One job is 20 turns. After the job, the Serial Monitor reports the turns and the time, and you have six seconds to put on an empty spool.

/* * This ESP32 Uno code was developed by newbiely.com * * This ESP32 Uno code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-uno/esp32-uno-stepper-motor-a4988-driver */ /* * ESP32 Uno sketch: a small spool winder with a NEMA 17 motor on a DIYables A4988. * One job = 20 turns with a soft start and a soft stop. The sketch counts the turns, * reports them, then waits while you put an empty spool on the shaft. * Library: AccelStepper by Mike McCauley */ #include <AccelStepper.h> #define STEP_PIN 17 // D4 (GPIO17) -> STEP #define DIR_PIN 25 // D3 (GPIO25) -> DIR #define MICROSTEPS 1 // 1, 2, 4, 8 or 16, the same as the DIP switches #define STEPS_PER_TURN (200 * MICROSTEPS) // a 1.8 degree motor has 200 full steps per turn #define TURNS_PER_SPOOL 20 // length of one winding job #define REPORT_EVERY 5 // one progress line every 5 turns #define MAX_SPEED 800 // top speed, in steps per second #define ACCELERATION 320 // steps per second gained in each second #define CHANGE_TIME 6000 // ms to change the spool // DRIVER = a driver board with a STEP and a DIR input, such as the A4988 AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN); int spool_number = 0; void windSpool() { int next_report = REPORT_EVERY; unsigned long start_time = millis(); stepper.setCurrentPosition(0); // an empty spool starts at 0 stepper.moveTo(TURNS_PER_SPOOL * STEPS_PER_TURN); // the goal, the motor does not move yet while (stepper.distanceToGo() != 0) { stepper.run(); // sends a step when its time has come if (stepper.currentPosition() >= next_report * STEPS_PER_TURN) { Serial.print(" turn "); Serial.println(next_report); next_report += REPORT_EVERY; } } // the motor has stopped: read the counter and report float turns = (float)stepper.currentPosition() / STEPS_PER_TURN; float seconds = (millis() - start_time) / 1000.0; Serial.print("Spool "); Serial.print(spool_number); Serial.print(" finished: "); Serial.print(turns, 1); Serial.print(" turns in "); Serial.print(seconds, 1); Serial.println(" s"); } void setup() { Serial.begin(9600); stepper.setMinPulseWidth(3); // each pulse stays HIGH for at least 3 us stepper.setMaxSpeed(MAX_SPEED); stepper.setAcceleration(ACCELERATION); Serial.println("Spool winder ready"); } void loop() { spool_number++; Serial.print("Spool "); Serial.print(spool_number); Serial.print(" started, "); Serial.print(TURNS_PER_SPOOL); Serial.println(" turns to wind"); windSpool(); Serial.println("Put on an empty spool"); delay(CHANGE_TIME); // safe here, the motor stands still }

With a top speed of 800 steps per second (4 turns per second) and an acceleration of 320, the job has three parts:

Part of the Job Turns Time What the Spool Does
Soft start 0 to 5 about 2.5 s speeds up from zero to 4 turns per second
Steady run 5 to 15 2.5 s keeps 4 turns per second
Soft stop 15 to 20 about 2.5 s slows down to zero

Upload and Test

  1. Unplug the 12V adapter. The USB cable stays in.
  2. Get the library: click the Libraries button in the left bar.
  3. Search for "AccelStepper". Several results have this word in them, and the right one names Mike McCauley as its author.
  4. Click Install. Go on when the IDE lists it as installed.
  • 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
  1. Paste the second sketch and click Upload.
  2. Plug in the 12V adapter when the upload is done.
  3. Press the reset (EN) button with the Serial Monitor open at 9600 baud:
∞
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
Spool winder ready Spool 1 started, 20 turns to wind turn 5 turn 10 turn 15 turn 20 Spool 1 finished: 20.0 turns in 7.4 s Put on an empty spool Spool 2 started, 20 turns to wind turn 5
Ln 11, Col 1
ESP32 Dev Module on COM15
2
  1. Pro tip: watch the rhythm of the lines. "turn 10" and "turn 15" come 1.25 seconds apart, and "turn 20" takes about twice as long. That is the soft stop at work.

The report counts the pulses that were sent. The A4988 gets no message back from the motor, so a spool that slips still shows 20.0 turns. Put a mark on the spool and count along once, and you know whether your speed is safe.

Video Tutorial

Code Explanation

Code Part What It Does
#define STEP_PIN 17 D4 on the header is GPIO17, and DIR_PIN 25 is D3
STEPS_PER_TURN 200 full steps times MICROSTEPS
STEP_DELAY 2000 us divided by MICROSTEPS, so the spool turns at the same speed with every step size
delayMicroseconds(5) in turnSpool() Gives DIR a moment before the first pulse
DRIVER in the AccelStepper line Tells the library that a STEP and DIR driver does the coil work
setMinPulseWidth(3) Keeps each pulse HIGH for at least 3 us, the A4988 needs 1 us
moveTo() Sets the goal in steps, nothing moves yet
run() Sends one step when it is due, so the while loop calls it all the time
currentPosition() Steps sent since setCurrentPosition(0), divided by 200 it gives the turns
delay(CHANGE_TIME) Allowed here, because the motor has already stopped

Never put a delay() inside the while loop. Each pause there is a missing pulse, and the spool would jerk.

ESP32 Uno vs UNO R4 for This Project

Both boards have the Uno shape, so the same two holes are used. The voltage on V is the trap.

Item Arduino UNO R4 ESP32 Uno
Logic level 5V 3.3V
Wire on V (VDD) 5V 3V3, never 5V
STEP wire D4 D4 (GPIO17)
DIR wire D3 D3 (GPIO25)
Numbers in the code 4 and 3 17 and 25
12V side and motor no change no change

If you copy a sketch from a UNO R4 page, change the two pin numbers first. On the ESP32 chip, GPIO3 is the USB serial line and GPIO4 sits on the A1 hole.

A4988 or DRV8825?

STEP and DIR work in the same way on both, and the expansion board takes either of them.

What You Compare DRV8825 A4988
V wire to 3V3 can stay away must be there
Finest step size 1/32 1/16
Amps per coil, bare about 1.5A about 1A
Amps per coil, cooled 2.2A 2A
Volts on VMOT 8.2V to 45V 8V to 35V

For a small winder with a 12V adapter the A4988 is enough. Choose the DRV8825 when the motor wants more current or a higher voltage.

Is a stepper the right motor at all? When something only has to spin, a DC motor with a BTS7960 driver is simpler. When something only has to swing to an angle, look at the servo motor.

Go Wireless: Order a Spool from Your Phone

WiFi and Bluetooth are already on the ESP32 Uno. A slider on the phone can choose the number of turns, and a text window can show the report of each spool.

windSpool() keeps the board busy until the spool is done. For a phone version, give up the while loop and call stepper.run() once in every pass of loop(), so the app gets its turn as well.

Try These Next

  • Choose the turns by hand: a rotary encoder sets the number, an LCD I2C shows it.
  • Stop button: press a button and call stepper.stop(). The spool slows down gently and does not stop with a jerk.
  • End of cable: let the cable hold down the lever of a limit switch. When the cable runs out, the lever comes up and the job ends.
  • Count real turns: a slotted disc and an IR slotted optical speed sensor tell you if the spool has slipped.
  • Flat layers: a second A4988 on D5 (GPIO16) and D6 (GPIO27) can move a cable guide left and right while the spool turns.

Troubleshooting

Problem Possible Cause Solution
Log is fine, shaft is loose and silent No 12V, or the V (VDD) wire is missing Plug in the adapter and check the wire from V to 3V3
Module alone, motor never moves RST is not joined to SLP Add the short wire between the two pins
Log is fine, motor holds still but does not step Sketch uses 4 and 3, or STEP is in the wrong hole Use 17 and 25 in the code, STEP on D4, DIR on D3
Motor hums or shakes Wires of two coils are mixed Find the pairs again, one pair on 2B and 2A, the other on 1A and 1B
Motor slips when the spool is heavy Current too low, or the start is too quick Raise VREF a little, then try ACCELERATION 160
Spool turns the wrong way The two wires of one coil are swapped Swap 2A with 2B, with the power off
20 turns in the report, 5 turns on the spool DIP switch is on 1/4 but MICROSTEPS is 1 Set both to the same step size
Driver too hot to touch Too much current for a bare chip Turn VREF down, fit the heat sink, leave the box open
Steps get lost at random V is on 5V, or the GND wire is loose Move V to 3V3 and push the GND wire in firmly
AccelStepper.h not found Library not installed Install AccelStepper by Mike McCauley
No COM port in the IDE The PC does not know the CH340 chip, or the cable has no data lines Install the CH340 driver, then try another USB-C cable
Strange signs before the first line The ESP32 reports its own start at 115200 baud No problem, read on from the first clear line

FAQ

Why 3.3V and not 5V on the V pin?

With 5V on VDD, the A4988 takes a signal as HIGH only above 3.5V. The ESP32 Uno sends 3.3V, so it could miss pulses. With 3.3V on VDD the border is 2.31V, and every pulse is clear.

Does the 3V3 pin power the motor?

No. It only feeds the logic of the chip. The motor current comes from the 12V adapter through VMOT.

Can I use a 24V adapter?

Yes. The A4988 accepts 8V to 35V on VMOT, and the expansion board is marked 12-24V. The ESP32 Uno side stays the same, and the current limit still comes from VREF.

Why is the motor warm when the spool stands still?

The driver keeps current in the coils to hold the shaft. To let go, wire EN to a free pin, for example D5 (GPIO16), and set it HIGH. The spool then turns freely, so tie down the cable end first.

How much cable is on the spool after 20 turns?

Multiply the turns by the way around the core. A 5 cm core gives 15.7 cm per turn, so 20 turns in one layer are about 3.1 m. Each new layer is a little longer, because the spool grows.

Can I wind faster?

Raise MAX_SPEED in small steps, for example 800, 900, 1000, and test with your real spool. At some speed the motor begins to slip. Go back to the last good value and leave some reserve.

What changes in the second sketch with microsteps?

Set MICROSTEPS to match the DIP switch. With 1/4 steps the same 800 pulses per second give only one turn per second, so use MAX_SPEED 3200 and ACCELERATION 1280 to keep the old pace.

Can I use other pins for STEP and DIR?

Yes. D2 (GPIO26), D5 (GPIO16), D6 (GPIO27), D9 (GPIO13) and D11 to D13 (GPIO23, 19, 18) work as outputs. Do not take A2 to A5, because they are input only, or D0 and D1, which carry the USB serial. Skip D8 (GPIO12), D10 (GPIO5) and A0 (GPIO2) as well: they are boot strapping pins. D7 (GPIO14) sends out a signal while the ESP32 starts, and the motor could twitch.

Does WiFi disturb the DIR pin on GPIO25?

No. GPIO25 shares a part with WiFi only when you use it as an analog input. Here it is a digital output.

Can one ESP32 Uno drive two steppers?

Yes. Each motor needs an A4988 of its own, two free output pins and its own AccelStepper object in the code. Call run() for both objects all the time.

Learn More

※ 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!