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);
}




How to Control Dc Motor with Arduino?

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.

Similar Reads

Architecture L293D Motor Driver IC

L293D IC...

Connection of all Components

List of all the components...

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

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

Contact Us