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