Project tutorial

# Large Stepper Motor Control A4988 © Apache-2.0

Ultimate getting started guide to easily understand how stepper motor work and how to properly control using an Arduino board

• 27,757 views
• 17 respects

## Components and supplies

 Arduino Nano R3 Control unit of our project
×1
 SparkFun Stepper motor driver board A4988 The stepper motor that we have used in our tutorial
×1
 Digilent Stepper Motor
×1
 Theremino Driver DRV8825 for Stepper Motors for Theremino System Optional Driver to use instead of A4988
×1
×1

## Apps and online services

 JLCPCB EasyEDA
 Arduino IDE

Hey guys! I Hope you already enjoyed my previous tutorial "How to Make a Humanoid Robot". This is an informative tutorial to teach you the basics of any stepper motor control, I already posted a video about controlling speed and direction of DC motors and today we will get started with the stepper motors and hopefully in the next tutorial I will explain how to play a bit with some servomotors.

You can watch this video if you don't want to read the whole post :

During the making of this post, we tried to make sure that this tutorial will be the best guide for you in order to enjoy learning the basics of stepper motors controlling since this is so important especially for those who want to start electronics and robotics learning. so we hope that this tutorial contains the needed documents. What you will learn from this tutorial:

• Define the stepper motors Uses and Needs.
• Understand the Stepper motor mechanism.
• Learn the importance of the stepper motor driver A4988.
• Make the appropriate wiring diagram with an Arduino board.
• Write your first stepper control test.
• Start testing and debugging

### Step 1: What Are the "stepper Motors"!

A stepper motor, also known as step motor or stepping motor belongs to the brushless DC electric motors with a specificity of dividing a full rotation into a number of equal steps or we better call them equal angles, the motor’s axis position can be controlled to move and hold at these angle without having a feedback sensor, and also due to its high torque capacity this kind of motors could maintain stable the motor axis on hold when it carries a heavy charge.

Talking about the two phase’s stepper motor category we have :

### The bipolar motors

Comes with 4 wires to control the coils power polarity

### Unipolar motors

That comes with 5 or 6 wires for the control.

### Step 2: How the Stepper Motor Works

The coils are used to control the steps performed by the motor following some polarity sequences.

A typical driving pattern for a two coils bipolar stepper would be: A+ B+ A− B−

We can define the coils patterns using a millimeter by measuring the resistance between the stepper connection wires, and it should measure some resistance between each two coils ends, please check the photos above.

In order to make the stepper moves, a sequence of power polarity should be sent from a controller through the stepper driver and every time the sequence change the stepper perform a step in a precise direction with a constant speed which is the sequences speed so the faster the sequences move the faster the stepper rotate.

### Step 3: The Stepper Driver A4988

Backing to the control driver, basically some transistors to control the current flow through the stepper coils.

In our tutorial we will use the A4988 driver which allows us to easily control direction and speed of the stepper and could provide up to 35V 2Amps output control for the motor, the driver will be connected to an Arduino Nano board which is the heart of our control part.

### Step 4: The Circuit Diagram

Moving the circuit diagram I used EasyEDA platform to create the above schematic (you can have it from the download link PDF Format), EasyEDA will help you to establish your circuit diagram easilly and then convert it in PCB due to its builtin features you can then place and order for your PCB from JLCPCB. by the way guys I invite you to join me in the new community HUB released by JLCPCB for makers in order to more engage the community.

★☆★ JOIN ME ON JLCPCB COMMUNITY HUB ★☆★Invitation link : https://www.hackster.io/jlcpcb/members/invitations?token=qUi9luLsdgqoeFgWov_ZAw

Backing to our circuit diagram as it shows the photo above we have the Arduino board connected to the stepper motor through the A4988 driver.

About the driver we will need three pins to connect to Arduino which are the Enable, the step and the direction pins.

About the driver output you will connect each pin the appropriate coil end. One important thing is connecting the sleep and reset pins to each other, otherwise the motor will be trembling and won’t move.

### Step 5: Codes and Tests

About the control program, no libraries required for this task so just some pinmode() and giditalWrite() functions

First we set the pins and we configure them as OUTPUTs and we set the initial value of the Enable output to LOW to activate the stepper motor driver.

``const int EN=2;         //ENABLE PINconst int Step=3;       // STEP PINconst int dir=4;        // DIRECTION PIN/*----------------------------SETUP FUNCTION--------------------------*/void setup(){pinMode(EN,OUTPUT);     // ENABLE AS OUTPUTpinMode(dir,OUTPUT);    // DIRECTION AS OUTPUTpinMode(Step,OUTPUT);   // STEP AS OUTPUTdigitalWrite(EN,LOW);   // SET ENABLE TO LOW}/*----------------------------LOOP FUNCTION--------------------------*/void loop(){digitalWrite(dir,LOW);        // SET DIRECTION LOW FOR FORWARD ROTATIONfor(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN{digitalWrite(Step,HIGH);    // STEP HIGHdelay(1);                   // WAITdigitalWrite(Step,LOW);     // STEP LOWdelay(1);                   // WAIT}delay(10);                    // DELAY BEFOR SWITCH DIRECTIONdigitalWrite(dir,HIGH);       // SET DIRECTION HIGH FOR BACKWARD ROTATIONfor(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN{digitalWrite(Step,HIGH);    // STEP HIGHdelay(1);                   // WAITdigitalWrite(Step,LOW);     // STEP LOWdelay(1);                   // WAIT}delay(10);                    // DELAY BEFOR SWITCH DIRECTION}``

About the direction pin once we set it to LOW the stepper will move forward and for HIGH the stepper will spin on the backward direction

Now every pulse on the direction pin will push the motor to make a step and in this code our motor will perform 1000 steps forward then it will spin 1000 steps backward.

Do not get confused on steps and full rotation because a step on our motor is a 1.6 degree angle and a full rotation is 360 degrees angle so our stepper will almost spin for 5 rotations in each direction.

After preparing the circuit as it shows the above photo all what it need now is uploading the code, then we plugin the external power supply for the stepper and it should be applied on Vmot input pin.

Do not forget to connect the reset pin and the sleep pin to each other.

Following this tutorial you are now able to control and test your stepper motors coming from electronic devices like 3D printers.

Let me know through the comment section what other tutorials do you want me to make for you, do not forget to thumb this tutorial if you like it and to follow us for more awesome projects.

One last thing, make sure that you are doing electronics every day.
It was BEE MB from MEGA DAS see you next time.

## Code

##### Code snippet #1Plain text
```const int EN=2;         //ENABLE PIN
const int Step=3;       // STEP PIN
const int dir=4;        // DIRECTION PIN
/*----------------------------SETUP FUNCTION--------------------------*/
void setup()
{
pinMode(EN,OUTPUT);     // ENABLE AS OUTPUT
pinMode(dir,OUTPUT);    // DIRECTION AS OUTPUT
pinMode(Step,OUTPUT);   // STEP AS OUTPUT
digitalWrite(EN,LOW);   // SET ENABLE TO LOW
}

/*----------------------------LOOP FUNCTION--------------------------*/
void loop()
{
digitalWrite(dir,LOW);        // SET DIRECTION LOW FOR FORWARD ROTATION
for(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN
{
digitalWrite(Step,HIGH);    // STEP HIGH
delay(1);                   // WAIT
digitalWrite(Step,LOW);     // STEP LOW
delay(1);                   // WAIT
}
delay(10);                    // DELAY BEFOR SWITCH DIRECTION
digitalWrite(dir,HIGH);       // SET DIRECTION HIGH FOR BACKWARD ROTATION
for(int x = 0; x < 1000; x++) // LOOP 1000 TIMES FOR 1000 RISING EDGE ON STEP PIN
{
digitalWrite(Step,HIGH);    // STEP HIGH
delay(1);                   // WAIT
digitalWrite(Step,LOW);     // STEP LOW
delay(1);                   // WAIT
}
delay(10);                    // DELAY BEFOR SWITCH DIRECTION
}
```

## Schematics

#### Team members

##### DIY GUY Chris
• 20 projects
• 270 followers
• 3 projects
• 4 followers
• 24 projects
• 23 followers

May 31, 2020

#### Members who respect this project

and 10 others

See similar projects
you might like

• 19,661 views
• 37 respects

• 27,806 views
• 52 respects

#### Control The Stepper Motor

Project tutorial by Haoming Weng

• 14,855 views
• 1 comment
• 27 respects

• 38,201 views
• 41 respects

#### Arduino Nano: Control 2 Stepper Motors With Joystick

Project tutorial by Boian Mitov

• 29,773 views