Some basic rules for driving common emitter BJT switchers:
1. Establish the maximum collector current. (Ic max)
2. Establish the transistor gain (hfe) and suitability.
3. Calculate the base resistor and power dissipation.
---
Ic max is the most crucial figure and everything else is worked out from that. It’s the maximum current that will ever flow through the collector circuit when it is switched fully on. This is usually calculated from the resistance of the load and the voltage across the load using ohms law, I=V/R. In this case, Ic max = 5v / Rcoil. Where Rcoil is the DC resistance of the coil as measured using a multimeter.
Usually, the lower the voltage rating of the relay coil, the lower its resistance and the more current it draws. I can only guess at the resistance of your coil, so let’s say it’s 50 ohms. So Ic max = 5/50 = 0.1A or 100mA.
100mA is actually quite a lot for a signal transistor and you should be looking for a power transistor. The transistor data will show the maximum collector current for a particular transistor.
ALWAYS OVER ESTIMATE. If you need 100mA, get a 500mA or 1A transistor. If you need 500mA, get a 1.5A or 2A transistor etc.
In this case, for 100mA, a BC639 should be fine. I have loads of them and it’s probably what I’d use if I were choosing a BJT.
---
The gain (hfe) is quoted on the data sheet. It’s usually quoted with a collector current. The reason a collector current is given alongside the gain is that the gain varies as collector current varies. The higher the current, the lower the gain, for a particular transistor. This is one of the reasons why you should over estimate the Ic max. Another reason is that the quoted hfe is only an estimate and can vary quite a lot from device to device, even on the same production run.
The BC639 has a minimum gain of about 40 at 150mA. It’s gain at 100mA should be a little more but let’s stick with hfe=40 for now.
Suitability is primarily about maximum collector voltage (before breakdown), power dissipation and frequency. For low frequency, low voltage, low power hobby applications, almost any BJT will do. But always check the data anyway. More on power dissipation below.
---
The base current (Ib) flows through the base resistor, through the transistors base and emitter and then to ground. It must be large enough to fully turn on (saturate) the transistor when it’s driving Ic max. The gain is the relationship between the base current and the collector current, or to put it another way, the collector current (Ic) is equal to the base current (Ib) multiplied by the gain (hfe). So since you know the maximum collector current (100mA) and you know the gain (40) you can establish the minimum required base current to turn the transistor fully on thus: Ib min = Ic max / hfe, or in this case, Ib min = 0.1/40. So Ib min is 0.0025 or 2.5mA.
So with this transistor, to drive a load of 100mA, you need a base current of AT LEAST 2.5mA. The Picaxe outputs 5v and the base-emitter junction, just like a diode, requires about 0.7v to be forward biased. So the voltage across your base resistor is 5-0.7 = 4.3v.
Now you have the voltage across the base resistor (4.3v), and you have the current required to flow through it (2.5mA). Ohms law gives you the value of the resistor thus: R=V/I. In this case, Rb = 4.3/0.0025. So Rb is 1720 ohms, or 1.72K. Remember though, that this resistor has been calculated to provide the minimum required base current, and you’ll be unable to get hold of that exact value anyway, so to provide a little more than the bare minimum base current, use the next smallest resistor you can find. On the E12 series, that’s 1.5K.
Personally, I like to assume my transistors are not quite to spec and would prefer to provide even more base current, just in case its gain is actually lower than specified. So I’d go one step down again on the E12 series, which is 1.2k.
The power dissipation is essentially the heat generated in the transistor quoted in Watts. Often this is negligible, but sometimes a heatsink may be required, or even a redesign with a bigger transistor. The power dissipated is a function of the voltage dropped across the transistor junctions and the current through them. It’s ohms law again.
Consult the datasheet for the saturation voltage (Vce sat). This is the voltage across the collector-emitter junction when it’s turned fully on. This is usually a very small value, less than half a volt, but again can vary from device to device, even on the same production run. The Vce sat for a BC639 is 0.5v maximum. Usually you will find that it’s actually lower, but you should use the maximum specified value to be sure.
So when the transistor is fully on, there is 100mA flowing through the 0.5v collector-emitter drop. Watts (P) = Volts (Vce) * Amps (Ic), so that’s P=0.5*0.1, so P=0.05 watts or 50mW. There is also a small contribution made by the base current. If using a 1.2k base resistor, the base current is 3.6mA. Through the 0.7v base-emitter drop, that’s about 2.5mW. So the transistor, when fully on, should generate about 52.5mW of heat.
That’s not much and should be fine without a heatsink. The calculations for exactly when a heat sink is required and how big it should be are a little complicated, but what I usually do is estimate then suck it and see. IE: Prototype the circuit, turn the transistor on and put my finger on the transistor. The rule of thumb is, if you can keep your finger held on the transistor indefinitely, then it’s ok. If you can’t, then heatsinking is required.
---
To recap.
Maximum base resistor value (Rb) = Base resistor voltage (Rv) / Minimum base current (Ib)
Minimum base current (Ib) = Maximum collector current (Ic) / hfe
Power dissipated (P) = Saturation voltage (Vce sat) * Collector current (Ic) + 0.7 * Base current (Ib)
And always overestimate!
And always use a reversed diode across the load if it’s inductive (IE: Coils, Motors etc).
---
More info on BJTs :
http://www.allaboutcircuits.com/vol_3/chpt_4/1.html
EDIT: Remember. This example is for an Ic max of 100mA. You will need to do your own calculations based upon your load.