Declaration of Variables in MATLAB
Hello everyone! I hope you all will be absolutely fine and having fun. In the tutorial
Declaration of variables in MATLAB, I will elaborate you that how to declare different variables in MATLAB and how to manipulate those variables without assigning them with the values. Before going into the details of this tutorial you must go through
Introduction to MATLAB, because understanding MATLAB first is better. This will help you in better understanding of this tutorial.
Variable declaration is one of the most important and compulsory steps while writing the code to perform any task. Without the declaration of the variables we can not proceed further to perform our task properly. So, if we want to do the different tasks in a proper way we must need to declare the variable before doing anything in the code. Once the variables have been declared, we can proceed further to explore the desired task.
Declaration of Variables in MATLAB
Here, in the tutorial
Declaration of variables in MATLAB, I will show you that how the variables can be declared in the MATLAB and how to manipulate them without assigning them with the values. The command
syms is used to declare the different variables in the
MATLAB.
- Here, I will show you that what happens if you have not declared the variables.
- The error is shown in the figure below, when you have not declared the variables.
- I have written an equation having three different variables named as f,t and r without declaring these variables.
- MATLAB has shown an error on the command window as undefined function or variable.
- So the error shown in the above figure shows the desired results can not be obtained without declaring the variables first.
- Now, I am going to declare all the variables first and will then manipulate them.
- The declared variables are shown in the figure below.
- Since I have declared all of the variables now which are being used in the equation.
- So, now I am going to manipulate them and going to observe the results.
- The result of the addition of the two variables, which is stored in the third variable, is shown in the figure below.
- The above figure shows the result of the addition of the two different variables in the form of an equation.
- The result has been obtained because all of the variables has been declared first, i.e. declaration of variables plays a very important role while writing an algorithm to perform any task either simple or complicated.
- You can also see all of the process in the video given below:
So, that is all from the tutorial Declaration of Variables in
MATLAB. I hope you enjoyed this tutorial. If you find any sort of problems you can ask me in comments without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will explain you that how to create an m.file in MATLAB. I will further explore MATLAB in my later tutorials and will share all of them with you as well. So, till then, Take Care :)
Introduction to MATLAB
Hello everyone! I hope you all will be absolutely fine and having fun. In this tutorial, I am going to give you a detailed
Introduction to MATLAB. I will explain in detail, step by step procedure of using
MATLAB for the first time. First of all, I would like to tell you a bit about the MATLAB software. MATLAB basically works on matrices and it is an excellent software for different kinds of simulation based projects as well as hardware based projects and specially, it is good for the
Engineering Projects.
MATLAB can be used for the manipulation purpose in order to obtain the proper results. It is very easy to use this software and most of the students prefer to use this software for their projects. So, this shows that MATLAB is a user friendly software as well. MATLAB has a very wide range of applications. It can also be used for the real time projects. Robotics, image processing and medical related projects are its major applications. You should also have a look at
How to Create GUI in MATLAB. So, let's get started with
Introduction to MATLAB:
Introduction to MATLAB
So, in the Introduction to MATLAB, I will show you the step by step procedure about how to use the MATLAB software for the first time.
- As you open the MATLAB software, a new window will be appeared on your screen.
- The window is shown in the figure below.
- The above window is the starting window which appears on the screen when you open the MATLAB.
- After sometime, you will be able to see another window on the screen.
- The second window is shown in the figure below.
Sections of MATLAB
There are six basic section of the MATLAB and understanding them is essential part of Introduction to MATLAB. The names of all of these sections are given below.
- Current Folder
- Command Window
- Workspace
- Command History
- View or Change Directory
- Help Section
Current Folder is the first section of the MATLAB’s front window at the left side of the window. It displays the files that has already been saved. You can open the files that are already saved somewhere in your PC or laptop.
- The current folder section is shown in the figure below.
Command Window is the second section and is used to show the results of the entire source code. If we want to observe the results on the command window, we must not put a semicolon after the statement then. If there is a semicolon at the end of the statement, result of that statement will not be shown on the command window.
- The command window section is shown in the figure below.
Workspace is the third section of the MATLAB’s front window which shows all the variables that are used while writing the complete algorithm.
- Workspace section is shown in the figure below.
Command History is the fourth and the last section of the MATLAB’s front window. It shows all of the commands that have been used in the entire algorithm.
- The command history section is shown in the figure below.
View or Change Directory helps us to view the directory or the complete destination of the file that is opened in the MATLAB. You can also open a new file by changing its directory.
- Directory box is shown in the figure below.
Help Section is also an amazing tool of the MATLAB. It provides help regarding of the command or any other thing related to MATLAB. By typing the thing which is not clear, you will be able to find a lot of help.
- The button to open the help section is shown in the figure below.
- As you press this button a new named as help will be appeared on your screen to help you.
- The help window is shown in the figure below.
- So that was the brief discussion about how to getting started with the MATLAB.
So, that is all from the tutorial Introduction to MATLAB. I hope you have enjoyed this tutorial. If you find any sort of problem in introduction to matlab, you can ask in comments anytime without even feeling any kind of hesitation. I will try my level best to solve your issues in a better way, if possible. In my next tutorial I will elaborate the
Declaration of Variables in MATLAB and how to manipulate them without assigning them with the values. Moreover, you should also have a look at
How to Create m file in MATLAB. I will further explore MATLAB in my later tutorials. So, 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 Matlab,
DC 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 C 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 H 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 L 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 A 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 S 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 Direction Control using Matlab
Hello friends! I hope you will be absolutely fine and having fun. Today, I am going to share that how to make a program for
Stepper Motor Direction Control using Matlab. I am going to share this tutorial on demand. I have also make different programs for
DC Motor Direction Control using Arduino,
DC Motor Direction Control in Matlab, DC Motor Direction Control using LabVIEW,
DC Motor Speed Control using Arduino, DC Motor Speed Control using Matlab and Stepper Motor Direction using Arduino in my previous tutorials. So before going into the detail of this tutorial you must have to go through these tutorial because I will use the same hardware and a bit changed Arduino source code. In this tutorial I will make an algorithm to send different commands from the Matlab to Arduino using the serial port.
I have created a simple GUI (Graphical User Interface) in Matlab having two different panels. The first panel contains two and the second panel contains three buttons respectively. Each button sends the corresponding command through the serial port from Matlab to the Arduino and hence the corresponding action takes place.
Stepper Motor Direction Control using Matlab
Here's the tutorial on Stepper Motor Direction Control using Matlab, I will elaborate you that how can you make a Matlab program to send different commands from Matlab to Arduino using serial communication. I have made a simple GUI first containing two different panels having five buttons in total for different purposes.
Initial GUI
- The initial diagram of the GUI for Stepper Motor direction control using Matlab, is shown in the figure below.
- Here you can download the complete Matlab Simulation.
Download Matlab Simulation
- Once the code is downloaded, simply extract the zip file and copy past the code in you Matlab software and enjoy the results. :)
- I have changed some of the properties of the GUI to make its external look better.
- The updated GUI is shown in the figure below.
Matlab Code Description
- Here I am going to explain the logic designed for this project Stepper Motor Direction Control using Matlab.
- Go to the callback function of the Start Serial button in the Matlab code.
- Copy and just paste the source code given below in its callback function.
clc
disp('Welcome to TEP');
disp('');
disp('www.TheEngineeringProjects.com');
disp('');
global tep
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
set(handles.text1,'String','Serial port os opened');
- The code given above first of all creates an object for the serial port named as tep.
- tep is made global so that it can be used in all the functions.
- Then it, opens the serial port and then sends command through the serial port.
- This code also prints baud rate, data bits, stop bit etc on the serial port as mentioned in the comments of the code given above.
- After sending the commands it also prints the commands executed on the GUI as well.
- The GUI with the printed command is shown in the figure below.
- Go to the callback function of the Clockwise button in the Matlab code.
- Copy and just paste the source code given below in its callback function.
global tep %making the serial port vaiable global
fwrite(tep,'C'); %sending C through the serial port
disp('Character C is sent to the serial port'); %displaying on the serial port
set(handles.text1,'String','Clockwise rotation'); %prints on the GUI
- The code given above send the command C through the serial port in order to rotate the motor in clockwise direction.
- It prints the command on the serial port as well as on the GUI created in Matlab.
- The GUI with the updated text for Stepper Motor Direction Control using Matlab is shown in the figure below.
- Now, go to the callback function of the Anti Clockwise button in the Matlab code.
- Copy and just paste the source code given below in its callback function.
global tep
fwrite(tep,'A'); %sending command to the serial port
disp('Character A is sent to the serial port'); %displaying on the serial port
set(handles.text1,'String','Anti clockwise rotation'); %prints on the GUI
- The code given above send the command A through the serial port in order to rotate the motor in anti clockwise direction.
- It prints the command on the serial port as well as on the GUI created in Matlab.
- The GUI with the updated text is shown in the figure below.
- Now, go to the callback function of the Stop button in the Matlab code.
- Copy and just paste the source code given below in its callback function.
global tep
fwrite(tep,'S'); %sends S to the serial port
disp('Character S in sent to the serial port'); %prints on the serial port
set(handles.text1,'String','Motor is stopped'); %prints on the GUI as well
- The code given above send the command S through the serial port in order to stop the rotation of the motor.
- It prints the command on the serial port as well as on the GUI created in Matlab.
- The GUI with the updated text is shown in the figure below.
- Now, go to the callback function of the Stop Serial button in the Matlab code.
- Copy and just paste the source code given below in its callback function.
global tep
fclose(tep);%Close Serial Port Object
set(handles.text1,'String','Serial port is closed');%prints the executed command on the GUI
- The code given above prints the command Serial port is closed on the serial port as well as on the GUI created in Matlab.
- The GUI with the updated text is shown in the figure below.
- Was it difficult! I do not think so ;)
- So, try to make the same project and observe the results, are they closed to me?
That's all from the tutorial
Stepper Motor Direction Control using Matlab. I hope you enjoyed this tutorial. If you face any sort of problem you an ask me any time without feeling any kind of hesitation. I will try my level best to solve your issue in a better way way if possible. I will explore Matlab and will share with all of you, by making different projects on it in my later tutorials. Till then, Take care :)
DC 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
DC Motor Speed Control in MATLAB by simply sending different commands to the serial port from Matlab to Arduino. In my previous tutorials,
DC Motor Direction Control using Arduino, we have seen the direction control of DC Motor via Arduino. 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 Matlab and
DC 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.
I have also given a detailed discussion about the PWM (Pulse Width Modulation) concept as well as about the duty cycle of the digital signal. I have also discussed that how to control the speed of the DC motor using PWN pins of the motor controller L298 in Arduino software. 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
DC Motor Speed Control in MATLAB:
DC Motor Speed Control using Matlab
Here, in this tutorial I will explain about how can make a program for DC Motor Speed Control in Matlab
by creating a GUI having different buttons on it to control the direction of rotation of the DC motor as well as the speed of the DC motor by sending different commands through the serial port.
Note:
Since you are working on the DC motor so you must also go through my previous tutorials, they will be helpful for you to simulate this project as well:
Here, you can download the complete simulation for DC Motor Speed Control in Matlab by clicking on the button below.
Download MATLAB Simulation
GUI Design and Matlab logic Description
- First of all I have created a very simple GUI (Graphical User Interface) 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 DC motor and stopping the DC motory.
- Screen shot of the simple GUI 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.text3, 'String','Motor is rotating in clockwise direction');
- Now, if you press the Clockwise button the command C 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 C_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.text3, 'String','Motor is accerating slowly');
- As you press the C_Accelrate button the command H 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 C_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.text3, 'String','Motor is deaccerating slowly');
- As you press the C_Deaccelrate button the command L 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.
- 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.text3, 'String','Motor is stopped');
- Now, if you press the Stop Motor button the command S 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);
disp('Serial port is closed');
set(handles.text3, 'String','Serial port is closed');
- 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 DC Motor Speed Control in Matlab is shown in below figure:
That's all from the tutorial
DC 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 feeling any kind of hesitation. I will try my level best to solve your problem if possible. I will explore the Matlab by making different projects on it in my later tutorials. Till then, take care :)
DC Motor Direction Control in MATLAB
Hello friends! 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 do the
DC Motor Direction Control in Matlab using serial communication with Arduino UNO. Serial communication is a very common and fast mean of communication now a days. In almost every engineering related projects we need to continuously send and receive data from micro controller to the computer and vice versa. So, I used this type of communication between Matlab and Arduino UNO. You must have a look at my previous tutorial
DC Motor Direction Control using Arduino because I am gonna use the same hardware and Arduino code and in today's tutorial I am gonna interface that hardware project with MATLAB so it will be like you will be sending commands from MATLAB and controlling your DC Motor.
So, in this tutorial I will explain you that how can you make a simple program in Matlab to control DC motor direction via serial communication between Matlab and Arduino. I have performed this serial communication with the help of different buttons created in the Matlab GUI. You can send commands to the Arduino UNO with the help of these buttons. So, let's have a look at
DC Motor Direction Control in MATLAB:
DC Motor Direction Control in MATLAB
In this tutorial I will explain that how to make a simple program in Matlab, to send the data through serial port and do the DC Motor Direction Control in MATLAB. Before going into the detail of this tutorial I would like to suggest you to first go through my previous tutorial
DC Motor Direction Control using Arduino because without reading that tutorial, you won't understand today's tutorial.
- You can download the complete Matlab simulation by clicking the below button:
Download MATLAB Simulation
Note:
If you are working on DC Motor then you should also have a look at these Proteus Simulations:
Its a very simple project which helps us to control the DC motor direction using serial communication between Arduino and Matlab. Step by step detailed discussion is given below, you can easily make this project by following these steps. Moreover, if you haven't worked on GUI before than I would suggest you to have a look at
How to create a GUI in MATLAB.
- I made a simple Graphical User Interface (GUI) consisting of two different panels named as Serial Port Controls and Motor Control.
- Serial Port Controls handles the serial port functions and this panel consists of two buttons Start and Stop which are helpful to start and stop the serial port respectively.
- Motor Controls handles DC motor direction and this panel consists of three buttons Clockwise rotation, Stop and Anti Clockwise rotation which are helpful to rotate the DC motor in clockwise and anti clockwise direction respectively.
- I have added a text box at the bottom to show the running commands when any of the buttons is pressed while the program is running.
- When you press any of the button, you can see the corresponding command on the text box.
- The simple GUI created in Matlab is shown in the figure below.
- After making this simple GUI shown in the figure above, I have made some changes to make its look better by changing the properties of the buttons and Static text box.
- The updated GUI with some changes for DC Motor Direction Control in MATLAB is shown in the figure below.
- Put your cursor on the Start Serial button and click on it and go to its call back function in the Matlab code.
- Just copy and paste the code given below in its call back function.
- If you haven't worked on Serial Port in MATLAB before then you should have a look at Send Data to Serial Port in MATLAB.
- So, now let's start working on the code for DC Motor Direction Control in MATLAB:
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
set(handles.text3, 'String','Srial port is opened. Please send your commands!');
- The variable tep is made global because we have to use in different functions, if we do not make it global we can not use it out of a particular function then.
- The code given above sets different properties e.g. baud rate, parity bits, stop bits, data bits etc.
- Then it is opening the serial port after making its variable named as tep and prints the text in the Static box created on GUI.
- The GUI with the updated text is shown in the figure below.
- Go to the call back function of Clockwise button.
- Copy and paste the sample of the source code given below, in the call back function of the clockwise button.
global tep
fwrite(tep,'C'); %Print character ‘C’ to the serial port
disp('Charater sent to Serial Port is “C”.');
set(handles.text3, 'String','Motor is rotating in clockwise direction');
- The code given above send the character C to the serial pot in order to rotate the motor in clockwise direction and displays this character as well on the serial port.
- In the second step, it is updating the text of the Static text box and displays it on the GUI created in Matlab.
- The GUI with the updated text is shown in the figure below.
- Now, go to the call back function of the Stop button in the Matlab GUI code.
- Just copy and paste the code given below in its call back function.
global tep
fwrite(tep,'S'); %Print character ‘S’ to the serial port
disp('Charater sent to Serial Port is “S”.');
set(handles.text3, 'String','Motor is stopped');
- The code given above is sending the character S to the serial port in order to stop the DC motor and also displays this character on the serial port as well.
- Then, it updates the text of the static text box in Matlab GUI.
- The GUI with the updated text is shown in the figure below.
- Now, go to the call back function of the Anti Clockwise button in the Matlab code.
- Copy and paste the code given below, in its call back function.
global tep
fwrite(tep,'A'); %Print character ‘A’ to the serial port
disp('Charater sent to Serial Port is “A”.');
set(handles.text3, 'String','Motor is rotating in anti clockwise direction');
- The code given above is sending the character A to the serial port in order to rotate the DC motor in anti clockwise direction and also displays this character on the serial port as well.
- Then, it updates the text of the static text box in Matlab GUI as .
- The GUI with the updated text is shown in the figure below Motor is rotating in anti clockwise direction .
- The GUI with the updated text is shown in the figure below.
- Now go the call back function of Stop Serial in the Matlab code.
- Just copy and paste the code given below in the call back function of stop serial button.
global tep
fclose(tep);
set(handles.text3, 'String','Srial port is closed');
- The code given above is closing the serial port and printing the text Serial port is closed on the GUI created in Matlab.
- The updated text printed in the Static text box is shown in the figure below.
That's all from the tutorial
DC Motor Direction Control in Matlab. I hope you enjoyed this tutorial. If you face any sort of problem regarding anything, you can freely ask me without feeling any kind of hesitation. I will try my level best to help you if possible. I will explore the Matlab software by making different projects in my later tutorial. Till then, Take care :)
Eye Ball Detection in MATLAB
Buy This Project
Hello friends, I hope you all are doing great. In today's tutorial, I am going to share
Eye Ball Detection in MATLAB. I have designed this simulation in MATLAB and I have designed a GUI in which I am detection the Eye Balls from Images. Our team has worked quite hard in designing this project so we haven't shared the code for free. Instead we have placed a very small amount and you can buy it from our shop via PayPal. Click the above button in order to buy the project.
If you are having any difficulty in buying this project then you can also ask in comments and we will surely help you out. Moreover, this algorithm has a small restriction that the person's eyes must be open in the image and his/her eye balls must be clearly visible otherwise this algo won't work on it. I have added 10 images in the database which will help you in understanding the main algorithm of this project. So, let's get started with Eye Ball Detection in MATLAB:
Eye Ball Detection in MATLAB
- So, first of all, click the below button in order to buy this Eye Ball Detection Project in MATLAB.
Buy This Simulation
- Once you download the files, then open the files named as MainGUI.m.
- Note, you must have MATLAB 2015 or higher in order to run this program correctly.
- It will open up a window which will look something as shown in below figure:
- One thing you should remember that MATLAB is not much good with graphics so you may have to change your axes' sizes.
- Anyways now click the button which says Browse and open any of the images present in the folder and then click Load Image button.
- I have opened the below image:
- If everything goes fine then you will get results something as shown in below figure:
- So, you can see in the above figure that I have labelled all the images of Eye Ball Detection in MATLAB.
- These labels are actually the steps in which we are doing our Eye Ball Detection.
- So, first of all I cropped the Pair of eyes and shown it in the Figure 2.
- After that I gray scaled the image and it is shown in Figure3.
- Next thing we need to do is to crop single eye from the pair of eyes which is done in Figure 4.
- In Figure 5, I have simply converted my gray scale image into binary image.
- In Figure 6, I have applied some filters to make it more smooth and to ignore noises.
- In Figure 7, I have taken the inverse of image in Figure 6. That's why now Eye Ball is showing in white color and rest of the background is in black.
- In Figure 8 and 9, I have applied filters, filters are always used to remove noise and to make figure more visible.
- In figure 10, I have taken the edges of the previous image.
- Finally in Figure 11, I have detected the circular region and then plotted a circle around it and I have done the same thing in Figure 12.
- Here's the results of another image for Eye Ball Detection in MATLAB:
- As, I have told earlier that our team has done a lot of effort in designing this project that's why we haven't shared its code but you can buy it quite easily from our shop by clicking the above button.
- Moreover, I have designed this video which will help you better in understanding this project.
- So, please must watch this video before buying this simulation so that you are sure what you are buying. you can also contact us if you are having any problems in buying it.
- Here's the video for EyeBall Detection in Matlab:
So, that's all about
Eye Ball Detection in MATLAB. I hope you guys have enjoyed today's post. Will meet you guys in the next tutorial. Till then take care and have fun !!! :)
ECG Simulation using MATLAB
Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to share a new project which is ECG Simulation using MATLAB. In this project, I have designed a complete simulation in MATLAB which is acting as ECG Simulator. This ECG Simulation also extracts ECG features and performs different functions which are explained in detail below. We have put a lot of effort in designing this project that's why its not free and we have placed a very small amount of $50 so that engineering students can buy it easily. You should also have a look at ECG Averaging in MATLAB.
Moreover, I have also designed a user friendly GUI which is quite easy to operate and you can simulate any kind of ECG data on this ECG Simulator. I have also extracted ECG features and then showed them separately. I have taken ECG data from PhsioBank website. Before buying this project must read this tutorial completely and also watch the video given at the end of this tutorial so that you are sure what you are buying. So, let's get started with ECG Simulation using MATLAB:
ECG Simulation using MATLAB
- You can buy this ECG Simulation using MATLAB by clicking the below button:
- When you buy this Simulation then you will get a lot of files and you need to run the ECGFinalGUI.m file.
- When you run this file, a GUI will open up as shown in below figure:
- As you can see this GUI contains two portions, the first portion is the Common ECG Diseases section:
- In this section, I have designed some basic ECG diseases signals.
- From the ECG curve, doctors find the disease so I have designed few curves which will show different ECG diseases.
- Like if the patient is suffering from Atrial Flutter then its curve will be something as shown in below figure and it comes when you click the Atrial Flutter button:
- I have designed curves of six ECG Diseases which are as follows:
- Normal Sinus Rhythm
- Junctional Rhythm
- Atrial Fibrillation
- Ventricular Fibrillation
- Ventricular Tachycardia
- Sinus Tachycardia
- Sinus Bradycardia
- Atrial Flutter
- You can check their ECG graphs by clicking each of these buttons.
- The last button in the common ECG Diseases is for filtering of ECG Signals.
- When you click this button then the program will filter the real ECG signal and remove noise from it as shown in below figure:
- Now let's have a look at the feature extraction from ECG Signal.
- I have downloaded around 12 signals from PhsioBank Website and here's my settings on this website:
- I have downloaded each signal of 10 sec duration as shown in above figure and saved it in txt form and then copied the data in txt files.
- So, this simulation contains total 12 signals.
- Now once you saved your signal in txt format now you need to use the Load ECG Signal button on the GUI and it will ask for browse.
- So, select your ECG signal and it will show its features like Heart Rate, R-R interval, QRS interval etc as shown in below figure:
- You can see in the above figure that as I selected the ECG signal, first of all GUI extracted the first ECG curve and shown it separately in the above window.
- After that I have extracted the P, Q, R, S and T peaks.
- After that I have also extracted the Heart Rate, R - R interval and QRS interval.
- Next phase is to diagnose the disease, which is Normal in this case.
- If this patient had some disease then it will be displayed in this Diagnosis section.
- Next section is Severity section, in which I have displayed the Condition of the Patient i.e. its severe or mild etc.
- So, let me load some other ECG signal and let's have a look at its results:
- Now in this set of ECG data we have detected that the patient is suffering with Sinus Bradycardia.
- The below video will give you a better idea of How this project works, so before buying it must watch this video first:
So, that's all for today. I hope you guys have enjoyed today's tutorial on ECG Simulation using MATLAB. Till next tutorial, take care and have fun !!! :)
Color Detection in MATLAB Live Video
Hello friends, I hope you all are fine and having fun with your lives. Today, I am going to show you Color Detection in MATLAB Live Video. In this project I am gonna take a live video feed from a camera and then will detect a specific color in that video feed. I have posted a project before in which I have done the same
Color Detection in Images using MATLAB but that color detection was for images but in today's post I am gonna do the color detection in MATLAB for live video feed.
I have designed this project in MATLAB 2009 and have also tested it on MATLAB 2015 and it works fine. So, I think it will work on any version of MATLAB. You must have the image processing toolbox in your MATLAB. If you got any questions then ask in comments and I will try to resolve them. So, let's get started with Color Detection in MATLAB Live Video:
Few Image Processing Projects:
Color Detection in MATLAB Live Video
- First of all, download the code from clicking the below button and open it in your MATLAB.
Download MATLAB Code
- Now let me explain this code in detail.
- One important thing, if you encountered some MATLAB project and you don't know how it works then the best way of testing is to paste commands one by one in your Command Window.
- In this way you will understand the working of each command and will give you a better idea of how it works.
- The complete MATLAB cofor Color Detection in MATLAB Live Video is as follows:
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
B=getsnapshot(obj);
framesAcquired = 0;
while (framesAcquired <= 10)
data = getsnapshot(obj);
framesAcquired = framesAcquired + 1;
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
%stats = regionprops(diff_im, 'BoundingBox', 'Centroid');
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2))), ' Color: Red'));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
end
hold off
end
clear all
- I have also added the comments in the code so that you can understand it in a better way.
- But if you still got any problems then as k in comments and I will resolve them.
- Now run your simulation and bring any red color object in front of your camera and you will see a bounding box will be formed around red color and it will also show the coordinated of that red color.
- The results are shown in the below figure:
- You can see in the above figure that I am holding a bottle cap which is of red color and our code is detecting that red color.
- Moreover it has created a bounding box around that bottle cap and then showing the X and Y coordinates of that cap.
- I have also mentioned the Color Red infront of it and the color of these coordinates is also RED.
- You can change these colors as you want and can detect any color using this code.
- Let me explain this code a little bit.
Code Explanation
- First of all, I created an object for the video camera and then took images from that video using the below code.
- In this code I have used 1 for video input. 1 is default for your webcams and if you have any external camera connected via usb then you have to use 2 instead of 1.
- The code is as follows:
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
B=getsnapshot(obj);
- After that I have created a while loop of 100 framesso it will keep on detecting the color for 100 frames and then it will stop you can increase or decrease it.
- In this while loop, first of all I have subtracted the red color from each frame using the imsubtract command.
- You can see the input of imsubtract is data( : , : , 1 ), where data is our image and 1 is used for red color subtraction, if you use 2 then it will subtract the green color so using these values you can subtract any color and infact you are detecting that color.
- After that I have applied filter to remove the noise and have also removed the pixels less than 300px.
- Then I have connected the dots of red color to create a single red object.
- This code is as follows:
data = getsnapshot(obj);
framesAcquired = framesAcquired + 1;
diff_im = imsubtract(data(:,:,1), rgb2gray(data));
diff_im = medfilt2(diff_im, [3 3]);
diff_im = im2bw(diff_im,0.18);
%stats = regionprops(diff_im, 'BoundingBox', 'Centroid');
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
- Finally after the detection of this red color, I have created a bounding box around it and have also printed its coordinates.
- The code is as follows:
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2))), ' Color: Red'));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
end
- So, that's how I ma doing the color detection in MATLAB Live Video.
- The below video will explain this Color Detection in MATLAN Live Video in more detail:
I hope you guys have enjoyed this Color Detection in MATLAB and are gonna use it your projects. That's all for today, will see you guys in the coming tutorials. Till then take care and have fun. :)
Motion Detection in MATLAB
Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a new project which is Motion Detection in MATLAB. In this project, I am gonna detect the motion in MATLAB. This project was designed for security purposes and the condition was to use MATLAB instead of PIR Sensor. We all know that PIR sensor is used for motion detection but for that we have to design a hardware but using this software we can easily detect any motion using MATLAB.
In this project, I have used the webcam and then applied a simple image processing algorithm, designed in MATLAB. Using this algorithm I have detected the motion in the environment. That's a quite simple project and you are gonna like this one. The code and complete simulation is given below for download. So, let's get started with Motion Detection in MATLAB:
You may also like:
Motion Detection in MATLAB
- First of all download the Motion Detection in MATLAB simulation by clicking the below button:
Download the MATLAB Simulation
- Once you downloaded the rar file, you will get two files in it, named as:
- Comparison.m
- Comparison.fig
- Now open the Comparison.m file and run your simulation.
- When you run the file a GUI will open up as shown in below figure:
- Now you can see it has three buttons on it.
- Press the first button which says Capture Image, so click this button and an image will be saved in the same directory and will also be shown in the first zone as shown in below figure:
- Now, click on the Start Comparison button and the software will start and will start capturing from the webcam.
- It will also show the captured image in the second zone and if there's no change then the text below will show No Change, as shown in below figure:
- You can see as there's no motion in the room that's why it says No change in above figure.
- Now, let's create some motion, so I am taking my hand in front of the cam and let's see what results we got in the below figure:
- You can see in the above figure that as I placed my hand in the range of webcam, the below text changed to Change Detected. Btw it should be motion detected. :P
- Once the software captures the motion it will indicate as well as stop and if there's no motion then it will keep on monitoring.
- If you want to stop the comparison then simply click the Stop Comparison button and it will automatically stop.
- Moreover, the software will also send character to the serial port, if you want you can receive it on any serial port.
- Now let's discuss the codes behind these buttons.
MATLAB Codes
- The code behind Capture Image button is as follows:
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
A=getsnapshot(obj);
axes(handles.axes1);
imshow(A);
imwrite(A,'A.jpg');
delete(obj);
- In the above code, first of all, I created an object of the webcam and then took a screenshot from that webcam.
- After that I saved that image in the project directory and named it as A.jpg.
- If you check your project directory then you will find this image.
- Now let's have a look at the code behind Start Comparison Button:
global go;
go = true;
while go
obj=videoinput('winvideo',1);
obj.ReturnedColorspace = 'rgb';
B=getsnapshot(obj);
axes(handles.axes2);
imshow(B);
imwrite(B,'B.jpg');
delete(obj);
global I1;
global I2;
I1 = imread('A.jpg');
I2 = imread('B.jpg');
% convert images to type double (range from from 0 to 1 instead of from 0 to 255)
Imaged1 = im2double(I1);
Imaged2 = im2double(I2);
% reduce three channel [ RGB ] to one channel [ grayscale ]
Imageg1 = rgb2gray(Imaged1);
Imageg2 = rgb2gray(Imaged2);
% Calculate the Normalized Histogram of Image 1 and Image 2
hn1 = imhist(Imageg1)./numel(Imageg1);
hn2 = imhist(Imageg2)./numel(Imageg2);
% Calculate the histogram error/ Difference
f1 = sum((hn1 - hn2).^2);
%set(handles.text1,'String',f1)
serialOne=serial('COM1', 'BaudRate', 9600);
fopen(serialOne);
if f1 > 0.009
fprintf(serialOne,'a');
set(handles.text1,'String','Change Detected')
go = false;
end
if f1 < 0.009
fprintf(serialOne,'b');
set(handles.text1,'String','No change')
end
fclose(serialOne);
- This is the main code of this Motion detection in MATLAB project.
- Here, again I am creating an object of the webcam and taking a screenshot.
- After that I am saving this screenshot in the project's directory and I have renamed it as B.jpg.
- So, now I have two images A and B.
- After that I have converted both of these images to grey scale and calculated their histogram error and on the basis of this error I have detected the motion.
- Finally I have created a Serial Port object, if you don't wanna use serial port then simply remove this code.
So, that's all for today, I hope you have enjoyed the motion detection in MATLAB. If you have any questions, then ask in comments and I will resolve them.