
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:- Speed Control of DC Motor using PIC Microcontroller
- DC Motor Speed Control using Arduino in Proteus ISIS
- Direction Control of DC Motor using Arduino in Proteus
- DC Motor Drive Circuit in Proteus ISIS
[dt_button link="https://www.theengineeringprojects.com/MATLABProjects/DC Motor Speed Control using matlab.rar" target_blank="false" button_alignment="default" animation="fadeIn" size="medium" style="default" bg_color_style="default" bg_hover_color_style="default" text_color_style="default" text_hover_color_style="default" icon="fa fa-chevron-circle-right" icon_align="left"]Download MATLAB Simulation[/dt_button]
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:

Leave a Reply
Leave a Reply
You must be logged in to post a comment.
Comments on ‘’DC Motor Speed Control in Matlab‘’
( 1 )
i m not getting this program can u pls help me