How to Control DC Motor Using Arduino

How to Control DC Motor Using Arduino

Are you a beginner and want to get into robotics? then you must have many questions in mind like "How to make the robot move?" or "which tools to use to make the robot move?". If you are anything like me then you probably have disassembled tons of toys to figure that out. Most common thing you find in these toys are DC motors. 

In this tutorial we will see, how to control a DC Motor, it's speed and direction using arduino and L293D motor driver IC.

So without wasting any more time, lets get right into it.

Disclaimer: This blog contains Amazon Affiliate links. Buying product from those links helps us run this blog without any extra charges on you.


For this project we will need,
So every thing in the list might look familiar except "L293D". Let me explain about this IC in a simple and short term. L293D is a motor drive IC which can be used to control 2 DC motors or 1 Stepper motor. It has 2 H-bridges which helps control the speed and direction of  motors. 

l293d ic

We cannot connect a motor directly to arduino as it will fry the board instantly. So we use a motor driver like L293D or L298N. You can find motor driver modules and shields online which help control multiple motors but for this tutorial we will stick with minimum for learning the basics. 

In the above image you can see the pin outs of the IC. Each side controls one motor. For this tutorial we will control one motor using arduino.

 Pin No
Enable pin for Motor 1
Enable 1,2
Input 1 for Motor 1
Input 1
Output 1 for Motor 1
Output 1
Output 2 for Motor 1
Output 2
Input 2 for Motor 1
Input 2
 Power the Motors
 Vcc 2
Enable pin for Motor 2
Enable 3,4
Input 1 for Motor 1
Input 3
Output 1 for Motor 1
Output 3
Output 2 for Motor 1
Output 4
Input2 for Motor 1
Input 4
Power the IC
Vcc 1



Here we have connected the input pins of IC to arduino:
  • IN1 to Pin 8
  • IN2 to Pin 9
  • Enable1 to Pin 5 (PWM)
Pin 4,5,12,13 of IC are connected to GND of Arduino and battery. If you don't do this, the motor will not work.

Connect Pin 8 and 16 of the IC to +ve terminal of 9v battery. This will power our motor and the IC itself. 

That's all we need to do. now we can write code to control the motor.


#define ENA1 5 //Sets pin 5 of arduino as Enable1
#define IN1 8 // Sets pin 8 of arduino as Input 1
#define IN2 9 // Sets pin 9 of arduino as Input 2

 void setup()
//Set all pins as output
   pinMode(ENA1, OUTPUT);
   pinMode(IN1, OUTPUT);
   pinMode(IN2, OUTPUT);

 void loop()
   analogWrite(ENA1, 255); //Sets speed of motor using PWM
   digitalWrite(IN1, HIGH); //Input 1 is turned on
   digitalWrite(IN2, LOW); //Input 2 is turned off
   delay(3*1000); //Runs for 3 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW); //Both inputs are turned off
   digitalWrite(IN2, LOW);
   delay(2*1000); // Waits for 2 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW); // Input 1 is off
   digitalWrite(IN2, HIGH); // Input 2 is on
   delay(3*1000); //Runs for 3 seconds

   analogWrite(ENA1, 255);
   digitalWrite(IN1, LOW);
   digitalWrite(IN2, LOW);

Let's brake down the code to make it easy to understand.

First we define pins that we will be using.

#define ENA1 5
#define IN1 8
#define IN2 9

Here the ENA1 pin is connected to pin 5 of arduino as it is a PWM enabled pin. This will help us control the speed of our motor.
Next there are pins 8 and 9 connected to pin IN1 and IN2 respectively. These pins will make the motor start, stop and change direction depending on which pin is HIGH/LOW. 

void setup()
   pinMode(ENA1, OUTPUT);
   pinMode(IN1, OUTPUT);
   pinMode(IN2, OUTPUT);

In the setup we set the purpose of the declared pins, in this case all pins are set as output. 

Now comes the loop function. The code in this part runs in a loop.

  analogWrite(ENA1, 255);
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);

Here notice the function analogWrite(ENA1, 255) we are using the PWM feature of pin 5. The PWM value can be anything between 0-255, where 0 will turn off the motor and 255 will spin the motor at full speed. Varying the value between 1-254 will give different speeds.
Next two functions are digitalWrite, these these sets a pin either HIGH or LOW. As in the above example, the IN1 is HIGH and IN2 is LOW, this will make the motor turn in one direction. Right or Left depending on the way you connected the terminals.

Swapping the HIGH and LOW will change the direction of rotation, and making both LOW/HIGH Will make the motor stop.

Now that you have an idea of the code, it's time to upload the code. So connect your arduino to PC and upload the code.


Now that the code is uploaded, power the motors and IC with 9v as shown in the circuit in previous step. You will see that the motor spins in one direction for 3 seconds then stops for 2 seconds and spins in other direction.

Here you can see the code in action. Now you can edit the code and change the values in analogWrite. Make it anything between 0-255 and see the results. Similarly you can control one more motor with L293D.

That's all for this tutorial. Now you know how to control DC motors using Arduino. There are other better methods like using Motor drive modules and shields but that's a topic for another tutorial. Till then keep experimenting. 

Feel free to ask any doubts or leave any suggestions. If you liked the tutorial, follow for more. It's totally free!