In this post, we are going to look on how to control DC motor with L293D by using Arduino. L293D is a Quadruple Half H Drivers which is designed to provide bidirectional control of motors up to 1A at voltages from 4.5V to 36V.

l293d

Datasheet for L293D. Always study the datasheet for detailed informations. 😀

Pin diagram for L293D

Capture

Explanation of the pin

L293D

How to control with L293D (taken from datasheet)

Capture

Let us look into detail how the H bridge works and enable us to control the motor bidirectionally.

motor 3

You can imagine a H-bridge with 4 individual switches, A, B, C, D. The motor is connected in between the four switches. Also, we know that for a DC motor, the motor will rotates when a voltage is applied to the motor terminal. When we reverse the voltage applied, the motor will rotates in an opposite direction. Now, we will investigate how the H bridge allows the motor to rotate bidirectional.

motor 1
Switch A and switch D closed
motor 2
Switch B and switch C closed

Conditions:

1. When switch A and switch D are closed, the motor will rotate clockwise.

2. When switch B and switch C are closed, the motor will rotate counter clockwise.

3. When switch A and switch B are closed, the motor will stop.

4. When switch C and switch D are closed, the motor is free to move.

Besides providing directional control, L293D also allows you to control the speed of you motor by the enable pin. If speed control is not needed, all you need to do is connect the enable pin to 5V to enable respective driver. However, if you would like to control the speed of your motor, connect the enable pin to a PWM pin. By altering the voltage input to enable pin, you can control the speed of your motor.

Enough of explanation, lets work with L293D and DC motor.

l293d1
Circuit Conenctions

Parts:

1x Arduino

1x DC motor

1x L293D motor driver

1x Variable resistor (speed control)

1x Tact switch (direction control)

motor6
DC Motor

This is the DC motor that I am going to use in this demo. Notice that there are 2 pins red and black. The polarity is not important as reversing the polarity will results in opposite directions.

motor5
This is how the circuits connection looks like.
motor4
Variable resistor for speed control.
motor3
Tact switch for direction control.

Notice that I am not using any external pull up or pull down resistor. The connection of switch to Arduino is simple, one pin to the Ground and the other pin to Arduino input pin. Further details on internal pull up at the code section.

Code example:

#define enable 9
#define input1 10
#define input2 11
#define button A1
#define pot A0

boolean rotation=0;
int mtr_speed=0;
int current_state=0,previous_state=0;

void setup()
{
 pinMode(enable,OUTPUT);
 pinMode(input1,OUTPUT);
 pinMode(input2,OUTPUT);
 pinMode(button,INPUT_PULLUP);
 pinMode(pot,INPUT);
}

void loop()
{
 mtr_speed=analogRead(pot);
 mtr_speed=map(mtr_speed, 0,1023,0,255);
 analogWrite(enable,mtr_speed);
 current_state=digitalRead(button);
 if(current_state==LOW&&previous_state==HIGH)
 rotation=!rotation;
 if(rotation==0)
 {
 digitalWrite(input1,HIGH);
 digitalWrite(input2,LOW);
 }
 else if(rotation==1)
 {
 digitalWrite(input1,LOW);
 digitalWrite(input2,HIGH);
 }
 previous_state=current_state;
}

Explanation of code:

pinMode(pin,INPUT_PULLUP)

– This actually declaring the button as INPUT and at the same time enable the internal pull up resistor. By enabling internal pull up, the state of button is always HIGH. When the button is being pressed, the state will change its transition from HIGH to LOW. Input Pull Up from Arduino official website.

map(value,fromLow,fromHigh,toLow,toHigh)

– This function is used to remaps the from one range to another. The reason I am using this is because I am reading the analog value from variable resistor. Recall that Arduino Analog to Digital (ADC) has 10 bit resolution, so it can goes up to 1023 from 0. However, PWM output is only 8 bit resolution (max 255). So I remap the value to control the PWM.

analogWrite(pin,value)

– This allows you to write an analog value to PWM pin. Check on your Arduino for PWM pin as not every pin has PWM function. PWM is in 8 bit resolution.

The program keeps on checking for value from variable resistor and control the enable pin of L293D. Besides that, the program also check on the state of button, whenever there is a HIGH to LOW state change on the button, the direction of motor will be toggled. The direction of motor is control simply by digitalWrite() of the 2 input pins.

Have a look on this video for this tutorial.

Advertisements