Skip to Content

Learn How To Drive Stepper Motor Using A4988 IC And ESP32

Learn How To Drive Stepper Motor Using A4988 IC And ESP32

Want to learn how to drive stepper motors with an ESP32? 

Stepper motors are used in cameras to focus lenses, 3D printers, CNC machines, and robotic arms. 

A dedicated driver IC to drive the stepper motor helps you to reduce the burden on the ESP32. 

In this article, I will take you through the basics of a stepper motor driver IC, A4988. I will present the step-by-step guide to connecting the ESP32 and A4988 IC. 

I have also provided a working code for ESP32 which you can easily use to test your connections.

The A4988 IC also offers helpful features such as overheating protection, overload protection, mixed and slow current decay modes, multiple-step modes, etc.

By the end of this article, you will also find a few frequently asked questions about the ESP32 and the A4988 stepper motor driver IC.

So, grab your breadboard, and let’s get started.

(I also have an article on How To Drive a Stepper Motor using the A4988 driver and Arduino if you want to work with an Arduino microcontroller instead).

Components Needed To Build ESP32 And A4988 Stepper Motor Project

Hardware Components

Software

Guide

Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.

Basics of The ESP32 And Stepper Motor Driver IC A4988

In this section, we will understand the basics of a Stepper Motor and how a driver IC helps you to simplify motor driving. ESP32 is a powerful microcontroller.

You can bring wireless features to your projects by using the ESP32 controller. 

A Stepper motor converts electrical energy into motion. The Stepper motor can be accurately controlled to set the rotation speed and direction. 

Here is an essential list of specifications of a stepper motor. 

  1. Motor size – You should select the motor based on the load you plan to control. When driving a big CNC machine, you need a tiny servo motor to control a small camera lens but with heavy monster motors. 
  1. Step Count – Number of steps per revolution. The stepper motor runs in steps. This parameter gives you an idea about the number of turns needed to complete a 360-degree rotation. 

The step count is a very important feature. A 1.8-degree motor needs 200 steps to complete a rotation. You will find 48 steps as well as 24 steps motors too. The higher the number of steps, the smoother the movement will be.

  1. Voltage ratings – 12 V and 9V are the standard stepper motor drive voltage specifications. Hence, you always need an external power supply to drive the stepper motor. 
  1. Termination Style – You will find wire leads, surface mount leads and other ways of termination leads.

Unipolar and bipolar stepper motor drivers

In a unipolar configuration, the current flows only in one direction. In the case of bipolar motor drive, the current can change direction through the coils. 

In the case of unipolar motor drives, only half of the copper is powered where, whereas full copper is powered in bipolar drives.

Torque performance is better in bipolar motors. 

Here is an image of a stepper motor driver.

stepper motor driver

The wires represent the ends of the coil. 

To drive the stepper motor, you have to magnetise the coil. You need complex circuitry to control the drives. The easiest solution is to use a dedicated stepper motor driver. 

A4988 is one such IC.

Let’s see the features of the A4988 stepper motor driver IC. 

The A4988 is a complete micro-stepping motor driver with a built-in translator for easy operation. The IC can support up to 2 A of load current. 

A high-level diagram shows a typical connection between Microcontroller and an A4988 driver IC.

connection between Microcontroller and an A4988 driver IC

As you can see, there are a few connections between the A4988 driver IC and the MCU. The MCU can be ESP32 in our example.

What are the MSx pins on the A4988 driver IC?

The driver IC contains three input pins. You can set the pins to control the micro-stepping resolution. 

MS1MS2MS3Microstep resolutionExcitation Mode
LLLFull step2 phase
HLLHalf Step1-2 phase
LHLQuarter StepW1-2 phase
HHLEight Step2W1-2 phase
HHHSixteenth Step4W1-2 phase

The reset pin on the A4988 IC is an active low input. The IC ignores all the settings and other inputs as long as the reset pin is at logic low.

The direction input (DIR) determines the direction of the stepper motor.

The enable pin turns on or off all of the FET outputs. If the pin is set to high, all the FETs are disabled. When set to low, all the FETs are enabled.

The sleep mode pin is used to put the IC in a low-power state when the motor driving feature is not needed. A logic high allows regular operation. A logic low on the pin to sleep mode.

The pin diagram of the A4988 driver IC.

pin diagram of the A4988 driver IC

The pin description of the A4988 pins is available in the table below. 

NameDescription
CP1, CP2Charge pump capacitor terminal
VCPReservoir capacitor terminal
VREGRegulator decoupling terminal
MS1, MS2, MS3Logic input (micro stepping)
RESETReset input
ROSCTiming set
SLEEPSleep mode control
VDDLogic supply
STEPLogic input
REFReference voltage input
GNDGround
DIRLogic input
OUT1BDMOS full bridge 1 output B
VBB1Load supply
SENSE1Sense resistor terminal for bridge 1
OUT1ADMOS Full bridge 1 output A
OUT2ADMOS Full bridge 2 output A
SENSE2Sense resistor terminal for bridge 2
VBB2Load supply
OUT2BDMOS Full bridge 2 output B
ENABLEEnable pin

Here is a common A4988 driver IC module here. It is easy to mount the module on the breadboard. 

The IC in the centre is the A4988 driver IC.

A4988 driver IC

There is a potentiometer that is set to control the current limit. It means you can preset the current level to select the limit.

potentiometer that is set to control the current limit

The driver module accepts a voltage supply in the range of 8 V to 35 V. It accepts the logic inputs in the voltage range of 3 V to 5.5 V. You can find all the pin details of the module here.

driver module accepts a voltage supply

Please refer to the datasheet below for more information on the A4988 driver capabilities.

A4988 driver capabilities

The module accepts supplies from pins VDD and GND. You can connect the motor between the VMOT and GND pins of the module.

An optional heatsink will help the driver IC to perform to its total ability.

optional heatsink

You will see a pinout of an ESP32 module in the below image:

We only need a few GPIOs on the ESP32 to control the stepper motor. Thanks to the A4988 stepper motor driver.

The below table summarizes the connections needed to A4988 stepper motor driver IC and ESP32.

A4988 Driver ICESP32Remarks
DIRGPIO12Controls the direction of the motor
STEPGPIO14Controls the movement of the stepper motor
MS1, MS2, MS3No connections
RSTNo connections
SLPNo connections
VCCVINSupply for the A4988 driver IC
GNDGNDGround connections

Stepper motors are commonly used in various applications that require precise motion control. Here are some examples:

  • 3D printers and CNC machines: Stepper motors are widely used in 3D printers and CNC machines for precise movement of the printer head or cutting tool.
  • Robotics: To position the robot arm and other parts precisely.
  • Camera and Telescope Mounts: To allow for precise tracking of celestial objects.
  • Medical Equipment: You can find stepper motors in imaging systems, precision pumps, and robotic surgery systems.
  • Industrial Automation: Some examples are conveyor systems, assembly lines, and packaging machines.
  • Automotive Applications: Stepper motors are used in automotive applications such as engine management systems, power window motors, and electric power steering systems.
  • Consumer Electronics: You can find stepper motors in consumer electronics such as disk drives, printers, and scanners.
  • Textile and Sewing Machines: Stepper motors are used in textile and sewing machines to control the sewing needle and fabric movement precisely.

Overall, stepper motors are ideal for applications that require precise positioning, speed control, and low maintenance.

The following section will connect the A4988 stepper motor driver IC to the ESP32. Let’s go!

Step-By-Step Instructions To Connect The A4988 Driver Module with ESP32

In this section, we will build a project using ESP32 and A4988 stepper motor driver IC.

Since we use the GPIOs on ESP32, you can complete the connections with fewer wires.

Let’s get started with the hardware connections!

How To Connect The A4988 Driver IC Module To ESP32?

Below is the step-by-step connection guide to complete the ESP32 and A4988 Stepper motor driver IC.

Step 1: Start with the GND connections

Start with the GND connections

Connect the GND pin of the ESP32 module to the GND of the A4988 driver module.

Choose any GND pins available on the ESP32 for the connection.

It is an excellent practice to start with the GND connections.

Step 2: Connect the logic supply for the A4988 driver IC

Connect the logic supply for the A4988 driver IC

Connect the VIN of the ESP32 to the A4988 driver IC’s supply pin. You can connect anything between 3 V and 5.5 V.

Step 3: Connect the DIR line

Connect the DIR line

Connect the GPIO12 of the ESP32 to the DIR (direction control) pin of the A4988 Module. 

Step 4: Connect the STEP control pin

Connect the STEP control pin

Connect the GPIO14 pin of ESP32 to the STEP control input of the A4988 driver IC.

Step 5: Connect the Stepper motor

Connect the Stepper motor

Connect the 4 pins of the stepper motor driver to the stepper motor.

Step 6: Connect the power supply ( 9 V )

Connect the power supply ( 9 V )

Connect the power supply to the A4988 driver module.

Step 7: Complete connection

Complete connection

Here is the complete connection. Congratulations. You have now completed all the needed connections.

In the next section, you will program the ESP32 module. 

ESP32 Code Example For The A4988 Driver Project

In this section, you can find the complete ESP32 code to test the stepper motor project.

Please follow the instructions in the guide to install the ESP32 core on Arduino UNO.

const int DIR = 12;
const int STEP = 14;
const int  steps_per_rev = 200;

void setup()
{
  Serial.begin(115200);
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);
}

void loop()
{
  digitalWrite(DIR, HIGH);
  Serial.println("Spinning Clockwise...");


  for (int i = 0; i < steps_per_rev; i++)
  {
    digitalWrite(STEP, HIGH);
    delayMicroseconds(2000);
    digitalWrite(STEP, LOW);
    delayMicroseconds(2000);
  }
  delay(1000);

  digitalWrite(DIR, LOW);
  Serial.println("Spinning Anti-Clockwise...");

  for (int i = 0; i < steps_per_rev; i++)
  {
    digitalWrite(STEP, HIGH);
    delayMicroseconds(1000);
    digitalWrite(STEP, LOW);
    delayMicroseconds(1000);
  }
  delay(1000);
}

Let’s walk through the code.

const int DIR = 12;
const int STEP = 14;
const int  steps_per_rev = 200;

Define the two pins connected to the driver board’s DIR and STEP pins, respectively. These pins control the direction and step of the motor.

Sets the number of steps required to complete one full motor revolution.

  Serial.begin(115200);
  pinMode(STEP, OUTPUT);
  pinMode(DIR, OUTPUT);

Sets the data rate in bits per second for serial data transmission to the computer. This allows the code to communicate with the Serial Monitor in the Arduino IDE.

Set the DIR and STEP pins as output pins.

The loop() function starts with the motor spinning clockwise.

  digitalWrite(DIR, HIGH);

Sets the DIR pin to HIGH, indicating that the motor should spin in the clockwise direction.

  Serial.println("Spinning Clockwise...");

Prints a message to the Serial Monitor indicating that the motor is spinning clockwise.

The for loop then runs 200 steps (defined by steps_per_rev) to complete one full revolution of the motor.

    digitalWrite(STEP, HIGH);

Sends a pulse to the STEP pin, telling the driver board to move the motor one step.

    delayMicroseconds(2000);

Wait for two milliseconds before sending another pulse to the STEP pin, controlling the speed of the motor. After one complete revolution, the motor stops spinning for 1 second, indicated by

  delay(1000);

The loop() function then sets the DIR pin to LOW, indicating that the motor should spin in the anti-clockwise direction.

  Serial.println("Spinning Anti-Clockwise...");

Prints a message to the Serial Monitor indicating that the motor is spinning in the opposite direction.

Another loop runs 200 steps in the opposite direction, which makes the motor spin at a slower speed than before.

After one complete revolution in the opposite direction, the motor stops spinning for 1 second again.

Please continue building your ESP32 project further to utilize the stepper motor feature.

FAQs About The ESP32 And A4988 Stepper Motor Driver Projects

I have included a list of the most frequently asked questions about projects built using ESP32 and the A4988 stepper motor driver module.

If you have more questions, please post them in the comments section. I will be glad to answer them.

1. What is an A4988 stepper motor driver?

The A4988 is a stepper motor driver that can control stepper motors. It is a popular choice for controlling stepper motors in many DIY and commercial projects.

It takes care of all the complex driving (micro-stepping, for example) and reduces the burden on the ESP32. 

2. What is an ESP32?

The ESP32 is a powerful microcontroller developed by Espressif Systems. It features Wi-Fi and Bluetooth connectivity, making it a popular choice for Internet of Things (IoT) projects.

You can control ESP32 remotely using Wi-Fi and Bluetooth technologies. 

3. Can the A4988 stepper motor driver be used with the ESP32?

Yes, the A4988 stepper motor driver can be used with the ESP32. The ESP32 can communicate with the A4988 using its digital output pins to control the stepper motor.

You can use any GPIO pins on ESP32 to control the A4988 stepper motor driver IC.

4. How do I connect the A4988 stepper motor driver to the ESP32?

You can connect the A4988 stepper motor driver to the ESP32 using four digital output pins. Two of these pins will control the direction of the stepper motor, while the other two will control the step pulses.

Additional controls which you can control are MSx pins (micro stepping settings), Enable, Reset and sleep mode controls. 

5. What voltage should I use to power the A4988 stepper motor driver?

The A4988 stepper motor driver can be powered using a voltage between 8V and 35V. It is important to use a voltage within this range to avoid damaging the driver.

6. Can the A4988 stepper motor driver be used to control multiple stepper motors?

No. One A4988 stepper motor driver can control one stepper motor. To control multiple stepper motors, you need to use multiple A4988 stepper motor drivers.

7. How can I control the speed of a stepper motor using the A4988 stepper motor driver and ESP32?

You can control the speed of a stepper motor by adjusting the frequency of the step pulses sent to the A4988 driver. You can do this by changing the delay time between each step pulse in your code.

8. How can I control the speed of a stepper motor using the A4988 stepper motor driver and ESP32?

The maximum current that the A4988 stepper motor driver can supply to a stepper motor depends on the configuration of the driver.

It can typically supply up to 2A of current to a stepper motor.

Conclusion

Driving a stepper motor using A4988 and ESp32 is now easier. In this article, I have covered everything you need to confidently start using a stepper motor in your next project. 

I hope you followed the step-by-step guide and completed the connections needed to drive the stepper motor. 

I have provided a step-by-step guide, working ESP32 A4988 basic code, and FAQs about the project.

Have a go now to build your own project, program your ESP32, and test it out.

Did you find any section challenging to understand? Are there any points you would like to read further?

Leave a comment here below to let me know