What am I doing to this transistor and motor?

I have the worst time with transistors. I seem to have so much trouble with them. Basically I need to use a microcontroller to control a DC motor. I have a picture of what I am trying to do here:

http://img502.imageshack.us/img502/6317/sdmbtransistorturnonmotor2uu.png

The +5V in the picture will come from the microcontroller and the Vcc is some voltage that will drive the motor. I have been testing this out on a breadboard as I don’t have the microntroller on me and I have been getting some weird stuff.

Basically I want to be able to write a ‘1’ (+5V) on a specific pin on a microcontroller and I want to be able to turn on a DC motor with the voltage Vcc (or close to it due to a small voltage drop across the transistor). The thing that I can’t figure out with what I have is that one (or I guess me) would think that as Vcc is increased the voltage across the motor would increase when the transistor is biased so it would turn faster. However, this is exactly the opposite of what I am getting. The smaller Vcc is the faster the motor turns. Does anyone know why?

Also if anyone has a better way of doing this then I would appreciate the advice. Thank you.

My only guess is that you are abusing the transistor. What type is it? Your problem is that you are biasing that transistor into a mode that is not normal. A bipolar transistor normally biases up with only about 0.7 volts between the base and emitter. Since you have 5 volts there the junction will exhibit some unwanted behavior and the transistor will eventually burn out. Trusting that this is an NPN transistor you probably want a resistor between the base and the +5 volt source, and its value would depend on what the expected base current of the transistor is.

I would suggest that you use a MOS transistor, since it will interface directly to +5v logic and can handle a lot of current. I would suggest a IRL540N from International Rectifier. It can handle 100V on the drain, has a 5V compatible gate, and a very low on resistance to handle motor loads without heating up much. It will handle 25 amps at 100C. They are not terribly expensive and are available from Digikey, Mouser, Avnet, and many other fine electronics distributors.
-J.Bartlett, MSEE

That’s a fairly common circuit for driving stuff from a microcontroller. You should have a resistor between the microcontroller output (where you have a constant +5 right now) and the base of the transistor. The value of the resistor isn’t critical. You want the resistor big enough to limit the amount of current going in through the base, but small enough that it still allows the transistor to go completely into saturation. If you use too big of a resistor then the transistor won’t fully saturate and it will be operating in what is called its forward active region, where it’s going to act like a linear amplifier. This will generate a lot more heat in the transistor, will be inefficient, and will make the motor run slower. You want to keep it in saturation. You can play around with a pot to get the right value. Once the motor won’t go any faster you’re in the right area.

This also requires that the transistor be biased such that Vcc is higher than the voltage going into the base. In other words, you want Vcc to be at least 5 volts. If Vcc is less than 5 volts then the transistor is going to end up not operating like you’d expect, which is what I’m guessing you are doing since you are seeing weird things and you didn’t mention exactly what values you are using for Vcc.

To vary the speed of the motor, switch the microcontroller output on and off really fast (at least 100 times a second) and vary the on time vs. the off time. This is called pulse width modulation and is a good way to drive motors, since it keeps the output transistor mostly in either cutoff or saturation (i.e. all the way on or off) which is much more efficient and will make your transistor run a lot cooler.

Note that all of the current going through the motor also goes through the transistor, so make sure you have a big enough transistor for the motor you want to drive.

It’s something from a RadioShack kit labeled “C1740.” I believe this is the datasheet for it if you are curious:

http://www.datasheetcatalog.com/datasheets_pdf/Q/6/2/7/Q62702-C1740.shtml

I was actually attempting to use Vcc as 1.5V, 3.0V, and 4.5V. I was doing this because I had a very small motor that I was using this circuit to play around with and I didn’t want to mess it up.

I am actually using PWM on some drive motors (with H-Bridge) where we need to control the speed; however, these motors only need to be turned on for a certain amount of time (2 seconds maybe) and we don’t need to control the speed.

So basically I should include a potentiometer to get it more exact between the output of the microcontroller and the base of the transistor. Then so long as my transistor can handle the power of driving the motors I should be okay.

This isn’t so much related to the first question but it does relate to transistors. I understand that a transistor can be used as both a switch and an amplifier. When using it as a switch you input a value to the base so that the transistor is biased and will allow current to flow from the collector to the emitter. So how exactly does amplification work? The current from the collector to the base (for NPN) is proportional to the base current. So would you input the signal you want amplified into the base and output the amplified signal from the collector to the emitter by having Vcc larger than the amplitude of the input signal? I think I need to head down to my local library and pick up a good book about this.

Thanks again for everyones help! :slight_smile:

This is how we run variable speed motors on our cars.
If you are just going to use the microcontroller to connect the ground, and vary the speed via the power supply, why don’t you find a relay that trips on 5V and leave the transistor out of the circuit? Simple, cheap, and less voltage drop.
Course I’m just a guy that dinks around with stuff and am not qualified to carry these other guys soldering irons. :slight_smile:

Honestly I have heard of a relay but didn’t know exactly what it did. I will definitely look into them and see if they can accomplish this task more easily than using transistors. Thanks

A relay is simply an electromagnetic switch. It has a coil of wire wound around a magnetically-permeable core (usually a soft ferrite) which comprises an electromagnet. It is arranged in such a manner so that when sufficient voltage is applied to the coil, the magnetic field thus produced will pull down a movable switch contact. The switches can have almost any configuration from a simple on-off SPST to complex multipole, two-position units. The purpose of a relay is to allow a small signal to control a much larger one. They are used, for example, in automotive starters. Starter motors draw a very large amount of current. If this current were switched directly by the ignition switch, it would burn out the contacts. On the other hand, if the contacts were made to handle the current, then the switch would be very large and wouldn’t fit where designers wanted it–which has to be someplace not easily accessible, so that it’s harder to steal the car. So, the ignition switch is instead made to control the coil of a high-current relay the switch of which, in turn, is made to control the starter motor.

In a typical configuration like what you have, let’s assume you start out with no current going into the base. This is what is called cutoff, and there will be no current from the collector to emitter. As you start increasing the current going into the base, you’ll start to get current flowing from the collector to the emitter. This gets you into what is called the forward active region of the transistor. For a while, as long as you increase the current going into the base, you’ll get a corresponding increase in the current flowing between the collector and emitter, multiplied by a factor which is called the gain of the transistor (also called the beta). Eventually, you reach a point where the transistor saturates, and no matter how much you increase the current going into the base you don’t get any more increase in current from the collector to emitter.

If you want to operate the transistor as a switch, you want to keep it all the way in the cutoff region when it is off, and you want to drive it all the way to saturation when it is on.

If you want to operate the transistor as an amplifier, you want the exact opposite. You want to avoid saturation and cutoff, and keep the transistor operating in its forward active region. What you want to do is put a bias current into the base (which can be set up with a couple of resistors) so that when there is no signal, the transistor is right smack in the middle of its forward active region (called the transistor’s Q point, or quiescent point). Then, if you add positive current to the base, the output of the transistor will increase proportionally, and if you add negative current into the base, the output of the transistor will decrease proportionally. If you look at the voltage output of your circuit, at the Q point, the output would be half way between Vcc and Gnd, and as you vary the current going into the base, the output will swing up and down around this point.

Most microcontroller outputs can’t drive a relay directly, because they can’t provide enough current for the coil. You’ll usually have a transistor, similar to your circuit, driving the relay coil. The important thing about relays is that when the relay switches off, all of the energy that was stored in the coil has to go somewhere. If you don’t give it someplace to go, it will likely feed back into your microcontroller and make it go poof. Fortunately, the simple solution for this is to stick a reverse biased diode across the relay coil. When the relay is on, since the diode is in the reverse direction, it doesn’t do anything. When the relay turns off, all of the energy in the coil discharges through the diode, instead of frying your circuit.

By the way, if you are going to do some reading, I would suggest “The Art of Electronics” by Horowitz and Hill. If you are going to tinker around with electronics, it’s well worth the investment of buying your own copy. It covers a wide variety of topics, and is well written such that a relative newbie can still understand most of it, though in places it does get a bit above the head of your average novice. It also has examples, and even shows examples of common misconceptions and mistakes that people make.

Thanks everyone so much for your help! This has really helped me. I will definitely take a look at that book.

I second the book recommendation, I’m an electrical engineer designing electronic test equipment, and I keep a copy on my desk to refresh my memory on certain types of circuits that I don’t use daily. It shows both good ideas and bad ones and tells you why you should or shouldn’t do things.

All good points. I’d go for the transistor approach, as they’re much cheaper than relays in this application. If there’s any sort of unpluggable connection between the transistor and the microcontroller, then add another resistor between the base and ground to stop the input floating around if unplugged. About 10kohm should do it.

A couple of points to watch when switching loads like motors or relays with a transistor:

  • When the load turns off, a large back-EMF will be unleashed from the energised coil inductance. This is often enough to pop a transistor. Eliminate it by connecting a flywheel diode in parallel with the load, and reverse-biased. The diode should have a current rating a little larger than the switched load current, and a reverse voltage rating a little larger than the voltage across the load. Typical examples are 1N4148 diodes for <100mA, or a 1N4001 for <1A, etc.

  • The gain of a bipolar transistor in saturation is quite low, and you may need to stuff a (relatively) large chunk of current into the base to get the transistor turned on good and hard. Look at the Vce vs. Ib curves on the datasheet to get a good idea of how much current you need, and hence what value of base resistor to use.

An ex-boss of mine used to ask prospective employees at interviews: “How would you interface a microcontroller to a relay?”. A frighteningly large percentage of engineers just don’t know