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