Matlab

DC Motor Control using MATLAB GUI and Arduino fully explained

DC motor control using MATLAB GUI and Arduino:

DC Motor Control using MATLAB GUI and Arduino fully explained- in my previous three tutorials I explained,

How to install the Matlab software and how to use it?

How to Make a GUI in Matlab?

How to interface Arduino with Matlab using the Hardware support Package?

How to control an LED using the Matlab GUI?

In my previous tutorial I covered the extreme basics and I explained each and every detailed to control an LED using the Matlab GUI. Now, I think we can try something intermediate, In this tutorial we will discuss how to control a DC motor from MATLAB by creating a GUI graphical user interface using Arduino.



Amazon Links:

Arduino Uno

L293D Motor Driver

Small 5V DC Motor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

DC motor Interfacing with Arduino using L293D, Circuit Diagram:

DC Motor Control using Matlab

In the circuit diagram I used the L293d motor driver and a toy DC motor. The L293d motor driver is capable of controlling 2 DC motors while in our case we are using only one side of the l293d Motor Driver because that I am using only one motor and the enable of first pin that is the enable pin of l293d is connected to the digital pin 8 of the Arduino and the input of the l293d are connected to the 6 and 7pin of the Arduino.

DC motor interfacing with MATLAB:

Now we will create GUI for the DC motor in MATLAB, so, first type guide and GUI interface will appear. Now we will design our GUI:

DC Motor Control using Matlab




I need three buttons for this GUI so if I press the first button the motor should run clockwise, if I press the second button the motor should run anti-clockwise, and if I press the third one the motor should stop. Now to create these buttons just select the button and drag it and click on the button and you can change the title and tag of the button as explained in my previous LED control tutorial. Change the names of the button to clockwise, anticlockwise, and stop.

DC Motor Control using Matlab

We are also going to control the speed of the motor. So, I am going to use a slider and the speed of the motor can be seen in the text box next to the slider, that’s it, and I am going to do some changes to the slider that the maximum value should be 5 for the slider.

DC Motor Control using Matlab

Then we will save the GUI by click on the save button in the GUI with the name dc_motor and click on the save button.

DC Motor Control using Matlab

After clicking on the save button the code will appear.

DC Motor Control using Matlab

The first thing, we have to initialize some variables, so, whatever the previous function that is in the memory that will be cleared if I give the clear all function, and I am going to assign a global variable and this global variable will have Arduino.

varargout{1} = handles.output;
clear all;
global a;
a=arduino;



Clockwise button code:

So everything is initialized in the beginning so when i press the push button which is clockwise so what should it has to do with global a and write the command:

functionclockwise_Callback(hObject, eventdata, handles)
% hObject    handle to clockwise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',0);
writeDigitalPin(a,'D7',1);

the writeDigitalPin(a,’D6′,0) command is quite simple, d6 is the pin 6 on the Arduino, 0 means to turn OFF the pin 6, and a represent the Arduino. Similarly, for the second command which control the D7 pin.

Anticlockwise button code:

Now we will write the command for the other button which is anticlockwise button. Which will be similar to that clockwise function button, but there will be change in the value for the digital pin 6 we will have 1 and for digital pin 7 we will have 0.

functionanticlockwise_Callback(hObject, eventdata, handles)
% hObject    handle to anticlockwise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',1);
writeDigitalPin(a,'D7',0);



Stop button code:

Now we will write the command for the stop button when we will click on the stop button the motor will stop. We will copy the above code and assign 0 for both digital pin 6 and 7.

functionStop_Callback(hObject, eventdata, handles)
% hObject    handle to Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',0);
writeDigitalPin(a,'D7',0);

Matlab Slider code:

Now we will create function for the slider and write the command

function slider1_Callback(hObject, eventdata, handles)
global a;
slider=get(hObject,'Value')
slider1=slider*20;
set(handles,edit1,'String',num2str(slider1);
writePWMVoltage(a,'D8',slider);
guidata(hObject,handles)



DC Motor Control using Matlab GUI, Complete code:

function varargout = dc_motor(varargin)
% DC_MOTOR MATLAB code for dc_motor.fig
%      DC_MOTOR, by itself, creates a new DC_MOTOR or raises the existing
%      singleton*.
%
%      H = DC_MOTOR returns the handle to a new DC_MOTOR or the handle to
%      the existing singleton*.
%
%      DC_MOTOR('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DC_MOTOR.M with the given input arguments.
%
%      DC_MOTOR('Property','Value',...) creates a new DC_MOTOR or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before dc_motor_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to dc_motor_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help dc_motor

% Last Modified by GUIDE v2.5 23-Jun-2021 13:33:17

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @dc_motor_OpeningFcn, ...
                   'gui_OutputFcn',  @dc_motor_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before dc_motor is made visible.
function dc_motor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to dc_motor (see VARARGIN)

% Choose default command line output for dc_motor
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes dc_motor wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = dc_motor_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;
clear all;
global a;
a=arduino;


% --- Executes on button press in clockwise.
function clockwise_Callback(hObject, eventdata, handles)
% hObject    handle to clockwise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',0);
writeDigitalPin(a,'D7',1);

% --- Executes on button press in anticlockwise.
function anticlockwise_Callback(hObject, eventdata, handles)
% hObject    handle to anticlockwise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',1);
writeDigitalPin(a,'D7',0);


% --- Executes on button press in Stop.
function Stop_Callback(hObject, eventdata, handles)
% hObject    handle to Stop (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global a;
writeDigitalPin(a,'D6',0);
writeDigitalPin(a,'D7',0);


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
global a;
slider=get(hObject,'Value')
slider1=slider*20;
set(handles,edit1,'String',num2str(slider1));
writePWMVoltage(a,'D8',slider);
guidata(hObject,handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider




% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end



function edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double


% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

Now when we will click on the run button the GUI will open and when we will click on the clockwise button the motor will move in the clockwise direction and when we will click on the anticlockwise button the motor will change its direction. When we will click on the stop button the motor will stop and also we can change the speed of the motor through slider. The text box will show the current speed of the motor.

DC Motor Control using Matlab

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

One Comment

  1. Hello,
    I watched your arduino-matlab motor rotation project in your youtube channel.
    I tried to do the same but the slider never worked.
    The motor rotates clockwise /anticw but the slider not. Do you have any idea why it doesnt work?
    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button