# Transistor for 5V relay

Following up this question I need a transistor to switch the necessary current through the coil. I have some spare BC817 lying. My digital output pins can switch 5V@20mA. Do I need a base resistor to limit the current? I am unable to find the limiting value in the datasheet.

In my current setup (No base resistor) the transistor does not switch. Adding 1kOhm allows me to use it as a switch, but I cant figure out why

• Do I need a base resistor to limit the current? Yes, you ALWAYS need base resistor when using NPNs or PNPs as a switch. Without that resistor too much current can flow. Usually that can still work (the current stays below 20 mA in your case) but the high current is not needed and in the long run could damage the control chip. A 1 kohm resistor is generally a good choice. It will limit the current to a couple of mA and that's OK. Feb 4, 2020 at 14:56
• Can you post a schematic? Is the relay driven from emitter or collector?
– Bart
Feb 4, 2020 at 14:56
• I found an explanation and a quick calculation in this thread
– v3xX
Feb 4, 2020 at 17:10 For switching operation you should set the base resistor value such that the when 5V is applied to the base, transistor works in saturation region (loosely saying it is a region where we get a maximum or saturated collector/emitter current for a given transistor circuit)

Now for calculating this resistor value, you can start off by initially assuming that your transistor is in saturation region when 5V is applied to the base. In saturation region, the voltage difference between Collector and Emitter goes to a minimum and constant value(0.7V for transistor BC817). This 0.7V is dropped across the collector-emitter. Remaining 11.3 V is applied across your Relay of 83ohms resistance giving a current of I=11.3/83=136mA which is way too much for your relay. So you need to increase the effective resistance between collector and 12V supply. Now you can try for various resistor values to put in series with the relay to get an appropriate current value. Assume this resistor value to be 'X'., resulting in an effective series resistance of (83+X). Let's say you want current of 65mA to turn the relay ON.

From ohm's law:

I=V/R

0.065=11.3/(83+X)

X=90ohms

Hence add a 90ohms(approx) resistance in series with the relay.

Coming to the next part. Now you need to know hfe value for your exact transistor model(different values for different variants) . Dividing 65mA(also called saturation current in this case) by this hfe value gives you a current value. This current value is the minimum required base current to let your transistor work in saturation region. You can give a little more value than this to the base (but not too large) to ensure saturation region.

The last calculation finds the value of base resistor to let flow this much base current. Apply Kirchhoff's current Law in path from base voltage (i.e 5V) to base resistor to base-emitter voltage (1.2V in your transistor) to ground. This leads to an equation with all known values except one, that is, base resistor value. Solve the equation, find this resistor value and you're done!

If you learn the basic transistor functionality you can come up with numerous designs for the same problem!