DC Motor Direction Control in MATLAB

Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge with all of you guys about how to do the DC Motor Direction Control in Matlab using serial communication with Arduino UNO. Serial communication is a very common and fast mean of communication now a days. In almost every engineering related projects we need to continuously send and receive data from micro controller to the computer and vice versa. So, I used this type of communication between Matlab and Arduino UNO. You must have a look at my previous tutorial DC Motor Direction Control using Arduino because I am gonna use the same hardware and Arduino code and in today's tutorial I am gonna interface that hardware project with MATLAB so it will be like you will be sending commands from MATLAB and controlling your DC Motor. So, in this tutorial I will explain you that how can you make a simple program in Matlab to control DC motor direction via serial communication between Matlab and Arduino. I have performed this serial communication with the help of different buttons created in the Matlab GUI. You can send commands to the Arduino UNO with the help of these buttons. So, let's have a look at DC Motor Direction Control in MATLAB:

DC Motor Direction Control in MATLAB

In this tutorial I will explain that how to make a simple program in Matlab, to send the data through serial port and do the DC Motor Direction Control in MATLAB. Before going into the detail of this tutorial I would like to suggest you to first go through my previous tutorial DC Motor Direction Control using Arduino because without reading that tutorial, you won't understand today's tutorial.
  • You can download the complete Matlab simulation by clicking the below button:

Download MATLAB Simulation

Note: If you are working on DC Motor then you should also have a look at these Proteus Simulations: Its a very simple project which helps us to control the DC motor direction using serial communication between Arduino and Matlab. Step by step detailed discussion is given below, you can easily make this project by following these steps. Moreover, if you haven't worked on GUI before than I would suggest you to have a look at How to create a GUI in MATLAB.
  • I made a simple Graphical User Interface (GUI) consisting of two different panels named as Serial Port Controls and Motor Control.
  • Serial Port Controls handles the serial port functions and this panel consists of two buttons Start and Stop which are helpful to start and stop the serial port respectively.
  • Motor Controls handles DC motor direction and this panel consists of three buttons Clockwise rotation, Stop and Anti Clockwise rotation which are helpful to rotate the DC motor in clockwise and anti clockwise direction respectively.
  • I have added a text box at the bottom to show the running commands when any of the buttons is pressed while the program is running.
  • When you press any of the button, you can see the corresponding command on the text box.
  • The simple GUI created in Matlab is shown in the figure below.
 
  • After making this simple GUI shown in the figure above, I have made some changes to make its look better by changing the properties of the buttons and Static text box.
  • The updated GUI with some changes for DC Motor Direction Control in MATLAB is shown in the figure below.
 
  • Put your cursor on the Start Serial button and click on it and go to its call back function in the Matlab code.
  • Just copy and paste the code given below in its call back function.
  • If you haven't worked on Serial Port in MATLAB before then you should have a look at Send Data to Serial Port in MATLAB.
  • So, now let's start working on the code for DC Motor Direction Control in MATLAB:
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
set(handles.text3, 'String','Srial port is opened. Please send your commands!');
  • The variable tep is made global because we have to use in different functions, if we do not make it global we can not use it out of a particular function then.
  • The code given above sets different properties e.g. baud rate, parity bits, stop bits, data bits etc.
  • Then it is opening the serial port after making its variable named as tep and prints the text in the Static box created on GUI.
  • The GUI with the updated text is shown in the figure below.
 
  • Go to the call back function of Clockwise button.
  • Copy and paste the sample of the source code given below, in the call back function of the clockwise button.
global tep
fwrite(tep,'C'); %Print character ‘C’ to the serial port
disp('Charater sent to Serial Port is “C”.');
set(handles.text3, 'String','Motor is rotating in clockwise direction');
  • The code given above send the character C to the serial pot in order to rotate the motor in clockwise direction and displays this character as well on the serial port.
  • In the second step, it is updating the text of the Static text box and displays it on the GUI created in Matlab.
  • The GUI with the updated text is shown in the figure below.
  • Now, go to the call back function of the Stop button in the Matlab GUI code.
  • Just copy and paste the code given below in its call back function.
global tep
fwrite(tep,'S'); %Print character ‘S’ to the serial port
disp('Charater sent to Serial Port is “S”.');
set(handles.text3, 'String','Motor is stopped');
  • The code given above is sending the character to the serial port in order to stop the DC motor and also displays this character on the serial port as well.
  • Then, it updates the text of the static text box in Matlab GUI.
  • The GUI with the updated text is shown in the figure below.
  • Now, go to the call back function of the Anti Clockwise button in the Matlab code.
  • Copy and paste the code given below, in its call back function.
global tep
fwrite(tep,'A'); %Print character ‘A’ to the serial port
disp('Charater sent to Serial Port is “A”.');
set(handles.text3, 'String','Motor is rotating in anti clockwise direction');
  • The code given above is sending the character to the serial port in order to rotate the DC motor in anti clockwise direction and also displays this character on the serial port as well.
  • Then, it updates the text of the static text box in Matlab GUI as .
  • The GUI with the updated text is shown in the figure below Motor is rotating in anti clockwise direction .
  • The GUI with the updated text is shown in the figure below.
  • Now go the call back function of Stop Serial in the Matlab code.
  • Just copy and paste the code given below in the call back function of stop serial button.
global tep
fclose(tep);
set(handles.text3, 'String','Srial port is closed');
  • The code given above is closing the serial port and printing the text Serial port is closed on the GUI created in Matlab.
  • The updated text printed in the Static text box is shown in the figure below.
  That's all from the tutorial DC Motor Direction Control in Matlab. I hope you enjoyed this tutorial. If you face any sort of problem regarding anything, you can freely ask me without feeling any kind of hesitation. I will try my level best to help you if possible. I will explore the Matlab software by making different projects in my later tutorial. Till then, Take care :)

DC Motor Direction Control using Arduino

Hello friends! I hope you all will be absolutely fine and having fun. Today, I am going to share my knowledge with all of you about how to make a simple program for DC Motor Direction Control using Arduino. The word DC is basically an abbreviation of Direct current. So, a direct current motor is commonly used motor having two input terminals, one is positive and the other one is negative. If we connect these terminals with the voltage supply the motor will rotate. If you change the polarity then motor will rotate in opposite direction. You should also have a look at Difference between DC & AC Motors to get a better idea about these motors. DC motor has a lot of applications. You can use it in automation projects, for controlling static as well as mobile robots, in transport system, in pumps,fans,bowers and for industrial use as well. In this tutorial, I will do the DC Motor Direction Control using Arduino and L298 motor controller. Moreover, I have also used LCD which will give us the status of our DC Motor i.e. whether its moving in clockwise direction or anticlockwise. In my later tutorial I will control the same DC motor using NI LabVIEW 2015 and MATLAB. I have added the next tutorial on this project in which I have done the DC Motor Direction Control in MATLAB so in that project, I have used the same hardware but instead of controlling it from Arduino I have controled it using MATLAB so you must have a look at that tutorial.

DC Motor Direction Control using Arduino

In this tutorial, I will make a simple program to do the DC Motor Direction Control using Arduino. Arduino is basically an amazing micro controller and is very easy to use because it is an open source device. So, it is a student friendly device. You can also write Arduino programs for different purpose. Arduino is also a cost efficient device in comparison to the other micro-controllers e.g. raspberyy pi, NI-myRIO, galileo, single board RIO etc. First of all I prepared my complete hardware setup. Then I made a program and interfaced it with the hardware. We will discuss all the steps in detail below. The logic is pretty simple i.e. Arduino has to send commands to L298 motor controller and then L298 decides the DC Motor Direction Control by manipulating the Arduino commands. Before going into the detail, I want to show you the list of components required. You can download complete Arduino source file here:

Download Arduino Source Code Note: If you are working on DC Motor then you should also have a look at these Proteus Simulations:

Components List & Description

Here's the complete list of the components required for designing DC Motor Direction Control using Arduino: So, now let's discuss the main components for this project individually so that you get better idea of why these components are used in this DC Motor Direction Control using Arduino:
Arduino UNO
Arduino UNO is basically the back bone of this DC Motor Direction Control Project. It controls and leads the whole project. In this project, Arduino reads the commends from serial port and sends to L298 motor controller IC in order to control the direction of rotation of the DC motor. So, the Arduino has overall major control over the whole project.
Motor Controller L298
Motor Controller is used to control the direction of DC motor. It consists of an L298 motor driver IC which is capable of rotating the motor in both clockwise and anti clockwise directions by switching its pins from HIGH to LOW and vise versa. Moreover, it needs +12V, GND and +5V in order to power it up. So, we will design a voltage regulator which will step down 12V to 5V. So, let's have a look at this voltage driver in next part:  
Voltage Regulator
Voltage regulator is also the part of this design. In our daily life, we need to step up or to step down the voltages according to the requirements. Requirements vary with the different purpose. Small electronics components like micro-controller, LED, LCD etc. So the main purpose of voltage regulator is to step down the voltage from 12V to just 5V in order to fulfill the requirements of the electronic components. Step down transformer can also be used instead of voltage regulator. Due to the huge structure and cost we prefer to use voltage regulator. You should read How to Design a 5V Power Supply in Proteus to get better idea about this voltage regulator. The circuit diagram of the designed voltage regulator is shown in the figure below.
DC Motor
DC motor is the essential part of the different projects and our daily life. for example if we want to automate our house doors i.e if we want to open and close the doors automatically by detecting the person, motor plays a vital role here. Similarly in robotics, vacuum, blowers and air conditioners, DC motor has a wide range of applications. LED is used here to show whether the designed circuit is working properly or not. Like in mobile phones and laptops as we connect the charger it shows the charging indication. So, we must need some indication that everything is going fine and the circuit is working properly.
Jumper Wires
Jumper wires are used to make the connections between all of the components. Use small pieces of the jumper wires in order to give a better look to the designed circuit. If you are using longer wires for the connections, it will create complexity and causes many problems while operating the circuits.
Power Supply
12V power supply is used as the main power supply. As we know, to operate any of the electronic components or electronic appliances we must need the main power supply. Power supply can vary according to the power consumption of the electronic equipment. Here I am using a 12V DC power supply because it is a small and simple project with minimum power requirements.
LCD 20x4
LCD is used to visualize the commands sent to the serial port. It basially display us that which function is being performed at a particular time. A 20×4 LCD is used and is shown in the figure below. If you haven't worked on LCD before, then you should have a look at Circuit Designing of LCD with Arduino in Proteus ISIS.
Assembling of the Components
Here are the few steps followed while designing this DC Motor Direction Control using arduino:
  • Connect the terminals of the DC motor with the output pins (OUT1 and OUT2) of L298 motor controller.
  • Connect L298 motor controller's pin IN1 and IN2 with the Arduino UNO's pin 2 and 5 respectively.
  • Now, connect ENA pin of L298 motor controller to the Arduino's pin 9.
  • Connect the power supply to turn on the circuit.
  • Make sure that you have supplied 12V, 5V and GND properly to the L298 motor controller.  
Circuit Diagram
Completely Assembled Diagram

Arduino Code Designing

After making all the connections properly, open your Arduino source code. If you are using Arduino for the first time then you should have a look at Installation of Arduino Driver in Windows.
  • Attach the Arduino board with your PC and go to Search->Device Manager as shown in the figure below.
  • Select the device manger and you can see different options here like Batteries, bluetooth radios, keyboards, monitors, ports etc.
  • Open the Ports(COM & LPT) as shown in the figure below.
  • See the COM Port supported by Arduino Board which COM5 in this case.
  • Now open the Arduino software and go to Tools and select the Arduino board and the COM port properly.
  • The description is shown in the figure given below.
  • Just copy and paste the source code given below.
#include <LiquidCrystal.h>
//Keyboard Controls:
//
// C - Clockwise
// S - Stop
// A - Anti-clockwise

// Declare L298N Controller pins
// Motor 1
int dir1PinA = 2;
int dir2PinA = 5;
int speedPinA = 7; // PWM control
LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() { 
 
Serial.begin(9600); // baud rate

lcd.begin(20, 4);
lcd.setCursor(5,0);
lcd.print("DC Motor");
lcd.setCursor(5,1);
lcd.print("Direction");
lcd.setCursor(5,2);
lcd.print("Control");
lcd.setCursor(2,3);
lcd.print("via Arduino UNO");
//Define L298N Dual H-Bridge Motor Controller Pins

pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);

}

void loop() {

// Initialize the Serial interface:

if (Serial.available() > 0) {
int inByte = Serial.read();
int speed; // Local variable

switch (inByte) {

case 'C': // Clockwise rotation
analogWrite(speedPinA, 255);//Sets speed variable via PWM 
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
Serial.println("Clockwise rotation"); // Prints out “Motor 1 Forward” on the serial monitor
Serial.println("   "); // Creates a blank line printed on the serial monitor
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Clockwise rotation");
break;

case 'S': // No rotation
analogWrite(speedPinA, 0); // 0 PWM (Speed)
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, LOW);
Serial.println("No rotation");
Serial.println("   ");
//lcd.clear();
lcd.setCursor(5,1);
lcd.print("No rotation");
break;

case 'A': // Anti-clockwise rotation
analogWrite(speedPinA, 255); // Maximum PWM (speed)
digitalWrite(dir1PinA, HIGH);
digitalWrite(dir2PinA, LOW);
Serial.println("Anti-clockwise rotation");
Serial.println("   ");
//lcd.clear();
lcd.setCursor(3,2);
lcd.print("Anti-clockwise");
break;

default:
// Turn off the motor if any other key is being pressed
for (int thisPin = 2; thisPin < 11; thisPin++) {
digitalWrite(thisPin, LOW);
}
Serial.println("Wrong key is pressed");
//lcd.clear();
lcd.setCursor(0,3);
lcd.print("Wrong key is pressed");
  }
    }
      }
  • Now, upload the source code onto the Arduino UNO board as shown below.
  • In the above figure shows that the source code is uploading to the Arduino board.
   
  • Done uploading shows that the source code has been uploaded successfully to the Arduino borad.
  • Now, go to the Serial Monitor on the top right corner of the Arduino software.
  • Press C, you can see the DC motor is rotating in the clockwise direction and statement Clockwise rotation will be printed on the Serial Monitor.
  • Now, press S, the DC motor will stop and a statement No rotation will be print on the Serial Monitor.
  • If you want to rotate DC motor in anti-clockwise direction, press A then, the statement Anti-Clockwise rotation will be printed on the Serial Monitor.
  • I have made the logic in such a way that if you press any of the other buttons the DC motor will stop in reaction to that and the statement Wrong key is pressed will be printed on the Serial Monitor.
  • All of the above steps are shown in the figure shown below.
   

Final Testing of DC Motor Direction Control using Arduino

  • The screenshot of the actual circuitry for DC Motor Direction Control using Arduino is shown in the below figure:
  • You can see in the above figure that we have attached Arduino UNO board with L298 Motor Driver and then we have attached DC Motor with Arduino UNO and LCD is used to show the current movement of Motor.
  • Moreover we have also designed a small circuit which I have mentioned above and named as Voltage regulator, and it is used to step down 12V into 5V.
So, that's all from the tutorial DC motor Direction Control using Arduino. I hope you enjoyed this tutorial. In my next tutorials, I will interface this project with LabView and MATLAB. If you face any sort of problem, you can freely ask me anytime without feeling any kind of hesitation. So, will see you guys in next tutorial. Till then Take  care :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir