Feed on
Posts
Comments

So here we are, we’re going to build us a motor controller to control 2 motors in both directions. First we’ll start off with our design which shows how our components will be connected and our reasoning, we could of just bought a motor controller chip like the L298N but we will rather learn something from our build and it should be easier to use than the L298N chip. Another on the plus our motor controller will have is about 1/2 the Vce of the L298N which is an advantage when dealing with low voltage motors and low voltage power supplies as well as back EMF protection (both will be explained).

This is quite a long post and do remember that I’m also new to understanding all of these concepts so do feel free to correct me if I’m doing something wrong! I’ve spent a good month checking all this out, posting on an electronics forum and trying to grasp the concepts I’ll be explaining you.

The components which we’ll need are:

  • Resistors
  • Diodes
  • PNP & NPN transistors

If you haven’t already, it would be a good idea to take a look at Building a Small Robot: Part 2 – Moving Forward as I explain the use of transistors. Also we will be using a easy to use circuit simulator program called Analog Circuit Simulator Applet which will speed up our design and make it easy to test our circuit.

How do I know which components we need? I’ve done it all through testing various configurations using the circuit simulator program as above. So just a quick re-cap of resistors and diodes below.

Resistors are used to limit the amount of current (milliamps mA or amps A) that flows through the circuit. Resistors are measured in Ohms and by knowing the input voltage and Ohms we can calculate the current that the resistor will allow. For example, we have 5 volts input and a 200 Ohm resistor, we do 5/.200 which gives us 25mA of current flowing. Below is our example, you can see the battery, the current being carried in the wire and the resistor value.

Resistors are important because if we didn’t use them a few things would happen, firstly the battery would send all it’s current through the wire which would almost guarantee any component that connected to it would be damaged/fry, another reason is the battery could actually explode because it will would be like short circuiting the battery which you should know to never do that. The important thing to remember is different components require different amounts of current.

A microcontroller could only require 40mA, LEDs normally require 20mA, motors are a special case; because of how motors are constructed they already have a resistance which means you can connect a motor directly to a battery and it won’t short circuit the battery as the motor has a resistance (though it’s quite low).

Also take note that resistors don’t affect the voltage passing through the wire (there is an exception if you have two in a row, in between them there will be different voltage but we don’t need to cover that here).

Diodes allow the voltage and current to only travel one way, there are various diodes with different ratings as to how much reverse voltage that diode can handle and the current that can flow through it. Reverse voltage just means how much voltage the diode can take when it’s blocking the flow of electricity from the reverse direction.  Note that a diode normally has a voltage drop generally of 0.7 volts, this means if you have 5V and connect a diode, you’ll only really have 5.3V travelling in that circuit. We have two examples below.

Here we see the diode allowing voltage/current to flow, you can see the diode has a arrow which shows the direction the diode allows the circuit to flow.

Now we see the diode is reversed and it doesn’t allow any voltage/current through.

Transistors: As we learned in building a small robot part 2, we can harvest transistors which can be used as switches. Transistors are like diodes in the fact that there is always a voltage loss between two parts of the transistor.

In the example below, you see that a transistor has a little arrow like  diode which shows where the voltage loss occurs. There is more to a transistor that meets the eye, they are complex little devices, if you’ve ever read a transistor datasheet you’ll see what I mean as there are various specifications which you need to take into consideration. There are hundreds of transistors to choose from so if you make a wrong choice your circuit could dramatically change leading to unexplained results.

Take a look at the circuit below, what is actually happening here?

Our voltage source is 5V, we have a 100 Ohm resistor and a NPN transistor. How do we know it’s NPN? It’s hard to tell as it is now but you can see a little arrow going from the text B to E. As you can see we have some specifics on the transistor shown too.

First things first, transistors can be used in various ways, for us we use it as a switch: we input a certain amount of current (42.7mA) to the Base (B) of the transistor and it lets 10 times (beta = 10) the amount of current (42.7mA x 10 = 427mA) to flow through the Collector (C) and Emitter (E). It’s important to note that the “beta” value is only 10 when the transistor is saturated, meaning the Vce is close to 0V, otherwise the “beta” value will vary depending on the Vce as well as the base current present. In the case above, it’s not saturated, the actual beta would be around 300 or higher at a guess, which means 42mA x 300 would let 12A through the transistor, so please don’t try this circuit at home, it’s just for simplicities sake.

What is Ic and Ib? Ic shows the current flowing through the Collector and Ib shows the current flowing through the Base.

How about Vbe, Vbc and Vce? These are going to be important for us later, V is voltage and be is the connection between Base and Emitter. So Vbe is the difference in voltage between the Base connection and the Emitter connection. Remember how we said transistors are like diodes? It shows us a Vbe of 729mV which is 0.7V, so we lose v0.7V. In this example above it’s not important to us. Vbc is 4.27V because the Base is now 0.7V less than the Collector because it has the “diode”. Vce is 5V because there is no other components between the battery and the Collector of the transistor. If a LED is added, assuming it uses 2V, the Vce would be 3V (5V – 2V).

Why is the current shown below the Emitter (E) actually 469mA? It’s because the Base current flows through the Emitter (E) too because it needs to go to the battery’s ground, so you add both the Collector and Emitter current.

Now what happens if we add a motor? In our case we represent a motor by using a very low Ohm value resistor. Example below, all looks good.

So we are designing this for the Arduino, if you look up the technical specifications you’ll see it can only output 40mA which means we can’t power our motor from it, so we’ll add another 6V battery to power the motor as below and we’ll change the 5V battery to a on/off logic switch as below to make things simpler, all looks good. (Ignore the fact that I’ve increased the resistors values, note that there is no a ground symbol at the bottom, this is needed for the logic switch)

Now I think it’s a good time to explain that all electrical components are rated to handle a certain amount of current and voltage which makes up the number of watts the component can disperse without being damaged, this information is found on a components datasheet.

For resistors it’s calculated by multiplying the voltage by itself and then dividing by the resistance. Just an example in the case above, it would be 6V x 6V / 55 which is 0.65mW, so our resistor needs to handle 0.65mW. Most common resistors only handle 1/2 Watt (0.5mW), so you’ll need to be careful in your selection and I personally would grab a 1Watt resistor to be safe.

For transistors as we are using them we can calculate it by multiplying the Vce by the amount of current being allowed to pass through. Why the Vce and not the voltage? It’s because heat actually comes from the difference in voltage which as we know is what Vce is. So if the Vce is 0.1mV as above, there is not much difference in voltage so the amount of heat generated is minimal. 0.1mV x 118mA = 11.8 mW, almost nothing. It’s important to know this because once you start playing around and putting 1 Amp through a transistor rated at 1.5mW you need to get down to the specifics as any Vce amount over 1.5V would cause damage (1.5W / 1000mA = 1.5V, you would be wise to not go near exactly on the limit either; I know I wouldn’t).

Now what if we wanted to reverse the direction of the motor? That would mean we need to switch the positive and negative voltages of the motor. Lets assume we were to use two NPN transistors for the two connections of the motors as below, this would technically allow us to control the direction of the motor. You can see this in the example below.

Now we see a few things happen to the transistor up in the top, the Vbe is still 0.7V which is fine; but what to notice is the Vbc and Vce are very high.  The NPN transistor is designed to work with loads (i.e motors) between the voltage source and the collector, not between ground and the emitter. NPN transistors when using as a switch in a way should be thought of being the closest component to ground.

This is where the PNP transistor becomes handy. The PNP transistor is a kind of reversed NPN transistor, it has the “diode” connected the opposite way between the Base and Emitter and instead of inputting a Base current, it actually outputs the Base current. PNP transistors need to be controlled by a resistor because as long as they have a connection to ground they will allow any kind of current to pass through them so you need to be careful. So we exchange the top NPN transistor to a PNP one and have added a resistor.

Wow, look at the difference, Vbe is still 0.7V (negative, because remember we said PNP was the opposite of NPN?) but look at Vbe and Vce, very close to 0 which means 0.1mV x 116mA = 11mW dissipation which is nothing. Now you need to be careful when choosing a resistor for the PNP, choose a resistor that’s too high and you will actually increase the Vce which will in turn increase the “beta” value. You want to stay on low values under 500 Ohm and even down to 50Ohm depending on the current you want to travel but then you need to remember about the resistor’s power dissipation as well.

Lets pretend we have 12V now for the motor battery, the current will be the same however this is where the voltage difference between the 5V and the 12V will affect the NPN transistor, the Vbe and Vce would be 6V which would cause us problems if the beta is at 10, at higher values it becomes better however I prefer not to risk it. Generally an increase in beta results in a decrease of the Vce because the transistor becomes saturated. So how do we fix it? Add another NPN transistor to the existing one!

When you have two transistors together it firstly boosts the current that flows through, if we have two beta 10 values for each transistor, we have 10 x 10 = 100 times the input current at a minimum so it’s like having 100 beta on just one transistor. So 10mA input would result in 1A current flow which would suit us because if you recall the Arduino only outputs 40mA, if we only had one transistor and the beta stayed at 10, it would only allow 400mA to flow, so using two transistors would suit us plus we don’t even need to worry about what the beta might be because of first transistor will always be saturated thus allowing the voltage to pass though.

Two transistors that are connected together are called a darlington pair, sometimes you’ll see this transistor as just 1 transistor which acts like 2 transistors in 1. Notice how the emitter of the first transistor goes to the base of the second transistor and the collectors are joined together. The second thing it does is completely reduce the Vce down to around 0.7V which is acceptable, 0.7V x 480mA = 336mW.

Now that’s good and all however do you see an issue with this circuit? What happens when there is no logic high and it’s a low instead?

The PNP transistor will continue to run even if there is no input to the base of the first transistor, so we need to change this. Also notice the 228 Ohm resistor, that resistor would receive the full motor battery voltage which could for us range between 6V to 12V, so we’d need a pretty high watt resistor if say we bumped the current flow up to 1 Amp.

We change this by adding a NPN connected to the PNP transistor, so now we are switching if we want the PNP transistor to be on or not.

Notice the 40 Ohm resistor, it resists the current flowing through the PNP and also though the NPN too. Remember our Vbe dropping 0.7V? This means that the emitter of the top NPN transistor actually will ready 4.3V no matter what the motor battery voltage is which is great! 4.3 x 4.3 / 40 = 0.46mW, not too bad I’d still go with a 1 Watt resistor to be safe.

We will assume our motor takes 6 volts and has a stall rating of 500mA, so I’ve made the circuit above handle up to 1 Amp. Stall rating gives a good indication of the maximum current the motor will need if it’s stalled, because once a motor is stalled (i.e. you don’t let it rotate, the resistance is affected thereby increasing the current it consumes trying to turn the motor). Always test your motors stall rating or look at the datasheet before designing a circuit.

So lets do our calculations, the resistor is 40 Ohm, 4.3V / 40 = 98mA, so to calculate how much NPN base it’s taking we assume the beta is the worst it will be as 10, so 98mA / 10 = 9.8mA. From 98mA we multiply by another 10 for “beta” for the PNP which gives us 980mA that will flow through the motor however the PNP collector needs to handle 980mA + 98mA = 1.1A because we are using 98mA through the NPN transistor. By varing the both the resistors in this circuit we come up with the best case scenario that will work just as we

Now remember we talked about transistor dissipation, look at the NPN connected to the PNP, if the motor battery voltage is 6V, the Vce is 0.9V. Now if we increase it to 12V, the Vce becomes 7V which is quite high, it’s this high because we have 11.3V coming to the NPN collector and 4.3V coming from the emitter which gives us 7V. Now going by our dissipation formula, 7V x 98mA = 686mW, pretty close to the limit if we were to use a less powerful transistor like a 1 Watt one (as it’s cheaper). So the maximum voltage of our circuit would be 15V, 15V – 0.7V = 14.3V – 4.3V = 10V * 98mA = 980mW so very close to the limit.

You heard me compare the Vce to the L298N chip? To calculate our Vce minimum, we remember we have two transistors so it’s 0.7V for that, then we need to check our datasheet for the PNP and NPN transistors we will select to handle the 1 Amp current. I’m going with these, it says 0.3V max at 1A Ic for the Vce, so we’ll say 0.1V as minimum; the total is 0.9V as a minimum. Now we look at the L298N chip, on page 3 it says Vcesat total drop as 1.8V as minimum. So as we see our motor controller is half the Vce of the L298N chip which means the motor will receive more voltage and run more efficiently as it’s running closer to the voltage that  it’s supposed to be running at.

Our circuit is now figured out, all we need to do is add a duplicate of components and connect up to the motor the opposite way to spin in the other direction as below, it’s a bit hard to see so I’ve also included a schematic below too.

Edit: The picture above should read 39 Ohms and 390 Ohms instead of 39K and 390K.

We’ll now talk about back EMF which basically means that when a motor is spinning and you turn the power off, the motor continues to spin and therefore creates voltage/current because as we know motors contain magnets which is where the EMF (Electric and magnetic fields) comes into it.  This voltage/current created has to go somewhere and if you don’t have a diode it will be routed back to the other components on your circuit which could potentially damage them. By using a diode, you can route that voltage/current back to the motor.

If we were spinning the motor in just one direction we would use it like the below.

So when the motor is running we see that the diode isn’t on because the current is flowing top to bottom and it’s actually resisting the current from the top (shown by the wire coloured green on the top half of the diode). Now if we were to stop the input high from the Arduino, the motor continues to spin and the current has no where to go because all transistors are switched “off” so the only route to take is through the bottom half of the diode to the top half and back to the motor again. This means we are now protected against back EMF.

But we are spinning in both directions so we need 4 diodes and the voltage/current is routed back towards the battery instead as below. A bit hard to see, schematic provided as well to make things a bit clearer.

Edit: The picture above should read 39 Ohms and 390 Ohms instead of 39K and 390K.

We now see 2 diodes connected both ways on each of the motor’s power. An example, we look at the DI1 line, it connects the left transistors up to the motor, so the motor spins anti-clockwise; left to right. When the DI1 is low, it continues to spin, wire 2 of the motor goes through D3 then back to the motor power and then out again though D1. So you see the current is routed back to the battery.

Now if we wanted this for 2 motors, we just need to make another copy of the circuit. And that concludes the explanation of my design, I hope you’ve learnt something out of it. I have to say that the circuit simulator really did save a lot of my time, so it’s very handy to have. Next we’ll do the implementation to the actual robot built and finally the finished PCB product along with variances of this design? Like instead of  2 motor bi-directional controller, how about a 4 motor one direction controller or possibly a motor controller with the ability to “switch” resistors so we can control the current. I’ve got a few variations planned.

Leave a Reply