Let's Make Robots!

Control your motors with L293D

guibot's picture
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.
Aniss1001's picture

Just found this tutorial and thought I'd share it. It explains how the L293D works in details so even noobs (like me) can't possibly misunderstand it.

Plus the accent is great :)

Aniss1001's picture
Now I gotta know for sure: Are the o.1uF caps tantalum OR ceramic?
Revenant.Revival's picture

The SN754410 is a good chip, it is pin to pin compatible with L293D, So you can put it in the place of the L293D.

Rudolph's picture

Remember though, the diodes in the SN754410 are for ESD protection, they are not flyback diodes like the L293D.

That said, it apparently works for some people without extra flyback diodes.

Revenant.Revival's picture
What does ESD stand for?
TeleFox's picture
ESD = ElectroStatic Discharge, i.e. 'static electric shock'.
Aniss1001's picture
Sure sounds like great fun to me :)
Aniss1001's picture

I'm planning to use the same H-bridge for a robot (my 1st robot and 1st H-bridge), so I'm very greatful for this istructable. Thanks :)

But excuse me for asking a noob question: Are ALL those capacitors (9?) really necesary just to control to DC motors? Or is it because you're using UNREGULATED power? Or some other specific circumstances?

jklug80's picture
required? No. suggested? yes! They suppress noise and help with the discharge when the magnetic field collapses after the motors are shut off. (Note: In not an EE major so someone may prove me wrong but that is my understanding.)
Aniss1001's picture

...I'm quite a noob in these matters but I am aware that one should use caps to stabilize the current flow. But still 9 (!!!) that's quite a bit...

Here are a few similar setups and they often do use caps, though not allways, and none of them comes close to 9:

www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1255204188

www.me.umn.edu/courses/me2011/robot/technotes/L293/L293.html

itp.nyu.edu/physcomp/Labs/DCMotorControl

www.ladyada.net/make/mshield/

akashxav.com/2009/04/18/arduino-l293d-dc-motor/

www.solarbotics.com/projects/circuits/2007/07/circuit-l293d-mini-sumo/

Just to name a few :)

Aniss1001's picture

Just noticed .. the 2 large caps are for the voltage regulator. A typical setup.

So we're down to 7 caps which is still quite a lot..

guibot's picture

You can build your circuit without the caps. But as jklug80 said, they are ment to stabilize voltage spikes.

i get this circuit schematic from the ladyada motor shield.  ;)

Aniss1001's picture
I was planning to use a cap or two. However 7 seems like a bit of a hassle, so I think I'll try it with less for starters and see how that goes. Did you start with 7 or is it because you had problems when using less?
guibot's picture

I started with just the L293D, the motors and the microcontroller, and it works ok  ;)

I used the caps because they were on the schematic and I wanted to learn how to use them on the h-bridge. I is just a matter of getting used to, now, I can't think in making this circuit without the caps.  :)

Aniss1001's picture
It seems that the small caps (0.1uF) are depicted with the symbol for electrolytic caps, but they sure look like ceramic caps on the fotos? Correct me if I'm wrong?....
robologist's picture

No plus sign = ceramics probably. Note the plus on the actual electrolytic. Sometimes folks use whatever symbol is handy (in graphics) for the device they have.

 It does appear they have a place for caps on the LadyAda Motorshield between ground and motor outputs, but they appear to not be populated. I'd probably omit those 4 too.

Aniss1001's picture
And the problem with the symbols is that the description I posted above (as well as many others) says the symbol with a plus sign is an ELECTROLYTIC cap. That's apparently incorrect. It simply means a POLARIZED cap? Furthermore the ones with no plus could mean both a tantalum or ceramic one, allthough tantalum caps are polarized too? Oh well I'll get used to it sooner or later...
TeleFox's picture

You can also get non-polarised electrolytics, which naturally have no (+) sign at all =D

Aniss1001's picture
You're just saying that to confuse me :D
TeleFox's picture
There are other non-polarised types too, such as polyester, aluminium foil, green caps...
At the end of the day only a few things really matter for most uses: capacitance, maximum voltage and polarity. Sometimes you'll need to know the equivalent series resistance, leakage rate, or other details but normally you don't worry about them.
Aniss1001's picture

"There are other non-polarised types too, such as polyester, aluminium foil, green caps..."

doh.jpeg

TeleFox's picture

They're probably Tantalum caps, judging from the appearance, value and polarisation.

Aniss1001's picture

HOW can you tell it's a tantalum cap judging from the appearance, value and polarisation?

According to the diagram they're NOT polarized (tantalums are). The value could be both ceramic and tantalum (or am I wrong?). And the appearences of the two types are extremely similar...

Aniss1001's picture

Damnit I think you're right. The 3rd kind I allways forget. Another noob mistake on my part. Man it isn't easy learning electronics. A lot of incomplete or wrong information around. Guess I'll have to find a better symbol list.

Besides these things look VERY similar to the ceramic ones :|

i connect everything like on the first breadboard-picture.

whit one motor everything is ok. but with two there is this problem. and i dont think it is the batterie cause it works but not in different ways.. together they drive in the same direction but in diffent ways not. in the serial monitor left and right doesnt appear ???

guibot's picture
how are you powering your motors and microcontroller?
Tomorow I will buy a batterie case... then I can give you more information.. until now i only powered the µC over usb.. is this a fault?
guibot's picture

Usb gives you 5V at 500mA (i guess), it might not be enough to power your motors.
9v little batteries aren't good for motors as well, you can use common RC race packs, or a couple of AAA bateries.

Take a read into the following posts:

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

 

 

so. i build the circuit on my breadboard. The easy one only works, if the upper connection from pin1 to pin16 on the L293D is Not connectet. otherwise the motors only drive forward. so today i tried to build the correct implementation but it is still not working until i pull the capacitor on pin16 out of the breadboard.

The Red "Dot" is the place where the capacitor should be. But when he is there the motors only beep. did i something wrong?

L293D


http://www.freewebs.com/sikspack1/DSC00638.JPG <- this is the link to the bigger picture.

 a.. why is on the pcb two more capacitors and a voltage regulator?

RobotFreak's picture

Do you have made connections between the upper and lower Power and GND rails. These are normally not connected internally. 

The voltage regulator in guibots schematic is only for a separate power supply for servo motors.

Thats the Problem! I can't. if i connect the upper gnd to the lower gnd.. it's ok and everything is working but when connecting the upper 5V to the lower 5V the motors only work in one direction. and if i don't remove the capacitor (red marked in the picture) the motors are only peeping.


Why there are 2 capacitors? when i remove the 100µF-capacitor and the 5V-5V-connection it works. but is it right!?!?

3235658022_f78495fddd.jpg

RobotFreak's picture

Did you check the polarity of the capacitor?

The L293D has 2 Power supply pins. Pin8 is normally used for the unregulated motor power supply (directly from the battery). Pin16 is for the regulated (5V) logic power supply. You can also use a single regulated power supply for both, this is not recommended, but for testing its should be ok.

For a real robot you should supply power to the Arduino board over the external power connector from a battery. The unregulated battery power is than available on Pin Vin on the Arduino.

 

 

yes the polarity is right. i've replaced the 100µF to a 0,1µF Capacitor and now its running unless you connect pin1 to pin16??? i don't know.


another questino: do i need a Volatage regulator if i only want to use the two motors? and do i need 1A oder 1.5A?

 

guibot's picture

Did you tried without the caps?  How does the motors work without the caps. Try first without the caps and then you can try with the caps, this is easier to debug.

The voltage reg is..  if you motors runs at 6v you should connect your power source (battery) directly to the pin8. Then because your microcontroller (Arduino) and the chip L293D operates at 5V you should power them with 5v. This is where the voltage reg comes in.

let's take the following case scenario:

9V little battery powers the arduino through the power plug. You connect the pin1, 8, and 16 on the L293D with the 5V that comes out from the arduino.

Then your motors operate at 7.2V, you connect V+ from the 7.2V battery to the pin8 on the L293D and then you have to connect the GROUND of that battery to the rest of your circuit.

hope this helps 

 

. Without the caps its the same thing. it only works with one motor. if you connect both they only drive one directon and stop. but they sould both drive 4times in different directions and then stop. on the serial monitor you can see Forward Backward forward backward. while it is writing backward the motors stop and during forward they work. hm. did i understand it right: i have to connect a batterie (6V or whatever the motors need) between pin8 of L293d and V+?

 

Thanx

guibot's picture

"... i have to connect a batterie (6V or whatever the motors need) between pin8 of L293d and V+? ..."

The battery power goes DIRECTLY to the pin8, nothing else here.
Than the other pins on the L293D gets regulated power (5V from the arduino or from an external voltage regulator)

 

You have to check what are your motors needs.
How are you powering the motors and the circuit??

 

RobotFreak's picture

I even don't know what is wrong with your schematic. Maybe you better take a look for the Aduino motor sheet. This is more Arduino specific.  Arduino - ArduinoMotorShield or http://blushingboy.net/p/motorShieldV3/

Yo don't need a voltage regulator. You can use the internal Arduino regulated power for the L293 logic. 

Can I 'stack' these chips , put several on top of each, soldering pin1 to pin1 etc  to boost the current output?

 

Just a quick 'dirty thought' for Mr Basic

 

Pete

guibot's picture
I think you can, but I never tried.. it is called "piggyback", ladyada talks about it here
Edgee's picture
tutorial mate, id been battling for ages trying to think of a decent way to integrate a power supply with the motor driver, but my electronic skills are a little lacking at the moment. Im gonna try and make one this evening for guilbot. Will post a pic and say thanks later :p 
guibot's picture
Hello Edgee, sorry for the dealy answering to your comment, my email box has been chaotic since I've been on vacation.. Good to know you find this tip useful  :)    Did you manage to build your motor driver already?
TotoroYamada's picture
I just found this tuto, it's really great thanks! I have a L293D that doesn't fit in my 18x project board, but now thanks to you it won't be useless :D
mintvelt's picture

Nice board!

I thought the L293D chip is actually 4 independent sets of 4 pins where each V+ pin provides power for one of the motor outputs. You have regulated power to 3 of them and unregulated power to one.  Why is that?

This is the diagram as the chip is connected on the picaxe 28 board. As you can see, all V pins are connected to V2

L293D.jpg

robologist's picture
Check the datasheet for the L293D. You'll see pin 16 is Vcc1 logic power, and pin 8 is Vcc2 motor power, with pin 1 being the enables to 2 outputs and pin 9 being the enable for the other 2 outputs. Pins 1 and 9 generally are spec'd as 5 volt logic inputs, not power, and the Vcc1 logic power should also be 5 volts. Vcc2 can be anything from the level of Vcc1 on up to 36 volts.
mintvelt's picture

How about that. I build edward from the schematics in the picaxe manuals. I just figured the 5V indications where a typo. Edward runs 7.2V on all corners of the motor driver chip and it still works.

Well, I wont make another board without first checking all the real datasheets properly. 

 

 

guibot's picture
I´ve made a major update on this. It is simpler, more accurate according the datasheet, and now I mention the use of a voltage regulator, capacitors and unregulated power supply.
robologist's picture
Pretty cool additions, and better clarification, looks good!
guibot's picture
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