Let's Make Robots!

Control your motors with L293D

UPDATE

***********************************************************************************************
update 26/4/09
***********************************************************************************************

My 1st instructable  :)

http://www.instructables.com/id/HiTec-Servo-Hack/

***********************************************************************************************
***********************************************************************************************

After long research and trial and error,  I´ve came up to a new walkthrough regarding this nice chip, the L293D.

Each project is one project and each one has its own unique power configurations, so you must be aware of the best battery choice and how to distribute voltage through your robot.

I strongly advice you to read the following articles:

Picking Batteries for your Robot
Once you’ve decided on batteries, how do you regulate the voltage

************************************************

L293D gives you the possibility to control two motors in both directions - datasheet

************************************************

The L293D Circuit:

Basic Implementation:

This is the most basic implementation of the chip.

As you can see, a 5V Voltage Regulator is between the battery and pins 1, 9, 16.

Pin 8 gets power before the VReg, if your motor needs for example 6V you should put 6V directly in this pin, all the other pins should not get more than 5V.

This will work with no problem at all, but if you want to do the right implementation take a look at the next example:

3235657956_b3be2b4f2f.jpg?v=0

3262168342_ae12307934.jpg?v=1240780647

This is the correct Implementation (with the capacitors), and note that pin 8 is feeded by unregulated voltage. This means that if your motors need more than 5V, you should power this pin with that amount of voltage, and the rest of the circuit with 5V.

3235658022_f78495fddd.jpg?v=0
The capacitors stabilize the current.

The same circuit on a breadboard:
3252941552_2f4919475f.jpg?v=1240780044

Soldered on a pcb and ready to go:
3234563157_780312a389.jpg?v=0
3451483356_2fdf26be19.jpg?v=0

3257970545_12de4f710e.jpg?v=0

This is the back of the circuit, click for high resolution photo.

***********************************************************************************************
CODE
***********************************************************************************************

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

// if you need PWM, just use the PWM outputs on the Arduino
// and instead of digitalWrite, you should use the analogWrite command

// —————————————————————————  Motors
int motor_left[] = {2, 3};
int motor_right[] = {7, 8};

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

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

}

// ————————————————————————— Loop
void loop() {

drive_forward();
delay(1000);
motor_stop();
Serial.println(”1″);

drive_backward();
delay(1000);
motor_stop();
Serial.println(”2″);

turn_left();
delay(1000);
motor_stop();
Serial.println(”3″);

turn_right();
delay(1000);
motor_stop();
Serial.println(”4″);

motor_stop();
delay(1000);
motor_stop();
Serial.println(”5″);
}

// ————————————————————————— Drive

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

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

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

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

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

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

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

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

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

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

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Thanks   :)
Hi, I am unable to open the sample code. What program is compatible in opening it? I am using MPLAB to create the code for my microprocessor and am using the PIC18F1220. The h-bridge I am using is the L293DNE but have added diodes where they are needed. I will need to use PWM as I am driving a twin motor gearbox with differential drive. Any advice would be greatly appreciated! Thanks!

This is Arduino specific code.  It is similar to C but with some slight variants.  You would need to download the Arduino Development environment from Arduino.cc

Hey Sir,

 can you please let me know if the l293d works only with PWM or if it can also be used with an SPDT switch.

Thanks in advance.

Yes, the chip will work with a switch, but of couse you would not get any speed control.  In fact, there wouldn't be much point in doing it that way -- you could simply connect the motor(s) directly to the switch.  But, if the switch is too small to take the current of the motors, of if you want to have long wires to the switch, or for some other reason, you could generate the logic levels that drive the L293D from a switch.

 

I read the datasheet and I'm afraid to understand (sorry, I'm french and my english is poor...) that the H-Bridge can only be used on motors with at least 4,5V voltage. Could you confirm? Second problem, my motors ask about 1A per motor, and the H-bridge can drive currents of up to 600 mA, so I'm afraid the motor ask too much and kill components, right? The L293D seems to be better to resolve that second problem, right? Has the L293 to be powered separatly from motors? Is it possibe to power it through Arduino at 5V? Sorry for all of that question, I'm new in electronics/robots...

Yes, you are correct, the minimum motor supply voltage for the L293 (and L293D) is 4.5V.  I'm having similar problems with the L298 and a car chassis that uses motors that go way too fast at 6V -- they start to turn at about 1V.

As for the current, again, you are right .  The L293/L293D will only supply 0.6A, while your motor requires 1.0A.  The L298 is able to supply more current, up to 4.0. Once again, a minimum motor supply of 4.5V.

It is always best to keep your motor supply separate from your logic (Arduino, PIC, etc.) supply.  Just connect the grounds (0V) in one place.  Motors will draw large currents and cause voltage variations and noise on the power supply.  If the logic is powered separately, then there's less chance of trouble.

I've just checked my l293d and how low can you go on the vcc2. I have vcc1 = 5V and hooked the vcc2 to regulated power supply and went down as low as 2.7V. Below that motors stop. I have 3v motors (tamiya double gearbox mabuchis) and I wondered whether L293d can be good to drive them. I think I'll use this solution 5v fed through a diode to vcc2 giving 4.3 V and pwm not exceeding 190/255. This gives ca 3.0V rms relative to ground on positive motor terminal. The actual voltage across two terminals is higher (so it seems not the rule of thumb that l293d eats up 1v off vcc2 - depending on how you measure it, but in worst case it eats almost nothing - i checked that on the scope and peak-to-peak voltage on the motor is almost the vcc2, at least in the 3-4.5v voltage range, where I checked it).

 And I thinkg the diagram on top of the page should indeed be corrected, as powering motors from the same voltage the uc is powered is asking for trouble. Not every controller board uses big 7805 1A regulator. Some have 78l05 or other tinier SMD versios, which will not handle motor load.

Thank you very much for your replies! It's more much clear for me now. So I'm going to study what the L298 can give to me...
I have used the L293D on a Picaxe28x1 board with 3V motors. Since the L293D eats up about 1V, feeding it 4,5V will work, because it would end up being about 3,5V. If 600mA is too low, take a look at the L298. There are separate power pins for the chip and the motor, but there is nothing wrong with connecting them to the same power source, as long as the power is within the specs of the L293D and the motor. I wouldn't recommend using the 5V output from the Arduinio for powering the motors, since I don't think you can draw that much power from the pin. I tried using it for two servos once and it couldn't deliver the power.