# In a circuit being switched by an NPN transistor, do power supply and input need the same ground?

I'm trying to make a circuit that will allow me to switch on a relay that will turn on an LED. However, the relay is rated for 12 V, and I only have an input of 5 V, so I'm using an NPN transistor. to switch the power to the relay on and off. Here's the schematic:

However, I'm confused about a few things (Note the ground for both the 12 V power supply and the 5 V power supply are unspecified):

1. If my 5 V power supply is an Arduino, can I use the ground for that for the ground of the 12 V power supply?

2. Is it okay for the base and the emitter to have different grounds on the transistor? Or do they have to be the same?

3. If my 12 V power supply is 8 AA batteries (not sustainable, but I'm just using it for testing), how would I connect that to the same ground as the arduino, instead of the negative side of the batteries?

4. How can I figure out what the R1 and R2 should be, based on the transistor? I read some things online, but am still confused.

5. Are there any other things that I'm not taking into account that I should be?

I'm completely new to this, so any help is much appreciated.

-
Any reason why the relay is so far to the right? If it is some distance away in reality as well then you have to place diode D2 directly across the coil. In any case much closer than the 10 cm they're now apart. –  stevenvh Aug 19 '12 at 10:08
Sorry, I was using some shitty free software to draw it, and the components lengths weren't adjustable. That's why the diode didn't fit between each side of the coil. In my circuit, they're right next to one another. –  Mason Aug 19 '12 at 15:13
No need to apologize. I just wanted to point out that it's important to have the two close together. But you seem to know that, so everything is peachy :-) –  stevenvh Aug 19 '12 at 15:16

1. Yes, you need to connect the 5V and 12V grounds in this circuit in order for the transistor to switch. Remember there must be a return path for the base current. You cannot send a signal using only 1 wire.
2. See above, the emitter needs to use the same ground as the signal source (Arduino) or there is no return path.

3. Connect the negative terminal of the bottom battery (assuming you have 8 in series) to the Arduino ground.
"Ground" is just a term for a reference point to measure voltages from in your circuit, you can pick any point, (though it's usually a net connected to the negative terminal of a supply). For example you could call the point the positive terminal connects to in the circuit "ground", and then the "original ground" (the ground as shown in your circuit) would be -12V relative to it. The negative terminal does not mean the voltage is negative, it just tells you which way the current flows.

4. (a) R1 is to limit current to the base of the transistor. To calculate the value, we need to know how much current we are switching (i.e. how much the relay needs) and the current gain of the transistor. Lets say we are using a transistor with a current gain of 200, and the relay needs 20mA to switch. Since the current through the base is amplified by the current gain, we know the base current needs to be at least 20mA / 200 = 0.1mA.
The base voltage of a typical bipolar transistor is around 0.7V, so the series resistor (R1) needs to be a maximum of: (5V - 0.7V) / 0.1mA = 43kΩ
As the gain may vary (go from min value in datasheet to be safe) we can pick a 33kΩ to have some base current to spare. Note that to be an effective switch we want the transistor to saturate, as effective gain starts to drop at the knee between linear and saturation mode (as mentioned by Shokran). So we pick a resistor of a lower value than calculated to make sure we can pull the collector near ground. In cases with e.g. power transistors where minimising dissipation is important it is wise to pick a value of at least 5 times less than calculated (or assume gain of ~20) so we could go as low as 4.3k in the above example.

(b) R2 is there to make sure the base is pulled to ground when drive current is removed. This is to stop leakage current turning the transistor partially on. The value does not need to be too precise, just enough to shunt the leakage current (datasheet) and not too low to steal too much base drive current. 5-10 times the series resistor (or 1kΩ to 500kΩ) is a rough range to go from. 100k&Omega is a reasonable value for most cases, though I'd go for 330k here as the leakage current should be minimal. If you need to go a lot lower then you have to adjust the series resistor to compensate.
Note that if the Arduino pin is driven to 0V (i.e. set to output and logic 0) then R2 is not really necessary, it's only if the pin is set to High Impedance (i.e. input)
Note 2 - that this is very rarely something to worry about with BJTs (MOSFETs are another matter and definitely do not want to be left floating) If you have a very high gain transistor (esp darlington), a noisy environment, and/or very high temperature (leakage increases with temp) and a very high collector resistor then it may cause issues, but generally the leakage current will be to small to matter.

5. Not that I can spot right now (however it is 4:48 in the morning here so my brain may have long since retired, so I reserve the right to have missed something obvious ;-) )

-
This is a seriously awesome answer. Thank you! –  Mason Aug 19 '12 at 6:14
Follow up question: let's say I was replacing the LED with some other component that requires 12 V to run. Is there a way to reduce the circuit? Presumably I don't need the relay at all, since I already have a 12 V power supply? Or are there other things to take into consideration in that situation? –  Mason Aug 20 '12 at 1:06
If the component can be driven with a current less than the maximum the NPN can handle (it's Ice rating) and you don't exceed it's power/temperature rating (Ic * Vce = power dissipated) then you can do without the relay, yes. For example most LEDs can be driven from a general purpose NPN (for example a typical Ic rating is 500mA, and a typical LED only requires 20mA max) A relay is mainly used with high voltages (e.g. mains) or currents and when isolation is a good idea. –  Oli Glaser Aug 20 '12 at 2:59
So, if my component still needs 5A, then I should stay with the relay, yeah? This is my relay. Update: when I apply 12V from double A batteries to the component, without any resistance added, the component is barely running. I was under the impression that the component would draw as much current as necessary from the batteries. Is that incorrect? Is it possible that it's a problem with the relay? The relay is still switching correctly, the component is just humming quietly instead of running full throttle like I'd like. –  Mason Aug 20 '12 at 4:56
Note: the component is this windshield wiper pump, if that makes a difference. After measuring the current I'm getting, it looks like I'm only drawing 2.5A. –  Mason Aug 20 '12 at 5:12

1), 2) and 3)
If you use different power supplies in a circuit you have to connect them some way or another so that they have a common reference. You will almost always connect grounds, since they are your reference. Voltage is relative: if you take the batteries' plus as a reference the minus will be at -12 V, if you take the minus as reference the plus will be at +12 V. Few circuits will use the plus as reference, we like positive voltages better. So the batteries' minus goes to the Arduino's ground.

Why do they have to be connected? Your transistor will see two currents: a base current, entering the base and returning to the 5V supply through the emitter, and a collector current entering the collector and also returning to the battery via the emitter. Since the currents have the emitter in common (it's called a common emitter circuit) that will be where both power supplies will be connected.

How does the base current know which way to go when it exits the transistor via the emitter? Current can only flow in a closed loop, from the plus from the power supply to the minus. The base current started at the +5 V, so it would not close the loop when it would go the way of the batteries' ground.

4)
We'll leave R2 out for a moment. Because the base-emitter acts as a diode the base is at around 0.7 V. You apply 5 V to activate the transistor, then according to Ohm's Law the current through R1 (which is the base current) is $\frac{5V-0.7V}{R1}$. The transistor will amplify that current to a sufficiently high collector current to drive the relay. What's sufficiently high? Therefore you have to check the relay's datasheet. It will either tell you the required current, or the coil's resistance, and then you can calculate the current, again with Ohm's Law. A relay typically needs around 400 mW to activate, so for a 12 V relay that would be a current of 400 mW/12 V = 35 mA. That's the minimum collector current.

To find out how much base current we need to get that we have to look into the transistor's datasheet. Let's say I have 100 000 BC547Bs lying around (I forgot the decimal point when I ordered them) for which I need a purpose. The current gain is given by the $h_{FE}$ parameter, which we find on page 2 of the datasheet. For the BC547B that's minimum 200. (Always use worst case values, for $h_{FE}$ that's the minimum value. If you use typical values you may have too little current for some parts.)

So to get 35 mA collector current we need 35 mA/200 = 0.175 mA base current. Then R1 has to be $\frac{4.3 V}{0.175 mA}$ = 24600 Ω. That's a value you won't find, so shoudl we choose a higher or lower value. If we would pick a higher value the current will be lower, also the collector current will be less, and our relay may not activate. So it has to be lower, the 24600 Ω is the upper limit. Now there's nothing wrong with supplying too much base current (within reason); the collector current will try to follow, but the coil's resistance will limit it. If the coil's resistance is 360 Ω then Ohm's Law says you can't get more than 35 mA at 12 V, no matter how hard you try.

Let's pick a 10 kΩ resistor. That's a much lower value than we needed but we'll be OK. The base current will be around 0.5 mA, which the Arduino will supply happily, and the transistor will try to make that 100 mA, but again, it will be limited to our 35 mA. In general it's a good idea to have some margin, in case the 5 V would be a bit less, or whatever variations there may be else in the parameters. We have a factor three safety margin, which should be OK.

What about R2? We didn't use that and everything seems to be OK. That's right, and it will be in most cases. When would we need it? If the output low voltage of the Arduino wouldn't go below 0.7 V so that the transistor also would get current when off. That won't be the case, but let's say the output low voltage would stick at 1 V. R1 and R2 form a resistor divider, and if we choose R1 = R2 then the 1 V input would become 0.5 V base voltage, and the transistor wouldn't get any current.

We had 0.5 mA base current when on, but with R2 parallel to the base-emitter we'll lose some of that current there. If R2 is 10 kΩ it will draw 0.7 V/10 kΩ = 70 µA. So our 500 µA base current becomes 430 µA. We had a lot of margin, so that would still give us enough current to activate the relay.

Another use for R2 would be to drain leakage current. Suppose the transistor is driven by a current source, like an optocoupler's phototransistor. If the optocoupler sources current it will all go into the base. If the optocoupler is off the phototransistor will still create a small leakage current, what's called "dark-current". Often not more than 1 µA, but if we don't do anything about it it will flow into the base and create a 200 µA collector current. While it should be zero. So we introduce R2, and choose a 68 kΩ for it. Then R2 will create a voltage drop of 68 mV/µA. As long as the voltage drop is less than 0.7 V all current will go through R2, and none into the base. That's at 10 µA. If the current is higher R2's current will be clipped at that 10 µA, and the remainder goes through the base. So we can use R2 to create a threshold. The dark-current won't activate the transistor, because too low.

Except for this case of current-driven R2 will very rarely be necessary. You won't need it here.

-
'R1 and R2 form a resistor divider, and if we choose R1 = R2 then the 1 V input would become 0.5 V base voltage' But you usually choose R2 >> R1 (x10 times), so the input would still be near 1 V.. –  m.Alin Aug 19 '12 at 11:22
@m.Alin - the base voltage will be 0.7 V maximum. In the 0.5 V case the base won't draw any current, but if your 10:1 divider would set that to 0.9 V the B-E junction limits that to 0.7 V. So R2 doesn't set the voltage, and will only draw a limited current, because much larger than R1, so it doesn't really serve a function. Which is why I said you'll rarely need it. –  stevenvh Aug 19 '12 at 11:34
In this case, then, I can just connect the base directly to ground? –  Mason Aug 20 '12 at 0:47
For some reason, when I just connected a lead from the base to ground, it didn't work, but when I added the 68kΩ resistor in its place, it worked perfectly. –  Mason Aug 20 '12 at 1:05
@Mason - I guess we have a slight misunderstanding here. Omitting R2 means no connection between base and ground, not replace it by a wire. Connecting the base to ground won't work, because the base voltage will be zero, and you need 0.7 V. I'm sure it will work with the 68k, but also without. Just don't connect base to ground. –  stevenvh Aug 20 '12 at 5:42

Seems worth mentioning that if you really need to have 2 separate grounds then you do have the option of an optocouple AKA solid state relay. But these are several times more bulky and expensive than transistors (still not bad for a small project) so only use if really needed.

-

## protected by Community♦Apr 24 '13 at 14:21

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.