DC Motor Arduino Tutorial

A DC motor is widely used motor in hobby electronics and in robotics to drive the robot and in other application. We can drive a DC motor by simply connecting its two terminal to the positive and negative of the battery. One measure issue here is if want to change the direction of the rotation then we have to interchange the battery terminal.And this is not always possible in a running application to change the wiring.

So the question is how to control a DC motor direction with the arduino. In this case, we need some kind of dc motor driver, arduino motor driver shield or motor driver circuit that can be used to control the dc motor direction. All the motor driver board containing H-Bridge circuit either in IC or in a transistorized circuit. We are interfacing dc motor with L293D motor driver IC. It has two H-Bridge so two motors can be controlled.

Hardware Required

  • Arduino Uno
  • H Bridge L293D Motor Driver IC
  • DC Motor
  • Breadboard

DC Motor Direction Control using Arduino

DC motor direction control using arduino by giving the high and low signal to the L292D motor driver IC. This table shows, how to control dc motor direction with arduino.

Inputs Motor Direction
Pin(2,10) Pin(7,15) Pin(3,11) Pin(6,14)
Low Low Stop
Low High Clockwise/Counter Clockwise
High Low Counter Clockwise/Clockwise
High High Stop

How to Run a DC Motor using Arduino

L293D motor driver has two H-Bridge for controlling two dc motor.As shown in the circuit diagram, a dc motor is connected to pin no. 3 and 6. For running it in clockwise or counter-clockwise direction we need to apply input at pin no. 2 and 7.
The input should be High and Low or Low and High at the input pin 2 and 7.
You can refer the table above for easy understanding.

Single DC Motor Circuit Arduino

DC Motor Arduino Connection

DC Motor Arduino Program

We drive dc motor using L293D motor driver IC by giving HIGH and LOW signal.

digitalWrite(10, HIGH);
digitalWrite(11, LOW);

And it will move either in clockwise or counter-clockwise direction. To change the direction simply change the input.

digitalWrite(10, LOW);
digitalWrite(11, HIGH);

And direction will be opposite to above.

/*DC Motor Connection with Arduino
Arduino Pin  L293D IC Pin 
       10   =>   2 
       11   =>   7  
Motor Connected at Pin 3 and 6 of L293D IC 
*/
void setup() {           
  pinMode(10, OUTPUT);  
  pinMode(11, OUTPUT);
 }

 void loop(){
  digitalWrite(10, HIGH); 
  digitalWrite(11, LOW); 
 }

How to Control DC Motor with Arduino – Two DC Motor

As we are comfortable in driving single dc motor from the above tutorial. Driving two dc motor is quite easy. While driving two dc motor to make sure dc motor M1 is connected to pin no. 3, 6 and dc motor M2 at pin no. 11 and 14 of L293D IC. Input pin 2, 7 for driving motor M1 and pin 10, 15 for motor M2 as we can see from the wiring diagram below.

Arduino L293D Motor Driver Connection

A pin no. 8 is used for giving power supply for dc motor. If a dc motor operating voltage is 12v. Then apply 12V to the pin 8. But sometimes we want less speed(RPM), to reduce the RPM of the dc motor give less voltage at pin 8 of L293D IC.

L293D and DC Motor Circuit Arduino – Two Motor

Arduino DC Motor Control Code

We drive dc motor by giving HIGH and LOW signal to each motor input pair.
digitalWrite(10, HIGH);
digitalWrite(11, LOW); 

If you are designing a robot we generally need all the motion like forward, backward, left and right. Simply write the code and change the input signal each time to motor input pair and we will get different motion like forward, backward and others. Once getting all motions do the rest of the programming for designing robot.

 /*DC Motor Connection with Arduino 
Arduino Pin   L293D IC Pin
       8    =>   15
       9    =>   10
       10   =>   7 
       11   =>   2  
L293D IC Motor Connection
Motor M1 => Pin 3, 6
Motor M2 => Pin 11, 14    
*/
void setup() { 
  pinMode(8, OUTPUT);  
  pinMode(9, OUTPUT);          
  pinMode(10, OUTPUT);  
  pinMode(11, OUTPUT);
 }

 void loop(){
  digitalWrite(8, HIGH); 
  digitalWrite(9, LOW); 
  digitalWrite(10, HIGH); 
  digitalWrite(11, LOW); 
 }

Leave a Reply

Your email address will not be published. Required fields are marked *