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.
Hexapod Simulation in MATLAB
Hello fiends, hope you all are fine and having fun with your lives. In today's tutorial, I am going to share a new project designed in MATLAB and named as Hexapod Simulation in MATLAB. We all know about the Hexapod, its a special kind of robot which has six legs. Hexa is used for six so its quite obvious that hexapod has six legs on it.
I have designed this project on a client's request and today I thought to share it with you guys. Because this Hexapod simulation in MATLAB is designed after a lot of efforts by our TEP team that's why this simulation is not free to download but we have placed a small price on it so that engineering students can buy it easily. So, let's get started with Hexapod Simulation in MATLAB.
Hexapod Simulation in MATLAB
- First of all, you need to buy this Hexapod Simulation in MATLAB by clicking the below button:
Buy This Simulation
- When you buy this project, you will get three files in it which are named as:
- Hexapod.m
- RobotMotion.m
- Robot Design.m
- You need to open the first one named as Hexapod.m, this is the Main file for this Hexapod Simulation in MATLAB.
- It has the below code in it:
clc;
clearvars;
close all;
imtool close all;
subplot(6,6,1:30)
xlabel('x'); ylabel('y'); zlabel('z');
axis([-150 200 -50 150 0 150])
grid on
hold on
Inc = 0;
firstLen = 50;
secondLen = 50;
stepSize = 20;
StepsTaken = 1;
y1 = 0;
y2 = 1;
RobotDesign(firstLen,secondLen)
subplot(6,6,31)
subplot(6,6,32)
subplot(6,6,33)
subplot(6,6,34)
subplot(6,6,35)
subplot(6,6,36)
OldInc = 0;
for b = 0:1:StepsTaken-1
subplot(6,6,1:30)
[Inc OldInc y1 y2]= RobotMotion(firstLen,secondLen,stepSize,Inc,OldInc, y1, y2);
end
- As you can see in the above code, we can set different parameters like lengths of legs and the steps it can take etc.
- Let me give a slight overview of Hexapod and how it works. So, have a look at below figure:
- I have designed a small hexapod and I have colored its legs.
- The red Color legs are called Gate 1 while the green color legs are called Gate 2.
- Now, when a Hexapod moves its first three legs (Gate 1) which I have designed in Red Color are first moved in upward motion and after that the Gate 1 moves in the Forward Direction and then finally Gate 1 moves in downward direction.
- After that the Gate 2 Legs are move in upward Direction and then Gate 2 Legs are moved in Forward Direction and finally Gate 2 moves in downward direction.
- Now when all legs are moved in Forward direction then finally the Robot Body is moved in Forward Direction.
- Let me summarize these steps:
- Gate 1 (Red Legs) moves in Upward Direction.
- Gate 1 (Red Legs) moves in Forward Direction.
- Gate 1 (Red Legs) moves in Downward Direction.
- Gate 2 (Green Legs) moves in Upward Direction.
- Gate 2 (Green Legs) moves in Forward Direction.
- Gate 2 (Green Legs) moves in Downward Direction.
- Robot Body moves in Forward Direction.
- These are the 7 steps a hexapod takes to move a single step Forward.
- Now in the above code, I have used some variables which are:
- firstLen = 50;
- secondLen = 50;
- stepSize = 20;
- StepsTaken = 1;
- firstLen is the length of Gate 1 legs which I have set 50 rite now.
- secondLen is the length of Gate 2 legs.
- stepSize is how big the step should be.
- StepsTaken is how much steps it should take.
- Now when you run the simulation then the first thing you will get is shown in below figure:
- AS I have given StepsTaken = 1 so it will just take one step, you can change it though and when it takes one step the final position of robot will be as shown in below figure:
- Now, if you compare the above two figures then you can see the starting position of Hexapod in x direction was 0 but when it took first step then now its position is 20.
- The below four graphs are showing the angles of your robot gates in radians.
- The below video will explain this project in detail:
That's all for today. I hope you have liked this Hexapod simulation in MATLAB. Before buying this project you must watch this video so that you have a clear overview of this Hexapod simulation in MATLAB.
DTMF Decoder using MATLAB
Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a project named as DTMF Decoder using MATLAB. In this project, I have designed a keypad in MATLAB using the GUI functionality of MATLAB. After designing the keypad, I have assigned a tune to each of these buttons. Obviously the tune attached to each button is different and when we press any of these buttons, then the MATLAB recognizes the respective button.
This project is designed in MATLAB and I have tested it on MATLAB 2009 and MATLAB 2014 and it works fine on both of them. Code is given below in this tutorial for download. f you got problem in it then ask in comments and I will try to resolve them. So, let's get started with DTMF decoder in MATLAB.
DTMF Decoder using MATLAB
- You can download the complete code by clicking the below button:
Download Project Files
- In this download package, you will get three files and you need to run the file named as decoder.m.
- When you run the file named as decoder.m, it will start the GUI which will look something as shown in below figure:
- That's the GUI used for DTMF Decoder using MATLAB.
- You can see a keypad is shown in the above GUI, now I have assigned a specific tune to each of these buttons and the code for assigning this tune is as follows:
t=[0:0.000125:.05];
fs=8000;
f1=770;f2=1477;
y1=.25*sin(2*pi*f1*t);
y2=.25*sin(2*pi*f2*t);
y=y1+y2;sound(y,fs)
- So, you can see in the above code that I have generated a sine wave and then created a sound using that sine wave.
- So, we have such sounds assigned to each of these buttons.
- Now once button is pressed, the respective sound will be activated and rite after that sound, I have added a subroutine for decoding that sound.
- This subroutine is placed in a separate file named as subdecode.m.
- This subdecode.m is responsible for DTMF decoding and its code is as follows:
axes(handles.fig1);
plot(t,y);
set(handles.fig1,'XMinorTick','on');
title('DTMF Input');xlabel('Time');
ylabel('Amplitude');grid;
rmain=2048*2;rmag=1024*2;
cn=9;cr=0.5;
cl=.25;ch=.28;
[b,a]=cheby1(cn,cr,cl);
yfilt1=filter(b,a,y);
h2=fft(yfilt1,rmain);
hmag2=abs(h2(1:rmag));
[b1,a1]=cheby1(cn,cr,ch,'high');
yfilt2=filter(b1,a1,y);
h3=fft(yfilt2,rmain);
hmag3=abs(h3(1:rmag));
axes(handles.fig2);
plot(yfilt1);grid;
title('Filtered Low Freq. Signal');
xlabel('Time');ylabel('Amplitude');
axes(handles.fig3);
plot(yfilt2);grid;
title('Filtered High Freq. Signal');
xlabel('Time');ylabel('Amplitude');
hlow=fft(yfilt1,rmain);
hmaglow=abs(hlow);
axes(handles.fig4);
plot(hmaglow(1:rmag));
title('FFT Low Pass');grid;
xlabel('Time');ylabel('Amplitude');
hhigh=fft(yfilt2,rmain);
hmaghigh=abs(hhigh);
axes(handles.fig5);
plot(hmaghigh(1:rmag));
title('FFT High Pass');grid;
xlabel('Time');ylabel('Amplitude');
m=max(abs(hmag2));n=max(abs(hmag3));
o=find(m==hmag2);p=find(n==hmag3);
j=((o-1)*fs)/rmain;
k=((p-1)*fs)/rmain;
if j<=732.59 & k<=1270.91;
disp('---> Key Pressed is 1');
elseif j<=732.59 & k<=1404.73;
disp('---> Key Pressed is 2');
elseif j<=732.59 & k<=1553.04;
disp('---> Key Pressed is 3');
elseif j<=732.59 & k>1553.05;
disp('---> Key Pressed is A');
elseif j<=809.96 & k<=1270.91;
disp('---> Key Pressed is 4');
elseif j<=809.96 & k<=1404.73;
disp('---> Key Pressed is 5');
elseif j<=809.96 & k<=1553.04;
disp('---> Key Pressed is 6');
elseif j<=809.96 & k>1553.05;
disp('---> Key Pressed is B');
elseif j<=895.39 & k<=1270.91;
disp('---> Key Pressed is 7');
elseif j<=895.39 & k<=1404.73;
disp('---> Key Pressed is 8');
elseif j<=895.39 & k<=1553.04;
disp('---> Key Pressed is 9');
elseif j<=895.39 & k>1553.05;
disp('---> Key Pressed is C');
elseif j>895.40 & k<=1270.91;
disp('---> Key Pressed is *');
elseif j>895.40 & k<=1404.73;
disp('---> Key Pressed is 0');
elseif j>895.40 & k<=1553.04;
disp('---> Key Pressed is #');
elseif j>895.40 & k>1553.05;
disp('---> Key Pressed is D');
end
- Now you can see in this code we are applying FFT on each of these sound signals and then comparing them to get our required button press.
- Now, when I press any of these buttons then the GUI will look something as shown in below figure:
- You can see in the above figure that first graph is showing the DTMF input, which is actual signal which I have converted to sound on button press.
- The second graph is showing the Filtered Low Frequency Signal while the third one is showing the Filtered High Frequency Signal.
- The two graphs on the right side are showing the Amplitude of FFT Low Pass and FFT High Pass.
- Now if you have a look at the Command window of MATLAB then it will give you the button pressed as shown in below figure:
- These are the buttons which I have pressed while testing it and it has given me each time which button is pressed.
- Here's the video which will give you better idea of How this DTMF decoder using MATLAB is working.
That's all about
DTMF Decoder using MATLAB. If you have any questions, then ask in comments and I will try my best to resolve them. Till next tutorial take care and have fun !!! :)
Convolution Calculator in MATLAB
Hello friends, hope you all are fine and having fun with your lives. In today's post, I am gonna design a Convolution Calculator in MATLAB. We all know about convolution but if you don't know then here's the wiki page for convolution which has a detailed description of Convolution. In simple words, convolution is a mathematical operation, which applies on two values say f and g and gives a third value as an output say v. In convolution, we do point to point multiplication of input functions and gets our output function.
Convolution is an important technique and is used in many simulating projects. It has a vital importance in image processing. So, today we are gonna do convolution in MATLAB and will check the output. You should also check Image Zooming with Bilinear Interpolation in MATLAB in which we have used correlation technique which is quite similar to convolution. It will give you a better idea of convolution, I recommend you to read their difference. Anyways, coming back to our today's Convolution Calculator, let's start its designing:
Convolution Calculator in MATLAB
- MATLAB has a built in command for convolution using which we can easily find the convolution of two functions.
- Syntax of this builtin convolution command is v=conv(x,h) where x and h are the input functions while v is our output.
- In my code I have used this builtin function as well as I have also design a small algorithm for calculating the convolution using its formula.
- After finding both the convolutions, I have simply compared them by plotting them on graph so that we can also verify that our result is correct.
- You can use this convolution calculator to find convolution of any two functions.
- So, first of all, copy this code and paste it in your m file and run it in your MATLAB:
clc
close all
clear all
x=input('enter the sequence, x(n)=')
h=input('enter the sequence, h(n)=')
m=length(x);
n=length(h);
subplot(4,1,1)
stem(1:m,x,'fill','r')
grid on;
title('input sequence, x(n)=')
xlabel('time n------>')
ylabel('amplitude----->')
subplot(4,1,2)
stem(1:n,h,'fill','r')
grid on;
title('impulse sequence, h(n)=')
xlabel('time n------>')
ylabel('amplitude----->')
%------linear convolution using inbuilt command------------------------%
v=conv(x,h)
l=m+n-1;
subplot(4,1,3)
stem(1:l,v,'fill','r')
grid on;
title('output sequence using inbuilt command, v(n)=')
xlabel('time n------>')
ylabel('amplitude----->')
%--------linear convolution using 'for' loop------------------------------%
X=zeros(1,l);
H=zeros(1,l);
X(1:m)=x;
H(1:n)=h;
for i=1:l
Y(i)=0;
for j=1:i
Y(i)=Y(i)+X(j)*H(i-j+1);
end
end
Y
subplot(4,1,4)
stem(1:l,Y,'fill','r')
grid on;
title('output sequence using loop, Y(n)=')
xlabel('time n------>')
ylabel('amplitude----->')
- The above code for convolution calculator is quite self explanatory but let me explain it a little.
- First of all, I am asking for inputs from user and they are saved in variables named as x and h.
- After that I am plotting them using stem function.
- In the next section, I have used the default MATLAB command for Convolution and calculated the convolution of x and h and saved it in v.
- Next I applied my simple algorithm and calculated convolution of x and h and saved it in Y and also plotted it.
- Once you run the simulation and give your input functions, you will get the below results:
- You can see in the above figure that I have given two inputs x and h and MATLAB Convolution Calculator calculated the convolution and gave us v and Y.
- v is calculated by default MATLAB command while the Y is calculated by our small convolution algorithm.
- Their graph representation is shown in the below figure:
- You can see in the above figure that we have plotted all the four signals, two inputs and two outputs and you can see both the outputs are same.
- Here's the video for this convolution calculator in MATLAB:
That's how you can design a convolution calculator in MATLAB. Let me know about your experience with this convolution calculator. I am planning to design a GUI for this calculator and will add it in this post.