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?

allpcb circuit

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.



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:

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.



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.

Matlab Slider code:

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



DC Motor Control using Matlab GUI, Complete code:

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...

Related Articles

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