DC Motor Control using MATLAB GUI and Arduino fully explained
Table of Contents
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 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:
Other Tools and Components:
Super Starter kit for Beginners
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:
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:
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.
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.
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.
After clicking on the save button the code will appear.
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.
1 2 3 4 |
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:
1 2 3 4 5 6 7 |
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.
1 2 3 4 5 6 7 |
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.
1 2 3 4 5 6 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
1 2 3 4 5 6 7 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
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.
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.