Home > Projects > Motor Projects > DC Motor Control Using MATLAB and Arduino

DC Motor Control Using MATLAB and Arduino

Summary of DC Motor Control Using MATLAB and Arduino


This tutorial demonstrates how to control a DC motor's rotation (clockwise, anti-clockwise, and stop) using MATLAB and an Arduino UNO. It guides users through creating a Graphical User Interface (GUI) with three pushbuttons via the `guide` command. The article provides specific MATLAB code snippets to write digital signals to pins 5 and 6 on the Arduino, utilizing the L293D motor driver to manage the motor's direction and state based on button clicks.

Parts used in the DC Motor Control Project:

  • MATLAB installed Laptop (R2016a or above)
  • Arduino UNO
  • DC Motor
  • L293D motor driver

DC-Motor-Control-Using-MATLAB-and-Arduino

In this tutorial, we will show you how to control DC motor using MATLB and Arduino. If you are new with MATLAB then it is recommend to get started with simple LED blink program with MATLAB.

Creating MATLAB Graphical User Interface for controlling DC Motor

After finishing setup with Arduino for MATLAB, we have to build GUI (Graphical User Interface) to control DC motor. To launch the GUI, type the below command in the command window

guide

A popup window will open, then select new blank GUI as shown in below image,

CREATI~1

Now choose three pushbuttons for Clockwise rotation, Anti-clockwise rotation and STOP, as shown below,

PUSHBU~1

To resize or to change the shape of the button, just click on it and you will be able to drag the corners of the button. By double-clicking on pushbutton you can change the color, string and tag of that particular button. We have customized three buttons as shown in below picture.

GUI-IN~1

GUI-FO~1

You can customize the buttons as per your choice. Now when you save this, a code is generated in the Editor windowof MATLAB. To code your Arduino for performing any task related to your project, you always have to edit this generated code. So below we have edited the MATLAB code.

MATLAB Code for controlling DC Motor with Arduino

Complete MATLAB code, after editing it for DC motor control, is given at the end of this project. Further we are including the GUI file (.fig) and code file(.m) here for download, using which you can customize the buttons as per your requirement. Below are some tweaks we did for rotating the DC Motor clockwise, anticlockwise and stop using three push buttons.Copy and paste the below code on line no. 74 to make sure that the Arduino is talking with MATLAB every time you run the m-file.

clear all;
global a;
a = arduino();

INITIA~1

When you scroll down, you will see that there are three functions for every Button in the GUI. Now write the code in every function according to task you want to perform on click.

In Clockwise button’s function, copy and paste the below code just before the ending braces of the function to rotate the motor in clockwise direction. Here we are giving HIGH at pin 6 and LOW at pin 5 to rotate the motor in clockwise direction.

global a;
writeDigitalPin(a, ‘D5’, 0);
writeDigitalPin(a, ‘D6’, 1);
pause(0.5);

COMMAN~1

Now in Anti-clockwise button’s function, paste the below code at the end of the function to rotate the motor in anti-clockwise direction. Here we are giving HIGH at pin 5 and LOW at pin 6 to rotate the motor in Anti-clockwise direction.

global a;
writeDigitalPin(a, ‘D5’, 1);
writeDigitalPin(a, ‘D6’, 0);
pause(0.5);

COMMAN~2

Finally in STOP button’s function, paste the below code at the end, to stop the rotation of motor. Here we are giving LOW at both pin 5 and 6 to stop the motor.

global a;
writeDigitalPin(a, ‘D5’, 0);
writeDigitalPin(a, ‘D6’, 0);
pause(0.5);

COMMAN~2

Material Required

  • MATLAB installed Laptop (Preference: R2016a or above versions)
  • Arduino UNO
  • DC Motor
  • L293D- motor driver

CIRCUI~1

DC-Motor-in-action-Using-MATLAB-and-Arduino

Controlling DC Motor with MATLAB

After setup the hardware according to circuit diagram, just click on the run button to run the edited code in .m file

RUN-PR~1

Read More Information..

DC Motor Control Using MATLAB and Arduino

Quick Solutions to Questions related to DC Motor Control Project:

  • How do I launch the GUI for this project?
    Type the command guide in the MATLAB command window.
  • What buttons are required to control the motor?
    You need three pushbuttons for Clockwise rotation, Anti-clockwise rotation, and STOP.
  • Which pins are used for clockwise rotation?
    Pin 6 is set HIGH and Pin 5 is set LOW.
  • How do you rotate the motor anti-clockwise?
    Set Pin 5 to HIGH and Pin 6 to LOW.
  • What happens when the STOP button is clicked?
    Both Pin 5 and Pin 6 are set to LOW to stop the motor.
  • What is the recommended starting point for new MATLAB users?
    Start with a simple LED blink program with MATLAB.
  • Where should the initialization code be pasted?
    Copy the initialization code to line number 74 of the generated editor file.
  • What version of MATLAB is preferred for this tutorial?
    R2016a or above versions are recommended.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top