Open In App

How to Control Dc Motor with Arduino?

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Here In this article, we will learn about DC motors and how these motors can be controlled using an Arduino board with very little programming. We are also going to use an L293D motor controller IC. This is very important to use the controller board because we can not connect the DC motors directly to the Arduino board. The power supply from an Arduino board is 3.3V and 5V which are insufficient to power a DC motor of 9V. If we try using Arduino there is a high risk of losing the Arduino board.

Architecture L293D Motor Driver IC

L293D IC

On this IC there is a total of 16 pins. The Upper 8 pins can control a single DC motor and the lower can also control another DC motor. A total of 2 DC motors can be controlled using an L293D IC. The pin description is given below.

  • Enable 1&2: This is to enable the lower side of the IC, and can also be used to control the amount of voltage in the lower part of the IC.
  • Input 1 and Input 2: Inputs to control the motor directions.
  • Output 1 and Output 2: Connect to the motor providing the current.
  • GND & Power 2: Connects to the 9V battery.
  • Enable 3&4: This is to enable the upper side of the IC, and can also be used to control the amount of voltage in the upper part of the IC.
  • Input 3 and Input 4: These control the motor directions as well.
  • Output 3 and Output 4: Connect to the motor providing the current.
  • GND & Power 1: Connects to the 9V battery.

Connection of all Components

List of all the components

  • Arduino board
  • L293D driver IC
  • Motors
  • 9V battery
  • Bread Board

Step 1: Motor and L293D connections

Connection both the motors directly to the motor ports or pins on the L293D motor driver IC. Named as Output 1 & Output 2 for motor 1 and Output 3 & Output 4 for motor 2.

Step 2: Battery and L293D connections

The 9V battery is connected to the breadboard, and the Power 1 and Power 2 of the IC can be connected to the +terminal on the breadboard. The GND from the upper and GND from the lower of the IC can be connected to the -ve terminal on the breadboard.

Step 3: Arduino Board and L293D connections

Connect the IC pins as mentioned. Enable pin to digital pin 13 on Arduino, input 1 to digital pin 12, and input 2 to digital pin 11 on Arduino. Now for the upper side of the IC, connect enable pin to pin 3, input 3 to digital pin 4, and input 4 to digital pin 5 on the Arduino board.

Check your connection from the below image.

All the connections we have done till now can be seen in the image below. Use this image to cross-verify your project before executing it yourself.

All connections on Arduino

Control DC Motors Using L293D IC and Arduino

After all the connections are done properly we can now move to the coding part on the Arduino board. Here we are using the free Arduino simulator tinkercad. After importing and connecting all the components as mentioned above, we have various cases of operating the motors.

Case 1: Rotate Both Motors

Here we will rotate the motors, in our case both motors are rotating anti-clockwise.

GIF: Motor anti-clockwise rotation


C++




// C++ code
//
int speedM1 = 13;
int dir1M1 = 12;
int dir2M1 = 11;
 
int speedM2 = 3;
int dir1M2 = 4;
int dir2M2 = 5;
 
void setup()
{
    pinMode(speedM1, OUTPUT);
    pinMode(dir1M1, OUTPUT);
    pinMode(dir2M1, OUTPUT);
 
    pinMode(speedM2, OUTPUT);
    pinMode(dir1M2, OUTPUT);
    pinMode(dir2M2, OUTPUT);
}
 
void loop()
{ // Motor 1 run
    analogWrite(speedM1, 255);
    digitalWrite(dir1M1, HIGH);
    digitalWrite(dir2M1, LOW);
    // Motor 2 run
    analogWrite(speedM2, 255);
    digitalWrite(dir1M2, HIGH);
    digitalWrite(dir2M2, LOW);
}


Case 2: Reverse the Rotation

Simply, change the HIGH to LOW and LOW to HIGH.

GIF: Motor direction reversed


C++




// C++ code
//
int speedM1 = 13;
int dir1M1 = 12;
int dir2M1 = 11;
 
int speedM2 = 3;
int dir1M2 = 4;
int dir2M2 = 5;
 
void setup()
{
    pinMode(speedM1, OUTPUT);
    pinMode(dir1M1, OUTPUT);
    pinMode(dir2M1, OUTPUT);
 
    pinMode(speedM2, OUTPUT);
    pinMode(dir1M2, OUTPUT);
    pinMode(dir2M2, OUTPUT);
}
 
void loop()
{ // Motor 1 reverse
    analogWrite(speedM1, 255);
    digitalWrite(dir1M1, LOW);
    digitalWrite(dir2M1, HIGH);
    // Motor 2 reverse
    analogWrite(speedM2, 255);
    digitalWrite(dir1M2, LOW);
    digitalWrite(dir2M2, HIGH);
}


Case 3: Rotate M1 and Stop M2

Both digitalWrites for motor 2 will become LOW for it to stop. Similarly, use it for stopping M1 and rotating M2.

GIF: M2 Stopped


C++




// C++ code
//
int speedM1 = 13;
int dir1M1 = 12;
int dir2M1 = 11;
 
int speedM2 = 3;
int dir1M2 = 4;
int dir2M2 = 5;
 
void setup()
{
    pinMode(speedM1, OUTPUT);
    pinMode(dir1M1, OUTPUT);
    pinMode(dir2M1, OUTPUT);
 
    pinMode(speedM2, OUTPUT);
    pinMode(dir1M2, OUTPUT);
    pinMode(dir2M2, OUTPUT);
}
 
void loop()
{
    analogWrite(speedM1, 255);
    digitalWrite(dir1M1, LOW);
    digitalWrite(dir2M1, HIGH);
 
    analogWrite(speedM2, 255);
    digitalWrite(dir1M2, LOW);
    digitalWrite(dir2M2, LOW);
}


Case 4: Control Motor Speed

Here in this article, the pins on Arduino we are using are digital PWM (Pulse Width Modulation). These pins allow us to control the signal strength using a pre-defined function analogWrite().

GIF: Speed of motor 2 is reduced


C++




// C++ code
//
int speedM1 = 13;
int dir1M1 = 12;
int dir2M1 = 11;
 
int speedM2 = 3;
int dir1M2 = 4;
int dir2M2 = 5;
 
void setup()
{
    pinMode(speedM1, OUTPUT);
    pinMode(dir1M1, OUTPUT);
    pinMode(dir2M1, OUTPUT);
 
    pinMode(speedM2, OUTPUT);
    pinMode(dir1M2, OUTPUT);
    pinMode(dir2M2, OUTPUT);
}
 
void loop()
{ // values between 0 and 255 are used. But below 100 it is
  // hard to operate.
  // use 255 for full speed.
    analogWrite(speedM1, 255);
    digitalWrite(dir1M1, LOW);
    digitalWrite(dir2M1, HIGH);
 
    analogWrite(speedM2, 150);
    digitalWrite(dir1M2, LOW);
    digitalWrite(dir2M2, HIGH);
}




Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads