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.
// PIN VARIABLES// the right motor will be controlled by the motor A pins on the motor driverconst int AIN1 = 13; // control pin 1 on the motor driver for the right motorconst int AIN2 = 12; // control pin 2 on the motor driver for the right motorconst 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 driverconst int PWMB = 10; // speed control pin on the motor driver for the left motorconst int BIN2 = 9; // control pin 2 on the motor driver for the left motorconst int BIN1 = 8; // control pin 1 on the motor driver for the left motorint switchPin = 7; // switch to turn the robot on and off// VARIABLESint motorSpeed = 0; // starting speed for the motorvoid 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}