DC Motor Speed Control in LabVIEW

Hello everyone! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge with all of you guys, about how to make a simple Virtual Instrument (VI) for DC Motor Speed Control in LabVIEW. In my previous tutorials, I have worked on DC Motor Speed Control using Arduino, in which I have controlled the DC Motor Speed using Arduino and after that I made some advancements in it and then created DC Motor Speed Control in Matlab in which commands were sent from Matlab. So, now we are gonna do the DC Motor Speed Control in LabVIEW.If you are working on DC Motor then you should also have a look at DC Motor Direction Control using Arduino. In this tutorial, I am going to work on the program for DC Motor speed Control using NI LabVIEW. So, before going into the detail of this tutorial, you must go through my previous tutorials because I am going to use the same hardware setup and Arduino source code with a bit modification. I will made a simple GUI (Graphical User Interface) for DC Motor Speed Control in LabVIEW. There will be five different buttons on the GUI for clockwise rotation, counter clockwise rotation, stopping  the stepper motor, accelerating and deaccelerating the stepper motor respectively.

DC Motor Speed Control in LabVIEW

In the tutorial DC Motor Speed Control in LabVIEW, I will explain you a complete step by step procedure to control the clockwise and counter clockwise direction of the DC motor as well as accelerating and deacelerating it with the help of the buttons on the GUI created in NI LabVIEW using serial communication between Arduino and NI LabVIEW.
  • You can download the complete simulation for DC Motor Speed Control in LabVIEW here:
Download LabView Simulation

  • Download .rar file, extract the files from the folder and enjoy the complete DC Motor Speed Control in LabVIEW simulation.
Flow Chart
  • First of all I would like you to explain the algorithm of DC Motor Speed Control in LabVIEW with the help of a flow chart.
  • Flow chart for this project is shown in the figure below.
  • The figure given above can help you to understand the entire algorithm for DC Motor Speed Control in LabVIEW.
Block Diagram
  • You can see the assembling of all the components used in DC Motor Speed Control in LabVIEW, in the block diagram.
  • Block diagram for this particular tutorial is shown in the figure below.
  • Initially commands are exchanged from NI LabVIEW to the Arduino using serial communication.
  • Then Arduino transfers these commands to L98 motor controller, hence in this way the DC motor.
  • LCD displays the executed commands.
Vitual Instrument's (VI's) Description
  • First of all open NI LabVIEW software on your laptop or PC.
  • Go to the Block Diagram window and Right Click on it.
  • Go to Functions-> Instrument I/O-> Serial and you can see different serial blocks like VISA Write, VISA Read, VISA Serial etc.
  • Choose the encircled VISA Configure Serial Port and place it on the Block Diagram window.
  • VISA Configure Serial Port block will help us to open the Serial Port before executing the algorithm.
  • The screen shot of the Block Diagram is shown in the figure below.
  • Go to the first input terminal of the VISA Configure Serial Port block and go to Create-> Control.
  • Above step will be helpful to select the COM port of the Arduino board in order to run the program properly.
  • Updated Block Diagram window is shown in the figure below.
  • Now go to Functions-> Instrument I/O-> Serial, you can see there different serial blocks.
  • Choose the encircled VISA Close block and place it on the Block Diagram window.
  • The VISA Close block is shown in the figure below and it will be help in closing the Serial Port if needed.
  • Now, go to the Functions-> Programming-> Structures and you can see the different structures there like For Loop, While Loop, Case Structure etc.
  • Choose the encircled block as shown i the figure below.
  • Place all the above blocks in a way shown in the figure below.
  • Now, go to the Functions-> Programming-> Structures-> Flat Sequence.
  • Flat sequence block is encircled and is shown in the figure below.
  • Put your cursor and go to Add Frame After.
  • Similarly ad another case after this as shown in the figures below.
  • Newly added frame is shown in the figure below.
  • Now, go to Functions-> Instrument I/O-> Serial, you can see different serial blocks there.
  • Choose the encircled VISA Write Block and place it on the Block diagram window.
  • The figure shown below elaborates the above steps.
  • Make the connections as shown in the figure below.
  • Now, go the Functions-> Programming-> Structures and you can see different types of structures like for loop, while loop, flat sequence etc.
  • Choose he encircled block as shown in the figure below.
  • Select the Case Structure block and place it on the block diagram window.
  • The figure shown below displays the above step.
  • Now, go to the input terminal of the write block and go to Create-> Control.
  • Change the name of this  block to Command box as shown in the figure below.
  • The block diagram window is shown in the figure below.
  • Now, go to Functions-> Programming-> Structures and you can see different structures blocks there.
  • Choose the encircled block as shown in the figure below.
  • Select the Local Variable Block and place it on the Front Panel.
  • Right click on it and select Command box as shown in the figure below.
  • Go to the input terminal of this local variable and go to Create-> Constant.
  • Place inside that constant.
  • The figure below elaborates the above step.
  • The above case structure is for the clock wise rotation of the stepper motor.
  • Similarly make four further case structures for counter clockwise rotation, accelerating, de-accelerating and stopping the rotation of the stepper motor.
  • All the three case structures are shown in the figure below.
  • You can see three different case structures in above figure.
  • The command box variable having command will rotate the stepper motor in clockwise direction.
  • A command box variable having command will rotate the stepper motor in counter clockwise direction.
  • The command box variable having command will rotate the stepper motor at higher and higher speed if it is rotating with a lower speed.
  • A command box variable having command will rotate the stepper motor with slower and slower speed if it is rotating at a higher speed.
  • .
  • The command box variable having command will stop the rotation of the stepper motor.
  • Now, go to the Front Panel and Right Click on it.
  • Go to Controls-> Modern-> Boolean and you can see there different Boolean blocks.
  • Choose the encircled block as shown in the figure below.
  • Select the Round LED block and place it on the front panel.
  • Similarly select two more round LED blocks and place them on the front panel as well.
  • Change their names from default to Clockwise, Anti clockwise,Stop Motor, Accelerate and Deaccelerate.
  • All of the above steps are explained visually in the figure shown below.
  • The LED shown in the above figure will control the stepper motor on clock wise, counter clock wise direction and will stop the motor as well.
  • Now go to the block diagram window and connect these blocks as shown in the figure below.
  • At the end, after sending all the commands we must need to close the serial port so that unnecessary exchange of commands could be avoided.
  • So I have cleared the all the commands in third frame of the case structure i.e I am sending no commands through the serial port.
  • This will be helpful in closing the serial port.
  • The figure show below explains all of the above steps visually.
  • Now add another case structure to start the program when you want so.
  • The figure below shows the newly added case structure.
  • Now, go to the Front Panel, the button encircled in the figure shown below is used to start the program when needed.
  • Now add a Stop button in order to terminate the program whenever you want so.
  • The complete output of the program is shown in the figure below.
  • A complete NI LabVIEW Virtual Instrument (VI) is shown in the figure below.
  • I have decorated the front panel to make it attractive for the users.
  • The final look of the Front panel is shown in the figure below.
  • Go to Controls, Modern-> Decorations you can see different decoration blocks there.
  • All these blocks are shown in the figure displayed below.
  • I have used three decoration blocks encircled with the blue color, to decorate my program.
  • Red boundary shows all of the decoration blocks to make your program attractive.
  • You can also decorate your programs using this amazing tools.
That is all from the tutorial DC Motor Speed Control in LabVIEW. I hope you enjoyed this tutorial. If you face any sort of problem you can ask me anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in some better way, if possible. I will explore NI LabVIEW, will make different projects on it and will share them with all of you as well. Till then, Take care :)

Stepper Motor Speed Control in Matlab

Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to tell you guys that how to design a simple algorithm for Stepper Motor Speed Control in MATLAB by simply sending different commands to the serial port from Matlab to Arduino. You should first read Stepper Motor Speed Control using Arduino because I am gonna take that tutorial further and will do the Stepper Motor Speed Control in Matlab. So, I would suggest you to first have a look at that tutorial before reading this one. Moreover, you should also have a look at DC Motor Direction Control in MatlabDC Motor Speed Control using Arduino. Stepper Motor Direction Control Direction Control in Matlab, Stepper Motor Direction Control using NI LabVIEW and Stepper Motor Speed Control using Arduino in which I have discussed in detailed about how to make simple and easy programs to control the direction of DC motor in both clockwise and anti clockwise direction and how to control the stepper motor direction and speed as well. So, you must go through my previous tutorials before going into the detail of today's tutorial because I have used the same hardware setup and a bit changed Arduino source code. Now, in this tutorial I will elaborate you that how can you send different commands through serial port from Matlab to the Arduino. I will create a simple GUI having different buttons on it through the direction as well as the speed of the DC motor can easily be controlled. So, let's have a look at Stepper Motor Speed Control in MATLAB.

Stepper Motor Speed Control in Matlab

Here, in this tutorial I will explain about how can make a program for Stepper Motor Speed Control in Matlab by creating a GUI having different buttons on it to control the direction of rotation of the stepper motor as well as the speed of the stepper motor by sending different commands through the serial port.
  • Here, you can download the complete simulation for Stepper Motor Speed Control in Matlab by clicking on the button below.

Download MATLAB Simulation

  • Download .rar file, extract it and enjoy the complete Matlab simulation.
Block Diagram
  • First of all I would like to elaborate the algorithm for Stepper Motor Speed Control in Matlab with the help of a block diagram.
  • Block diagram is shown in the figure below:
  • Fist of all power is supplied to the whole system to run it.
  • Matlab sends commands towards the Arduino UNO through the serial port using serial communication.
  • Arduino transfers those commands to the L298 motor controller andL298 drive the motor.
  • Arduino prints the executed commands on the LCD as well.
Initial GUI
  • First of all I have created a very simple GUI (Graphical User Interface) for Stepper Motor Speed Control in Matlab.
  • GUI has two different panels, Serial Port and Motor Controls. 
  • Serial Port panel has two buttons for opening and closing the serial port.
  • Motor Controls panel has five buttons for clockwise and anti clockwise rotation, accelerating and deaccelerating the Stepper motor and stopping the stepper motor.
  • Screen shot of the simple GUI for Stepper Motor Speed Control in Matlab is shown in the figure below.
  • Then I made few of the changes by double clicking on all the buttons to make the GUI beautiful.
  • The updated GUI is shown in the figure below.
  • The figure shown above is the starting GUI when you just run the program.
  • You can see below, after entering different commands GUI updates its text according to the command sent to the serial port.
Source Code Description
  • First of all you must have to open the serial port by clicking on the button Start Serial. 
  • Go to the callback function of Start Serial button in the Matlab code.
  • Just copy and paste the source code given below, in its callback function.
clc
global tep;
disp('Welcome to TEP');
disp('');
disp('www.TheEngineeringProjects.com');
disp('');
tep=serial('COM5'); % assign serial port object
set(tep, 'BaudRate', 9600); % set BaudRate to 9600
set(tep, 'Parity', 'none'); % set Parity Bit to None
set(tep, 'DataBits', 8); % set DataBits to 8
set(tep, 'StopBit', 1); % set StopBit to 1
%display the properties of serial port object in MATLAB Window
disp(get(tep,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits'}));
fopen(tep); % Open Serial Port Object
disp('Serial port is opened');
set(handles.text3, 'String','Serial port is opened');
  • Now, as you press this button GUI will update its text as Serial port is opened.
  • The updated GUI is shown in the figure below.
  • Now, go the callback function of Clockwise button inside the Matlab source code.
  • Just copy and paste the code given below in its call back function.
global tep
fwrite(tep,'C'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “C”.');
set(handles.text1, 'String','Motor is rotating in clockwise direction'); %setting static text's text
  • Now, if you press the Clockwise button the command will be sent from Matlab to arduino via serial port.
  • The GUI will update its text as Motor is rotating in clockwise direction.
  • The screenshot of the updated GUI is shown in the figure below.
 
  • Go to the callback function of Accelrate button and copy paste the code given below, in its callback function.
global tep
fwrite(tep,'H'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “H”.');
set(handles.text1, 'String','Motor is accerating slowly'); %updating text on GUI
  • As you press the Accelrate button the command will be sent from Matlab to Arduino via serial port.
  • The GUI will update its text as Motor is accelerating.
  • If you press this button again and again, motor will continue to rotate at higher and higher speed.
  • The screenshot of the updated GUI is shown in the figure below.
  • Now, go the callback function of the Deaccelrate button in the Matlab source code.
  • Just copy and paste the code given below in the callback function of this button.
global tep
fwrite(tep,'L'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “L”.');
set(handles.text1, 'String','Motor is deaccerating slowly'); %updating GUI text
  • As you press the Deaccelrate button the command will be sent from Matlab to Arduino via serial port.
  • The GUI will update its text as Motor is deaccelerating.
  • If you press this button again and again, motor will continue to rotate at slower and slower speed.
  • The screenshot of the updated GUI is shown in the figure below.
  • Now, go the callback function of the Anti Clockwise button in the Matlab source code.
  • Just copy and paste the code given below in the callback function of this button.
global tep
fwrite(tep,'A'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “A”.');
set(handles.text1, 'String','Motor is rotating in anti clockwise direction'); %updating the text onto the GUI
  • As you press the Anti Clockwise button the command will be sent from Matlab to Arduino via serial port.
  • The GUI will update its text as Motor is rotating in anti-clockwise direction.
  • If you press this button again and again, motor will continue to rotate in anti clockwise direction.
  • The screenshot of the updated GUI is shown in the figure below.
  • Go to the call back function of the Stop Motor button inside the Matlab source code.
  • Copy and just paste the code given below in its all back function.
global tep
fwrite(tep,'S'); %Print character ‘a’ to the serial port
disp('Charater sent to Serial Port is “S”.');
set(handles.text1, 'String','Motor is stopped');%Updating text on the GUI
  • Now, if you press the Stop Motor button the command will be sent from Matlab to Arduino via serial port.
  • The GUI will update its text as Motor is stopped.
  • The screenshot of the updated GUI is shown in the figure below.
  • Go to the call back function of the Stop Serial button in the Matlab code.
  • Copy and just paste the code given below in its call back function.
global tep;
fclose(tep);
set(handles.text1, 'String','Serial port is closed');%updating the text on the GUI
  • At the end you must need to close the serial port.
  • As you press the Stop Serial button the serial port will be closed.
  • The GUI will update its text as Serial port is closed.
  • The screenshot of the updated GUI for Stepper Motor Speed Control in Matlab is shown in below figure.
That's all from the tutorial Stepper Motor Speed Control in Matlab. I hope you enjoyed this tutorial. If you face any sort of problem anytime, you can ask me freely without even feeling any kind of hesitation. I will try my level best to solve your problem in a better way, if possible. I will explore Matlab by making different projects and I will share them with you as well in my later tutorials. Till then, take care :)

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