Tuesday, September 29, 2015

ARTICLE #6 : Using L293D dual H bridge motor driver with Arduino

Introduction :


The L293D H bridge is a simple circuit that allows the Arduino board to control two independent DC motors (also steppers and servos). So, for example in a robotics application, by connecting each DC motor to a robot’s wheel you will be able to program it to drive forward, backward and turn left or right. You can also control the rotation speed of the motors by connecting them to the PWM pins instead of the digital pins as we will see in the present tutorial.

L293D features and pin description :





The L293D H bridge is a device designed to control two DC motors from 4.5V to 36V simultaneously. It can provide bidirectional currents up to 600 mA. All its pins are TTL compatible and it can work in a temperature range from -65°C to 150°C.

You can find all electrical features and other technical informations by viewing the datasheet. 





pinout description diagram :







  • L239D has two pin groups, pins from 1 to 8 used for the right motor and pins from 9 to 15 for the left one.
  • Vss is the device’s working voltage.
  • Vs is the motors supply voltage, it varies from 6V to 12V depending on the motor type.





  • Enable pins are used to activate the pin groups of each side of the L293D. If Enable pin is low, the output pin will be at 0 always. If it is high, output depends on input. In this tutorial, enable pins are connected to logical level one from the start.
  • Input pins :

Inputs 1, 2, 3 and 4 are connected to digital I/O pins of the Arduino. They are the pins that are used in the program code to control the output pins.

  • Output pins :

Output 1   (Pin 3)     ------- >   Negative Terminal lead of Right Motor
Output 2   (Pin 6)     ------- >   Positive Terminal lead of Right Motor
Output 3   (Pin 10)   ------- >   Positive Terminal lead of Left Motor
Output 4   (Pin 14)   ------- >   Negative Terminal lead of Left Motor


INPUT
1
INPUT
2
INPUT
3
INPUT
4
OUTPUT
1
OUTPUT
2
OUTPUT
    3
OUTPUT
4
MOTOR
OUTPUTS
MOVEMENT
RIGHT
LEFT
LOW
HIGH
HIGH
LOW
0
Vss
Vss
0
Straight
Straight
Forward
LOW
HIGH
LOW
HIGH
0
Vss
0
Vss
Straight
Reverse
Turn left
HIGH
LOW
HIGH
LOW
Vss
0
Vss
0
Reverse
Straight
Turn right
HIGH
LOW
LOW
HIGH
Vss
0
0
Vss
Reverse
Reverse
Backward




Circuit wiring :


     Project parts :


In this project, you will need :


  • 1 breadboard.
  • 1 Arduino Uno.
  • 1 L293D H-bridge motor controller.
  • 2 DC motors.
  • 9V battery.
  • Wire connectors. 

     Wiring :


As seen in the image below, there are two DC motors connected to the for output pins of the L293D H-bridge.each of these outputs is controlled by an input pin as described in the previous table, and we connect all these input pins to the microcontroller's pins.

Enable1 and enable2 pins, as well as Vss pin are connected to the 5V supply voltage of Arduino. Vs (pin No 8) is connected to 9V battery that is used to supply the two DC motors.

The for remaining pins are connected to the ground.




Code :


This program below makes the DC motors to both turn in one sens to perform the driving forward behavior of a wheeled robot, then turn in the other sens to perform a backward movement. When motors turn in opposite senses it performs a turn left or right movement. And finally it stops when both motors stop turning.

To be able to control the turning speed of DC motors, you just have to connect the 4 input pins to PWM pins of the microcontroller. you should also use analogWrite function in place of digitalWrite.

In a future development of this tutorial, I will enhance the previous system by adding a potentiometer that enables the user to increase or decrease the speed of these DC motors. I will add a picture of this project as well as the code when I finish writing it. Stay tuned! 


// Use this code to test your motor with the Arduino board:


// ---------------------------------------------------------- Motors pin declaration
int motor_left[] = {11, 10};
int motor_right[] = {9, 6};
int delay_sec = 10000;


// ----------------------------------------------------------- Setup function
void setup() {
Serial.begin(9600);

// Initializing motors as outputs
int i;
for(i = 0; i < 2; i++){
pinMode(motor_left[i], OUTPUT);
pinMode(motor_right[i], OUTPUT);
}

}

// ------------------------------------------------------------ Loop function
void loop() { 
  
drive_forward();
delay(delay_sec);
motor_stop();
Serial.println("1");

drive_backward();
delay(delay_sec);
motor_stop();
Serial.println("2");

turn_left();
delay(delay_sec);
motor_stop();
Serial.println("3");

turn_right();
delay(delay_sec);
motor_stop();
Serial.println("4"); 

motor_stop();
delay(delay_sec);
motor_stop();
Serial.println("5");
}

// ----------------------------------------------------------- Drive functions

void motor_stop(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], LOW);
delay(25);
}

void drive_forward(){
analogWrite(motor_left[0], HIGH); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], HIGH); 
analogWrite(motor_right[1], LOW); 
}

void drive_backward(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], HIGH); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], HIGH); 
}

void turn_left(){
analogWrite(motor_left[0], LOW); 
analogWrite(motor_left[1], HIGH); 

analogWrite(motor_right[0], HIGH); 
analogWrite(motor_right[1], LOW);
}

void turn_right(){
analogWrite(motor_left[0], HIGH); 
analogWrite(motor_left[1], LOW); 

analogWrite(motor_right[0], LOW); 
analogWrite(motor_right[1], HIGH); 
}

No comments:

Post a Comment