Intro

picture 3
description

Features:

  • Can be used to run Two DC motors with the same IC.
  • Speed and Direction control is possible
  • Motor voltage Vcc2 (Vs): 4.5V to 36V
  • Maximum Peak motor current: 1.2A
  • Maximum Continuous Motor Current: 600mA
  • Supply Voltage to Vcc1(vss): 4.5V to 7V
  • Transition time: 300ns (at 5Vand 24V)
  • Automatic Thermal shutdown is available
  • Available in 16-pin DIP, TSSOP, SOIC packages

picture 1

description

Using this L293D motor driver IC is very simple. The IC works on the principle of Half H-Bridge, let us not go too deep into what H-Bridge means, but for now just know that H bridge is a set up which is used to run motors both in clock wise and anti clockwise direction. As said earlier this IC is capable of running two motors at the any direction at the same time, the circuit to achieve the same is shown above.

Demo

L293D_motor_driver.ino

picture 0

// PIN VARIABLES
// the right motor will be controlled by the motor A pins on the motor driver
const int AIN1 = 13; // control pin 1 on the motor driver for the right motor
const int AIN2 = 12; // control pin 2 on the motor driver for the right motor
const int PWMA = 11; // speed control pin on the motor driver for the right motor
 
// the left motor will be controlled by the motor B pins on the motor driver
const int PWMB = 10; // speed control pin on the motor driver for the left motor
const int BIN2 = 9;  // control pin 2 on the motor driver for the left motor
const int BIN1 = 8;  // control pin 1 on the motor driver for the left motor
 
int switchPin = 7; // switch to turn the robot on and off
 
// VARIABLES
int motorSpeed = 0; // starting speed for the motor
 
void setup()
{
    pinMode(switchPin, INPUT_PULLUP); // set this as a pullup to sense whether the switch is flipped
 
    // set the motor control pins as outputs
    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);
 
    pinMode(BIN1, OUTPUT);
    pinMode(BIN2, OUTPUT);
    pinMode(PWMB, OUTPUT);
 
    Serial.begin(9600);                        // begin serial communication with the computer
    Serial.println("To infinity and beyond!"); // test the serial connection
    motorSpeed = 255;
    Serial.print("Motor Speed: "); // print the speed that the motor is set to run at
    Serial.println(motorSpeed);
}
 
void loop()
{
    motorSpeed = 255;
    if (digitalRead(7) == LOW)
    { // if the switch is on...
        rightMotor(motorSpeed);
        leftMotor(motorSpeed);
    }
    else
    {                  // if the switch is off...
        rightMotor(0); // turn the motor off
        leftMotor(0);
    }
}
 
/********************************************************************************/
void rightMotor(int motorSpeed) // function for driving the right motor
{
    if (motorSpeed > 0) // if the motor should drive forward (positive speed)
    {
        digitalWrite(AIN1, HIGH); // set pin 1 to high
        digitalWrite(AIN2, LOW);  // set pin 2 to low
    }
    else if (motorSpeed < 0) // if the motor should drive backward (negative speed)
    {
        digitalWrite(AIN1, LOW);  // set pin 1 to low
        digitalWrite(AIN2, HIGH); // set pin 2 to high
    }
    else // if the motor should stop
    {
        digitalWrite(AIN1, LOW); // set pin 1 to low
        digitalWrite(AIN2, LOW); // set pin 2 to low
    }
    analogWrite(PWMA, abs(motorSpeed)); // now that the motor direction is set, drive it at the entered speed
}
 
/********************************************************************************/
void leftMotor(int motorSpeed) // function for driving the left motor
{
    if (motorSpeed > 0) // if the motor should drive forward (positive speed)
    {
        digitalWrite(BIN1, HIGH); // set pin 1 to high
        digitalWrite(BIN2, LOW);  // set pin 2 to low
    }
    else if (motorSpeed < 0) // if the motor should drive backward (negative speed)
    {
        digitalWrite(BIN1, LOW);  // set pin 1 to low
        digitalWrite(BIN2, HIGH); // set pin 2 to high
    }
    else // if the motor should stop
    {
        digitalWrite(BIN1, LOW); // set pin 1 to low
        digitalWrite(BIN2, LOW); // set pin 2 to low
    }
    analogWrite(PWMB, abs(motorSpeed)); // now that the motor direction is set, drive it at the entered speed
}