L293D Motor Driver Shield for Arduino

l293d motor driver shield for Arduino

The L293D Motor driver shield is one of the best way for controlling DC, Servo and Stepper motors especially if you are using Arduino UNO or MEGA in projects like robotics and CNC. Another rather common driver is the L298N motor driver but unlike the L293D driver, this one mainly controls DC motors.

Before looking at the L93D motor driver shield you should first learn how the L293D motor driver works and how it is used to control DC and stepper motors. I have looked at this driver in detail in another tutorial that can be referred to using the link below.

  • L293D Motor Driver for Controlling DC and Stepper motors using Arduino.
  • Hardware overview for L293D motor driver shield

    This shield is based on the L293D IC and can drive 4 bi-directional DC motors , 2 stepper motors and 2 servo motors. It is mainly compatible with the Arduino UNO and MEGA boards.

    Take note that each channel of this module has the maximum current of 1.2A and doesn’t work if the voltage is more than 25v or less than 4.5v.

    l293d motor shield pinout

    The L293D is a dual-channel H-Bridge motor driver capable of driving a pair of DC motors or single stepper motor. This shield offers total four H-Bridges and each H-bridge can deliver up to 0.6A to the motor.

    The shield also comes with a 74HC595 shift register that extends 4 digital pins of the Arduino to the 8 direction control pins of two L293D chips.

    Power Supply considerations for the L293D motor driver.

    If you are using a single power supply for both Arduino and motors , simply plug it into the DC jack of the Arduino board or use the EXT_PWR block on the shield .Make sure the power jumper on the motor shield is in place. This method is best if the motor supply voltage is less than 12V.

    You can also use two separate power supplies for Arduino and motors for example you can power the Arduino through the USB and the motor powered off of a DC power supply or use two separate DC power supplies for the Arduino and motors. In this case make sure the jumper is removed from the motor shield.

    Controlling motors using the L293D motor driver shield and Arduino

    Before using the L293D motor driver shield with Arduino IDE, we need to install the AFMotor.h library which contains the commands to control DC, stepper and servo motors.

    You can download the AFMotor.h library from here.

    Driving DC Motors.

    This motor shield can drive DC motors having voltages between 4.5 to 25V. The DC Motors I am using are rated at 9V. So, we will connect external 9V power supply to the EXT_PWR terminal.

    The motor is connected to any of M1, M2, M3 or M4 motor terminals. In this case, Arduino pin11 for M1, pin3 for M2, pin5 for M3, pin6 for M4 and pins 4, 7, 8 and 12 are all in use.

    driver shield dc motor control

    Code for controlling the DC motor.

    #include <AFMotor.h>
    AF_DCMotor motor(4);
    void setup() 
    {
    	//Set initial speed of the motor & stop
    	motor.setSpeed(200);
    	motor.run(RELEASE);
    }
    void loop() 
    {
    	uint8_t i;
    	// Turn on motor
    	motor.run(FORWARD);	
    	// Accelerate from zero to maximum speed
    	for (i=0; i<255; i++) 
    	{
    		motor.setSpeed(i);  
    		delay(10);
    	}
    	// Decelerate from maximum speed to zero
    	for (i=255; i!=0; i--) 
    	{
    		motor.setSpeed(i);  
    		delay(10);
    	}
            // Now change motor direction
    	motor.run(BACKWARD);
    	// Accelerate from zero to maximum speed
    	for (i=0; i<255; i++) 
    	{
    		motor.setSpeed(i);  
    		delay(10);
    	}
    	// Decelerate from maximum speed to zero
    	for (i=255; i!=0; i--) 
    	{
    		motor.setSpeed(i);  
    		delay(10);
    	}
    	// Now turn off motor
    	motor.run(RELEASE);
    	delay(1000);
    }
    

    Driving Stepper Motors.

    If you are using 28BYJ-48 unipolar stepper, those motors are rated at 5V and offer 48 steps per revolution. So, connect external 5V power supply to the EXT_PWR terminal. Remember to remove the PWR jumper.

    The stepper motor is connected to any of the four terminals M1-M2(port1) and M3-M4(port2). In this experiment we are connecting it to Port 2.

    stepper motor control
    #include <AFMotor.h>
    // Number of steps per output rotation
    // Change this as per your motor's specification
    const int stepsPerRevolution = 48;
    // connect motor to port #2 (M3 and M4)
    AF_Stepper motor(stepsPerRevolution, 2);
    void setup() {
      Serial.begin(9600);
      Serial.println("Stepper test!");
      motor.setSpeed(10);  // 10 rpm   
    }
    void loop() {
      Serial.println("Single coil steps");
      motor.step(100, FORWARD, SINGLE); 
      motor.step(100, BACKWARD, SINGLE); 
      Serial.println("Double coil steps");
      motor.step(100, FORWARD, DOUBLE); 
      motor.step(100, BACKWARD, DOUBLE);
      Serial.println("Interleave coil steps");
      motor.step(100, FORWARD, INTERLEAVE); 
      motor.step(100, BACKWARD, INTERLEAVE); 
      Serial.println("Micrsostep steps");
      motor.step(100, FORWARD, MICROSTEP); 
      motor.step(100, BACKWARD, MICROSTEP); 
    }
    

    Driving Servo Motors.

    Driving servos with L293D motor shield is very easy. Just connect the three pins of the motor to the servo terminals of the shield.

    In this case Arduino pins 9, 10, 2 are in use and the power for the Servos comes from the Arduino’s on-board 5V regulator, so you don’t need an external power supply on the EXT_PWR terminal.

    servo motor control

    The motor shield actually breaks out Arduino’s 16bit PWM output pins 9 and 10 to the edge of the shield with two 3-pin headers and since we are using the onboard PWM pins, the sketch uses IDE’s built in Servo library.

    #include <Servo.h>
    Servo myservo;	// create servo object to control a servo
    int pos = 0;	// variable to store the servo position
    void setup() 
    {
    	// attaches the servo on pin 10 to the servo object
    	myservo.attach(10);   
    }
    void loop() 
    {
    	// sweeps from 0 degrees to 180 degrees
    	for(pos = 0; pos <= 180; pos += 1) 
    	{
    		myservo.write(pos);
    		delay(15);
    	}
    	// sweeps from 180 degrees to 0 degrees
    	for(pos = 180; pos>=0; pos-=1)
    	{
    		myservo.write(pos);
    		delay(15);
    	}
    }