Let's Make Robots!

Are You Wondering How to Hook Up a Motor Driver IC chip (such as an L293D) ?


  **Click the picture to show it bigger, and then click that picture again to make it big enough to read the fine print**

ADDED (06MAY2012) This is the motor drive circuit exactly as I built it for my newest lttle SHR-like robot.

The above configuration using the L293D is a good way to prevent shorting out the power supply as can happen in the circuit below if you make inputs A and B high at the same time. The Integrated circuit can supply 600 mA current per channel (and twice that in a non-repetitive peak).  It also contains internal protection against reverse spikes such as are generated when driving an inductive load. 

With this configuration, in adding the extra transistors I have changed the function of the inputs. Power to the motor is turned on or off with one lead and You set forward or reverse with the second one. While this feature is not essential, some people may like this better. It is still the same 2 wires per motor (as below), but their functions have changed. Some of my older drawings using the L293D just tied the Enable leads to +V (always enabled), but by using the enable leads you can switch the output to a high-impedance state when the on/off lead is low.

 

Below is a common driver circuit I pulled )almost at random) off the internet. (If anyone is upset that I posted something I did not draw myself, I will remove it; there are many more examples.) I have even seen a video for that particular 6-transistor circuit showing how to put it together, but I do not remember them saying, "Oh by the way, don't turn both inputs on at the same time or you could burn down your house." (Ok, it may not be THAT bad, but the transistors *will* burn up if the battery does not go dead first.)

This does work but there is always the danger of accidentally turning both inputs on at the same time, and *P*O*O*F*.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

 

Thank You for detailed explaining.

Sorry about my english and low level electronics.. I was wrong about transistors. So as I understand.. I don't need any capicator with external battery pack in Your schematic too?

And I have one more question.. I have two dc motors from RC car.. they have 2 (0.1uF) ceramic capicators soldered on them, so I can let it be right? (photo of dc motor: http://cl.ly/3B0H2s261k3o3P3F2q2I )

I think I will use Yours schematic because I like it more.. More simple.. And there is no chance to make mistake in result to short circuit..

p.s. As I understand You are going to write code with regulated speed by PWM pin..?

 

You can leave the 0.1μf capacitors on the motors. They will not hurt anything. They are not necessary, but may help in one way. DC motors can generate electrical noise. Even though there are diodes in the L293D chip, the extra capacitors being right at the motors could help and do not cause any problem by being there.

Concerning the code. On the little robot, I will probably not use pwm signal, --only dc. If I were using stepper motors or servos, I would use pwm, but this one I will not bother.

Generating pwm code is not that hard though.

_________________________________________________

For picaxe it could either be: pulsout (pin number), (timing)

Here is an example for getting an input value from a potentiometer and sending it out as a PWM signal

for b0 = 1 to 10
readadc 0,b1               ; read value on pin0 into variable b1              ;
pulsout 4, b1               ; sends a pulse on pin 4 consistant with potentiometer input b1
pause 20                     ; you want to pause between pulses at least 10 mSec per cycle)
next b0

or you could also use the Servo or Servopos commands. Servo and servopos automatically send pwm signal at whatever value you specify. (Even though the name is "servo", it still sends pwm signals to anything you have hooked to that pin.)

__________________________________________________

For an Arduino (AtMega8, 28 pin chip) there are 6 pins which can be PWM or for an Atmega 1260 there are 12.

Here is a sample Arduino program for getting an input value from a potentiometer and sending it out as a PWM signal:

// Code Example – Analog Input – PWM Output
// Read potentiometer from analog pin 0
// PWM output on pin 3 will be proportional to potentiometer input (check with voltage meter).
int pot_val; // use variable "pot_val" to store the value of the potentiometer
int pwm_pin = 3; // name pin Arduino PWM 3 = "pwm_pin"
void setup(){
pinMode(pwm_pin, OUTPUT);
}
void loop(){
pot_value = analogRead(0); // read potentiometer value on analog pin 0
pwm_value = pot_value / 4; // pot_value max = 1023 / 4 = 255
if (pwm_value > 255){ // filter to make sure pwm_value does not exceed 255
pwm_value = 255;
}
if (pwm_value < 0){ // filter to make sure pwm_value does not go below 0
pwm_value = 0;
}
analogWrite(pwm_pin, pwm_value); // write pwm_value to pwm_pin
    //   analogWrite is where the PWM signal is actually sent out
}
// end code

Thank You again.

I tried it, and it works great.. :)

But there is one strange thing.. Forward is ok, backward is much more slower (half of speed).. (I used external 9V) But as I understand it is because of capicators on motors, right? :)

And the last question (I hope so :) ).. I'm looking at your schematic.. And as I understand transistor's leg E of 2N3904 should be connected to the ground, right?

p.s. I wrote code for testing purpose on Arduino.. If anyone needs it - http://cl.ly/3o1b3k1R2g2o0r0I2G0k

Later I will post on comments finished code with a sensor..

As ignoblegnome said, the capacitors on the motors do not affect the motors normal operation. All they may do is help syphon off electrical noise generated by the motors. If the motors turn faster one way than the other, the problem is not the capacitors.

The emitters of the NPN transistors are hooked to the negative side of the power. They do not have to be connected to a rod into the Earth. The term 'ground' is a broad convention dating back to the early days of radio when it was necessary to have a rod into the earth to help pick up distant weak signals.  That is a whole separate topic.  In this case it only means the negative side of the battery or power supply.

Also the emitter of an NPN transistor does not always have to be hooked directly to the negative side of the supply. This is what is called a "common emitter" type circuit.

I wrote a bit about transistors on other pages.  You may wish to read:

http://letsmakerobots.com/node/28841  Page 1

http://letsmakerobots.com/node/28927  Page 2

http://letsmakerobots.com/node/29007  Page 3

[ I did not finish the discussion with the info on FETs as yet, but hopefully I will get to that soon.]

Thank You, I will read it.

I do not believe that the difference between your forward and backward speed has anything to do with the capacitors on your motors. Those are just there to cut down on electrical noise generated by the motors. 

However, DC motors are usually not perfectly symetrical. What I mean is, they will often run faster in one direction than in the other. That is probably your issue.

BTW, an 9V alkaline battery (if that is what you are using) does not have a lot of current capacity. You may find the battery runs down very quickly. You can double up two 9V batteries in parallel (positive to positive, negative to negative) to double the capacity. Or you could go with a racing pack or even a bunch of AA batteries. 

Good luck!

Thank You for explaining. I think You was right about battery.. It's current downs too quick.. I changed to new battery and everything goes much more better.. 

So I will try Your suggestion to use few 9V batteries. :)

Here's a design that is a lot better, it won't allow you to switch both inputs to high at the same time(blatantly stolen from http://pyroelectro.com):

I've based my own H-bridge on this design as well.

I do not understand. On the one hand you say it is better, and on the other, you mention that it is like those others in that you cannot allow both inputs to go high at the same time. I notice this one also does not have any reverse voltage protection diodes.

That was my point as to why I felt these were poorly designed.

When the collectors are wired together, both can be on at the same time.

If the emitters are wired together instead, as per my earlier design, then even if the inputs are both high at once, there will be no short-circuit. Here is the one of my design that illustrates what I mean. I had an earlier blog page that described this.

http://letsmakerobots.com/node/27578

Good one Dan, collected!