Stepper Motor Speed Control using Arduino

Hello everyone! I hope you all will be absolutely fine and fun. Today, I am going to tell you that how to make a simple algorithm for Stepper Motor Speed Control using Arduino. I have already discussed with you about DC Motor Direction Control using Arduino, Matlab and NI LabVIEW. Moreover, I have also discussed the DC Motor Speed Control using Arduino,Matlab and LabView. If you are working on Stepper Motor, then you must have a look at Stepper Motor Direction Control using Arduino, Stepper Motor Direction Control using Matlab and Stepper Motor Direction Control using NI LabVIEW. Now, in this tutorial I will explain you about the program which will helpful for Stepper Motor Speed Control using Arduino. Before going into the details of this tutorial you must have go through my previous tutorials because I am using the same hardware. So, they will be a lot helpful for the better understanding of this tutorial. In this tutorial I will explain you about making an Arduino program for Stepper Motor Speed Control using Arduino with the help of the serial communication. If the stepper motor is rotating at its maximum speed and you are continuously sending the command through the serial port to reduce its speed, it s speed will be reduced in proportion to the number of command sent through the serial port. Similarly the same procedure will be followed to increase the speed of the stepper motor.

Stepper Motor Speed Control using Arduino

In the tutorial Stepper Motor Direction Control using Arduino, I will explain you about making an algorithm to run the stepper motor at different speed. If the stepper motor is already running at its maximum speed and you want want to accelerate it further then nothing will happen to the speed of the stepper motor. If the stepper motor is rotating slowly and you enhance its speed, then the speed of the motor will increase in proportion to the number of accelerating command sent through the serial port.
  • You can download the complete Arduino source code here by clicking on the button below.

Download Arduino Code

  • Download .rar file, extract it and enjoy the complete source code.
Flow Chart
  • I have made a flow chart so that you can easily understand the entire algorithm because sometimes it becomes difficult to understand the algorithm with the help of the source code.
  • Flow chart for the Stepper Motor Speed Control using Arduino is shown in the figure below.
  • First of all we need to start the serial port so that our communication could be started.
  • Then there is a method to check the speed, if the speed is greater than the maximum speed of the stepper motor then the program will wait for the next command.
  • If the stepper motor is not rotating with its maximum speed then we can increase its speed.
  • Similarly if the minimum speed of the stepper motor is reached then the program will rotate for the next commands.
  • If the minimum limit of the speed of the stepper motor is not reached then we have a option to reduce its further.
  • At the end we should close the serial port so that exchange of unnecessary commands through the serial port could be avoided.
Block Diagram
  • Block diagram will be helpful for use for the better understanding of the exchange of information.
  • It tells us that how the information is exchanged sequentially among all the components used.
  • Block diagram is shown in the figure below.
  • Arduino UNO communicates with the L298 motor controller to control the speed of the stepper motor.
  • L298 Motor controller manipulates the Arduino's commands and starts to control the speed of the stepper motor.
Arduino Code Description
In this section of the tutorial Stepper Motor Speed Control using Arduino, I am going to elaborate you about the Arduino source.
  • I have made two different functions for increasing (accelerating) the speed of the stepper motor and for decreasing (deaccelerating) the speed of the stepper motor respectively.
  • I have declared a variable named as count.
  • In Accelerate function, you have to send the command through the serial port to increase the speed of the stepper motor.
  • In this function, I am continuously increasing the value of the count i.e as many times you send the command the speed of the stepper motor will increase continuously.
  • The source code of the Accelerate function is given below.
   void Accelerate_Motor()
   { 
    count=count+10; //Speed will increase continuously as we continue to press H
    if (count>120)  //Speed must not be greater than 120
    {
      count=120;
      }
    Serial.println("Accelerating"); //printing on the serial port
    Serial.println("");//prints blank line on the serial port
    myStepper.step(stepsPerRevolution);//counter clockwise rotation
    myStepper.setSpeed(count); //Updating the speed of the motor
    lcd.setCursor(3,0);//setting LCD cursor
    lcd.print("Acelerating"); //printing on LCD
   }
  • In Deaccelerate function, you have to send the command through the serial port to increase the speed of the stepper motor.
  • In this function, I am continuously reducing the value of the count i.e as many times you send the command the speed of the stepper motor will reduce continuously.
  • The source code of the Deaccelerate function is given below.
void Deaccelerate()
{
  count=count-10; //reducing the speed of the motor
  if (count<20) //speed of the motor must not be less than 20
  {
    count=20;
    }
  Serial.println("Deaccelerating"); // prints on the serial port
  Serial.println(""); //prints blank line on the serial port
  myStepper.step(stepsPerRevolution);
  myStepper.setSpeed(count); //Updating the speed of the motor
  lcd.setCursor(3,0);  //setting cursor on LCD
  lcd.print("Deaccelerating"); //prints the command on LCD
  }
  • In the main source inside the loop I am calling both of these Accelerate and Deaccelerate functions.
  • The executed commands will also be printed on the LCD (Liquid Crystal Diode).
  • The main source code is given below.
#include <LiquidCrystal.h>//Library for LCD
#include <Stepper.h>     //Library for Stepper motor

const int stepsPerRevolution = 255;  

// initialize the stepper library on pins
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
char data;
int count = 120;
//LCD pins assigning
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
  // set the speed at 60 rpm
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);// rate at which the arduino communicates

lcd.begin(20, 4);//LCD type

lcd.setCursor(3,0);//setting LCD cursor and printing on it
lcd.print("Stepper Motor");
lcd.setCursor(6,1);
lcd.print("Speed");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");

delay(3000);

lcd.clear ();//Clearing the LCD screen

lcd.setCursor(0,2);
lcd.print("www.TheEngineering");
lcd.setCursor(4,3);
lcd.print("Projects.com");
}

void loop() {
  if(Serial.available())
  {
    data = Serial.read(); //Reading the data from serial port
  }
  
    if(data == 'C'){Clockwise();}      //Clockwise rotation
    if(data == 'A'){AntiClockwise();} //Anti-clockwise rotation
    if(data == 'S')                  //stopping the stepper motor
    {
      data = 0; 
      lcd.setCursor(3,0);
      lcd.print("No rotation");
      Serial.println("No rotation");//print on the serial
      }   
     if(data == 'H'){Accelerate_Motor();}
     if(data == 'L'){Deaccelerate();}
}
Complete Hardware Setup
  • In this section of the tutorial, I will show you the complete hardware setup that I have used for this project.
  • Hardware consists of 12V power supply, Arduino UNO, L298 motor controller.
  • When you upload the code to the Arduino board the system will look like the figure shown below.
  • When you press to increase the speed of the stepper motor, the statement accelerating will be printed on the LCD.
  • The printed executed command is printed on the LCD and is shown in the figure below.
  • When you press to reduce the speed of the stepper motor, the statement Deaccelerating will be printed on the LCD.
  • The printed executed command is printed on the LCD and is shown in the figure below.
That is all from the tutorial Stepper Motor Speed Control using Arduino. I hope you all have enjoyed this tutorial. If you face any sort of problem regarding anything you can ask me anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way if possible. I will explore Arduino by making further projects and I will share them with all of you as well in my later tutorials. So, till then, Take Care :)

Stepper Motor Direction Control using Arduino

Hello friends! I hope you all will be absolutely fine and having fun. Today, I will elaborate you that how can we make a simple algorithm for Stepper Motor Direction Control using Arduino. In my previous tutorials I made algorithm for DC Motor Direction Control using Arduino, DC Motor Direction Control using Matlab, DC Motor Speed Control using Arduino and DC Motor Speed Control using Matlab. Now, in this tutorial I will control a stepper motor using Arduino by entering the different commands through its serial port. Before going into the detail of this tutorial, you must know the basic difference between stepper and DC motors. DC motors have only two input terminal one is positive and the other one is negative. You just have to provide the power supply and it will start rotating but this is not the case in stepper motor. The stepper motor which I will use in this tutorial, has six pins out of which four pins provide pulses or steps and the other two pins are power pins. So, in this tutorial I will control this six pins stepper motor using L298 motor controller and Arduino UNO board. Basically we can use stepper motor where precision is required. Stepper motor has wide range of applications e.g robotics, CNC machines, home automation etc. In simple word, we can say that stepper motor can be used where there is a need to move at particular angle. So, let's get started with Stepper Motor Direction Control using Arduino:

Stepper Motor Direction Control using Arduino

In this tutorial we will learn how to make a program for Stepper Motor Direction Control using Arduino by sending dfferent commands from the serial port. First of all, I am going share the list of components used for this mini project.
  • Arduino UNO
  • Stepper motor (6 wire)
  • L298 Motor Controller (H-Bridge)
  • Voltage Regulator (7805)
  • 1000uF
  • Jumper Wires
  • Solderig Iron
  • Soldering Wire
I want to tell you a bit about the stepper motor because all the other components are discussed in detail in DC Motor Direction Control using Arduino.
Stepper Motor
Basically, stepper motors are like the DC motors that rotate in discrete steps. They have multiple arranged coils and they are usually known as phases. Motor will rotate one step at a time if we energize each phase sequence. High levels of precision can be achieved by controlling the stepper motor with computer. Steppers motors are available in the market in many different sizes. The speed of the stepper motor is controlled by frequency of pulses generated. They have wide range of applications like hard disk drives, robotics, telescope, antenna, toys etc. A six wire stepper motor is shown in the figure below.  
  • You can download complete source code for Stepper Motor Direction Control using Arduino by clicking the below button:

Download Arduino Source Code

Selection of Wires
  • I have used 6 wire stepper motor and each wire has its own function.
  • I have first divided these six wires into two pair.
  • Each pair is consisting of three wires out of which one wire is common and the other two generate pulses.
  • The two pair of three wires are shown in the figure below.

  • Then, I have chosen a common wire in each pair from which the resistance to the other two wires is common.
  • I have checked the resistance from the common wire to the both of the other wires of the same pair.
  • I found that the resistance from the common wire to both of the other wires is same.
  • We can see in the figure above the blue, pink and white wires belong to the same pair out of which white is a common wire.
  • Here is the screen shot of the figure when I found the resistance between white and blue wire and I found it to be 8.0 ohms.
  • The screen shot of the above steps is shown in the figure below.
  • After that. I checked the resistance between white and pink wire and found it to be 8.1 which is almost the same as 8.0 so, this shows that the white wire is common to both of the blue and pink wire.
  • Here is the screen shot of the above step.
  • Then I found the resistance between pink and blue wire and it was 15.6 which is exactly the double of the earlier resistance.
  • It is shown in the figure below.
  • I have connect the both common wires as shown in the figure below.

  • Here's the video in which I have discussed it in detail How to identify the wires of Stepper Motor:
  • The remaining four wires are used to generate pulses which are also know as steps
  • I have connected theses four wires to the output pins OUT1, OUT2, OUT3 and OUT4 of the L298 micro controller.
  • Input pins of L298 micro controller In1, In2, In3 and In4 are connected to the pin no 7, 6, 5 and of the Arduino UNO's board respectively.
Note:

I have also controlled the stepper motor using PIC micro controller so I would suggest all of you to first go through that tutorial before going into the details of this tutorial.

Block Diagram
  • I have made a simple block diagram for Stepper Motor Direction Control using Arduino, which will be helpful to clearly understand the algorithm and the assembling of the components of Stepper Motor Direction Control using Arduino.
  • The screenshot of the block diagram is shown in the figure below.
  • First of all we need a power supply to run the project properly.
  • Arduino reads the commands from the serial port and sends to the L298 motor driver to rotate the stepper motor.
  • The commands got printed on the LCD (Liquid Crystal Display).
Arduino Source Code Description
  • The main function of the Stepper Motor Direction Control using Arduino is given below.
#include <LiquidCrystal.h>//Library for LCD
#include <Stepper.h> //Library for Stepper motor

const int stepsPerRevolution = 255;  

// initialize the stepper library on pins
Stepper myStepper(stepsPerRevolution, 4, 5, 6, 7);
char data;
//LCD pins assigning
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);
void setup() {
  // set the speed at 60 rpm
  myStepper.setSpeed(60);
  // initialize the serial port:
  Serial.begin(9600);

lcd.begin(20, 4);//LCD type

lcd.setCursor(3,0);//setting LCD cursor and printing on it
lcd.print("Stepper Motor");
lcd.setCursor(5,1);
lcd.print("Direction");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");

delay(3000);

lcd.clear ();//Clearing the LCD screen

lcd.setCursor(0,2);
lcd.print("www.TheEngineering");
lcd.setCursor(4,3);
lcd.print("Projects.com");
}

void loop() {
  if(Serial.available())
  {
    data = Serial.read(); //Reading the data from serial port
  }
  
    if(data == 'C'){Clockwise();}//Clockwise rotation
    if(data == 'A'){AntiClockwise();}//Anti-clockwise rotation
    if(data == 'S')//stopping the stepper motor
    {
      data = 0; 
      lcd.setCursor(3,0);
      lcd.print("No rotation");}
    
}
  • In the code given above we have first initialized the LCD and Stepper motor libraries.
  • Then, I assigned stepper motor pins at which it is connected to the Arduino.
  • After that I initialized the LCD pins at which it is connected to Arduino UNO.
  • Then I have made three different if statements, for the clockwise, for the anti clockwise rotation and for the no rotation.
  • Then in the loop I called clock wise and anti clockwise functions whose source code will be give and explained below.
  • Then, I cleared the serial data in order to stop the rotation of the motor.
  • The source code of the clockwise function is given below.
void Clockwise()//function for clockwise rotation
{
    Serial.println("clockwise"); //printing on the serial port
    Serial.println("");//prints blank line on the serial port
    myStepper.step(stepsPerRevolution);//counter clockwise rotation
    lcd.setCursor(3,0);//setting LCD cursor
    lcd.print("Clockwise"); //printing on LCDa
}
  • The source code for the anti clockwise function is given below.
void AntiClockwise()//function for anti clockwise rotation
{
  Serial.println("anti-clockwise");//print on the serial
  Serial.println("");//prints a blank line on the serial
  myStepper.step(-stepsPerRevolution);//clockwise movement
  lcd.setCursor(3,0);//setting LCD cursor
  lcd.print("Anti-clockwise");//printing on LCD
}
  • Now, open your Arduino software, just copy and paste the source code given above.
  • Run the program and open the Serial Port at the top right of the Arduino software.
  • Now, when you enter the command stepper motor will start running in clockwise direction.
  • If you send the command through the serial port stepper motor will start to rotate in counter clockwise direction.
  • If you send the command the rotation of the stepper motor will be stopped.
Actual Hardware Setup
  • The actual hardware operating setup for Stepper Motor Direction Control using Arduino is given in the figure below:
  • Now, if you send the command  through the serial port the stepper motor will start to rotate in clockwise direction and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Now, if you send the command  through the serial port the stepper motor will start to rotate in anti clockwise direction and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Now, if you send the command  through the serial port the stepper motor will show no more rotation and the command will also be printed on the LCD.
  • The screenshot of the printed command on LCD is shown in the figure below.
  • Here's the complete video demonstration of Stepper Motor Direction Control using Arduino, I hope it will help as well:
That's all from the tutorial Stepper Motor Direction Control using Arduino. I hope you enjoyed this tutorial. If you face any sort of problem, you can ask me anytime without feeling any kind of hesitation. I will try my level best to solve your problem in a better way if possible. I will explore Arduino by making different projects on it. Till then, Take care :)

Stepper Motor Drive Circuit in Proteus ISIS

Hello friends, hope you all are healthy, wealthy and wise. Today's topic is about the control of stepper motor. In the last post we have seen How to control DC motor in Proteus, and now we are gonna see How to design a Stepper Motor Drive Circuit in Proteus ISIS. Stepper motors are usually of two types and the main difference between the two is in the number of wires used to control them. Mostly stepper motors use 6 wires to control them but few of them also have 4 wires to control them. Today we will have a look on the 6 wired stepper motor. In stepper motor, there are electromagnets which gets polarized when we supply voltage to them and depolarized when we remove the voltage. These electromagnets act as a stater and when one side get magnetize, it attracts the rotor towards it and then we need to magnetize the other side and demagnetize the previous one and in this ways if the sequence is right the motor starts moving.

Stepper Motor Drive Circuit in Proteus ISIS

  • First of all, add the below two components from the Proteus library in the workspace.
  •  Now design the circuit as shown in the below figure:
  •  This circuit is just for understanding purposes. Now I have added four states in the circuit, when I make any state one that stator got magnetize and the motor rotor will attract towards that stator and start moving.
  • In the below series of images, I have shown the clockwise movement of motor.
  • Now, as you can see from the above sequence of images, the motor is moving in the clockwise direction as I am supplying voltage in the clockwise direction.
  • Now, if I reverse the order of applied voltage the motor will also reverse its direction and will move in the counter clockwise direction.
  • The speed of the motor will depend on the speed of this sequenced voltages. If you apply these voltages with delay, the motor will move slow and if you apply them fast and continuously, the motor will rotate quite fast i.e. rpm of motor will increase.

Stepper Motor Control with Microcontroller

  • Now, in order to control this motor using stepper motor, simply connect these wires with four pins of microcontroller and apply a sequenced voltage in programming and the motor will run quite smoothy.
  • I will upload the video of the stepper motor control with microcontroller soon in this post.
That's it for today and I hope now you got the idea how to design a Stepper motor Drive Circuit in Proteus ISIS. In the next post, we will have a look at How to design a Servo Motor Drive Circuit in Proteus ISIS. So, we will meet in the next post hopefully. Take care.
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir