BNZ wrote:Hi All.
I am a complete electronics noob and i have found this post incredibly useful as I have a 4 channel sainsmart 5v relay that I could only trigger one channel via 3.3v.
I have replicated the circuit in the first post (except I use USB and 5v from GPIO pin 2). What I don't know how to calculate despite my google efforts is how I calculate the value required where you have a 2.2K resistor for my 2N3904 Transistor.
I have blindly tested this with a 4.6K resistor (I dont have any 2.2k!) and this worked, but I want to understand how this value is calculated and what I should be using.
Let's see how your circuit with the 4.6K resistor will behave:
We look for the "worst case", to see if the current through the relay will be sufficient to trip it.
The GPIO pin specs on page https://www.raspberrypi.org/help/quick-start-guide/
tell us that when high, a pin will at worst provide 2.40V. (This is the Voh rating. You will notice that there are three numbers, depending on how the pin was programmed. We will take the worst case.) The transistor base-emitter voltage will be approximately 0.6V when on. That means you will have 2.4-0.6=1.8V across your 4.6K resistor. So, the current through the resistor (and therefore the transistor base) will be 1.8V/4.6K=0.4ma.
Now the transistor specs for 2N3904 (http://www.st.com/web/en/resource/techn ... 002987.pdf
) state that the current gain (h_fe or simply beta) of the transistor varies between 50 and 300. It is usually safe to assume a value of 100, although if you really want to be pessimistic you should choose 50. This means, the collector current may be up to 0.4ma x 100 = 40ma.
If this is sufficient current for your relay, you should be ok. For a 5V relay, 40ma would mean a 5V/40ma = 125ohm coil resistance. If your coil resistance is larger than this, you are ok: The transistor will saturate, and a relay current less than 40ma will flow. Otherwise, the relay has been designed for a larger current, and it will operate only if you are lucky. (Not only that, but some voltage will remain on the transistor, and extra power will be dissipated in the transistor as heat, which may cause problems if the remaining voltage is high.)
Ooops. I thought you will be driving the relay directly with your transistor. (So the paragraph above is not valid, but I will not remove it since you may be intrested in that case as another design example.) If you will be driving the relay-module with the transistor (as you mention in your post) then all you need is only a 2ma current. (See my post above.) You are safe by a wide margin.
The original design with 2.2K resistor generates approximately twice the current, so it will allow for collector currents as high as 80ma.
Remember that this is a "worst case" scenario, you may get even a higher collector current allowance depending on device behavior.