2N3904 Transistor as switch
amboxer21
Posts: 15
Hey everyone. I am in need of some expert assistance. I am trying to use the 2N3904 as a switch. I need to close a 12v circuit with the collector. I will be using a micro controller and a common emittor. 3.3v will be pushed to the base. How do I calculate the base to turn the transistor on? What i am trying to do is enter a 4 digit combo into a keypad and if the combination of numbers are correct, my microcontroller with push 3.3v to the base of the 2N3904. That in turn will allow me to close a 12v circuit in my cars ignition on the collector side of the transistor. I want to understand this and how it fully works. How to calculate the base resistor. How much voltage is needed to turn the base on and how much voltage do i need to saturate it. ALl that good stuff so i can do more projects. I would appreciate any help!! Even as much as a link. Thanks!
Comments
The 2N3904 will need to drive some kind of 12V relay that requires less than 200mA otherwise you'll need some other kind of transistor. Do have a look at the ULN2803A which is a Darlington high-current driver whose outputs can be paralleled for currents up to about 2A (with 4 in parallel)
Now what fun would that be?
Could you please explain this -> (5V - 0.3V (for the saturation voltage of the I/O pin). What do you mean by saturation voltage of the I/O pin. I thought saturation had to do with the base of the transistor. AM I accounting for voltage that the I/O pin pushes out? That extra voltage is what will saturate the base?
As well as this ->At 20mA, that's about 200 Ohms. I assume you are stating that I need a 200 Ohm resistor to apply to the base but where does the 20mA come in? Is that the current coming in from the collector side? Cold you please elaborate for me?
And this -> The current gain depends on the collector current. To my knowledge the current gain is also known as the DC collector current or the hfe. The 2N3904 has an hfe of 100. Also, I thought that current and voltage are not interchangable. If I am pushing volage to the base why are we talking about current at the base. I appreciate your time and wisdom.
An explanation is at:
http://www.electronics-tutorials.ws/transistor/tran_4.html
Remember as Mike Green said, the 2N3904 is a TO92 style transistor and you must put a resistor on the collector side to keep the current under 200mA or have a load equal to the resistor or it will get extremely hot and magic smoke will emerge. Transistors expect you to know what current you need to correctly define the case. They do sell Darlingtons as single transistors if you only need one.
See that is what i am not understanding, why do i have a 20A load at the collector? I want to use the 2N3904 as a switch. I need it to close a 12vdc curcuit. How do I know how much voltage is 200mA?? I am going to take a look at your link now. Thank you Irone!
EDIT: Ok, now i see that the relay's coil uses a nominal current to switch from normally open to normally closed.
If you are wondering about voltage, current, resistance and Ohm's law please go to top left of the page and then select Home. Then try looking at DC Theory. Another explanation is there.
Well, you might look at the data sheets for the 2n3904, which can handle 200ma, and its cousin the 2n2222a, which can handle 700ma.
You are not going to get 20Amps switched out of either, that would require a hunky T0-3 packaged transistor, as you go up in current demand.. you go up and up in physical package size and in heat buildup.
12VDC on the switch is not a problem as nearly all traditional transistors can handle 30 VDC or more... the amps are your problem. But in an automotive context, a transistor rated at 100VDC is wiser as there are a lot of nasty spikes that can go up to that.... a 30VDC rated transistor might die quickly.
=++++++
You can buy a big Darlington in one package, but I believe that that largest available is about 8 amps max. A 2n3055 T0-3 could possibly use to build a your own Darlington transistor pair, but will run sizzling hot. So nowadays, everyone has migrated to MOSfets that are smaller and run cooler for this kind of switching.
I like BJTs, but they are best limited to switching under 500ma and migrating to MOSfets for the heavy lifting.
+========
The vast majority of relays require 60ma or less for switching, but some go into hundreds of milliamps. So I stopped buying 2n3904 transistors and just use 2n2222 transistors for any application that requires either. That means less trips to the store and less junk in my kit.
How much voltage to turn on a 2n3904 .. just 0.7volts.. With a power Darlington pair .. maybe a full 2.0 volts to get saturation (it is listed in the spec sheet). Either will work with a Propeller output, but you want to protect the Propeller output with a current limiting resistor of at least 220 ohms... 1K is not a bad choice and you could even go higher.
Just use a 2n2222 and a 30amp automotive relay and everything will run cool while lasting longer. Be sure to include a flyback diode on the relay.
The I/O pin on my MCU outputs 5v. The Rb = V / I -> 5v - .7 / 20mA = 220 Ohms
The Max Ic on the 2N3904 is 200mA. The Nominal coil current on my 12VDC SPDT relay is 37mA. SO I need to retard the current passing through the relay to 37mA.
Ohm's Law -> Rc = V / I -> 12v / 37mA = 330 Ohms.
What I have left to figure out is the protection diode. Not sure how to do that. I am going to use a common emitter circuit as well.
The 12VDC SPDT relay is rated 1A @ 120AC/24VDC PC contacts. The coil resistance is 320 Ohms +/- 10%. The pickup/dropout voltage is 9/0.6VDC
Do I need a 660 Ohm resistor due to the coil resistance being 330??
So what am I missing so far and is what i have correct?
I am using these circuits as a basis ->
First you need to know how many volts and amps you need. Are you going to light a LED on 12 volts or are turning over a diesel engine with 12 volts. The next thing is how many volts and amps your micro controller can put out. Micro controllers are pushing it at 40mA. (.040 A) Most of them run at 5V or 3.3V. We fill in the needed power with transistors, SCR's, relays and such but need to know what we have and what is required.
My first post explains what i want to do. I will explain it again though. I am turning over a 12v car engine. I'll explain in short so you can get an idea of what I am trying to accomplish. A friend lost her key and could not get a spare. So I installed a SPST monetary push button up to her igniton with a toggle switch. The toggle switch is to push power throughout the car and the SPST MPB is to start the car. I also installed 2 SPST MPB switches under her wheel well to lock/unlock her car. Now I want to use the 2N3904 as a switch.
What i am trying to do is enter a 4 digit combo into a keypad and if the combination of numbers are correct, my microcontroller with push 5v to the base of the 2N3904. That in turn will allow me to close a 12v circuit in my cars ignition on the collector side of the transistor. Which is where the relay will reside. Maybe you could look at my previous post too??
WHICH ALSO HAS CIRCUIT DIAGRAMS.
All i am asking is what i have so far correct? And since the relay has a coil resistance of 330 ohms and the collector needs a resister to retard the current to match the nominal coil current of the relay(37mA), Do i need a 660 ohm resistor instead of a 330 ohm resistor?
Also, how do i figure out the size of the protector diode for the relay?
If you are going to power the microcontroller from the vehicle battery, that is another huge can of worms you need to consider. What will your circuit do when the microcontroller resets because of all the noise and spikes and sags on the 12v system. I can only imagine the dangerous results. Driving at highway speed the MCU resets, killing the ignition. Quickly fumble around for the keypad, punch in the code, find the start button, try to restart the engine, all the while coasting on the highway, trying not to get rear-ended. And if this happens at night????? Please think long and hard about doing this.
Is there a better more safer way of going about this then? Maybe hook it up to the ignition wiring harness?? Here is the push start i hooked up-> http://www.youtube.com/watch?v=jtDB6MnLBmg Maybe I could integrate the transistor circuit into this?? Basically, I would put it in between the push start and the ignition wiring harness.
Your 12V relay coil is 330Ω.
(12V - VCEsat) / 330Ω
(12V - 0.3V) / 330Ω = 35mA coil current
Rb, as in the second schematic, can be 220Ω. Although 1KΩ would probably work fine and drive the 2N3904 into saturation:
The spec is based on 50mA
Rb = (Vpin - VBEsat) / IB
Rb = (5V - .95V) / 5mA = 810Ω make it 820Ω to make it even.
R from 10KΩ to 100KΩ to absorb the 2N3904 Collector to Base leakage current.
You don't need now want a collector resistor. The coil resistance is enough to limit the current to 35mA.
The snubber diode needs to have a pulse current rating of at least the coil current, 35mA in your case.
A small 1N4148 signal diode is sufficient to do this. Many use the standard 1N400x series 1A rectifier diode.
An interesting variation is to use a visible light 50mA LED instead, and it looks cool to see it flash when turning off the relay.
Duane J
Take another look at Hal Albach's last statement. My best advice would be to have the vehicle towed to a qualified mechanic and have them replace locking mechanism. Also remind your friend to bring her pocketbook because keys are expensive these days! Do not risk her life on a quick cheap fix.
Personally, I would not use a microcontroller for this. If I understand the purpose of this exercise, it is to somewhat secure a vehicle that can all to easily be stolen because you have "hotwired" the ignition. The only advice I am willing to give you is to replace the existing "ignition toggle switch" with a switch that requires a key to activate it. These are easily found on the internet, possibly some hardware stores, or those places that specialize in security devices. Use the keyswitch contacts to operate a heavy duty automotive relay like Loopy suggested. Use the relay contacts to provide power to the ignition system. Simple, somewhat secure, and a lot more reliable.
Just Google KEYSWITCH to see what I mean.
Back in 1993 I bought a van which was customized with a cherry dash, fancy lights, a ten disk CD player and a lot of other fancy things. A couple of years later the key would not turn to start the van. I talked with a good mechanic and he told me if I could pick the key slot and turn it past lock I could use a pin to remove the key entirely I could replace it with a new locking mechanism. After about 15 minutes with a home made picking device I did the job. I ran the van a couple of weeks with a nail pounded flat and a set of needle nose vice grips. I then went to an Auto Parts Store and bought a new mechanism and ran the van until I traded it in. This option may not work for you because now the keys have enclosed a transmitter which tells you that not only the key is correct but the code from the transmitter is also correct. The key and transmitter were designed by an engineer who has a lot more training than either of us. Electricity is a strange thing and if a lightning strike hits near the vehicle it may stop the power necessary to keep the vehicle running. Remember, we are only hobbyists.
@Duane - how did you come up with Ib=5mA ? Is that assuming the worst case scenario when h21=10 ?
...very nice trick - I'll implement it from now on
Look under VCE(sat) and VBE(sat).
These parameters are describing transistors under saturation conditions. As when used as a switch with minimum VCE. IB of 5mA causes IC of 50mA and VCE of 0.3V. You only need 37mA of relay current but I would keep it at 5mA on the base. (You might be able to use IB of 3.7mA.)
hFE gain parameters describe operation in the linear region. Not good for switching.
Duane J
Making Rb 820 ohms. Rb = v / I.
5v(Vin) - vbe (.7v) - vcesat (.3v) / Ib
4.1 / 5mA = 820 Ohms
I am taking everyone's advice to not touch her ignition. I am instead going to switch on my ps2 with a transistor. The ps2 power supply has an output of 9v and 5.5 mA. Too much current for the 2N3904! So I bought a TIP3055 220 case.
It has an hfe of 20 min and 70max for 4A. So I used 15.
Ib = Ic / hfe -> 5A / 15 = .33A
Rb = Vin - Vbe (5v - 0.9v) / Ib -> 4.1 / .33A = 12.42 Ohms at 1.3W
Am I missing something?
EDIT: I would be using a 9v 5a spdt relay
hFE is what is called a "Small Signal" parameter. It is a gain parameter associated with the transistor operating in the analog or linear range. In the spec VCE is 1V (or greater).
But we want the transistor in saturation where VCEsat is only 0.3V or less. Less power would be dissipated and leave more voltage for the load.
hFE is never used when operating in saturation.
Not quite right:
VCEsat is not in the base circuit.
And you shouldn't use VBE when calculating transistors in saturation. You should use VBEsat which is 0.95V, see the spec.
( Vin - VBEsat ) / IB
( 5V - .95V ) / 5mA = 810Ω but the closest 5% value is 820Ω But that hFE is measured with a VCE of 4V. So there would be quite a bit of power dissipated in the TIP3055 transistor and would probably need a heatsink.
5A2 * 4V = 80W
So there would be quite a bit of power dissipated in the TIP3055 transistor and would probably need a heatsink. Furthermore the 4V would be subtracted from the input to the power supply.
What you should do is use the saturated parameters:
Where VCE would be more like 1V.
It looks like the current gain is 10 at IC of 4A, maybe 8 at IC of 5A.
IC / 8 = Ib
5A / 8 = 625mA
Of course the controller pin can't supply that much. Close but I would think the resistor would be:
( 5V - VBEsat ) / IB = Rb
( 5V - 1.8V ) / 625mA = 5.12Ω
And dissipate:
625mA2 * 5.12Ω = 2W
Ultimately this is an unworkable solution and it still needs another driver transistor.
There are Darlington transistors that can do this. I hate Darlingtons.
OK, I have refrained from suggesting the use of MOSFETs in this thread.
But here is a clear example of why they are almost always superior to bipolar transistors.
A much better solution would be to use a power MOSFET that can be driven directly by the controller whether 5V or 3.3V. I like the IRF3708 which with a 1K, or so, gate resistor and a 100K gate pull down resistor would work nicely.
RDSon is 13.5mΩ
ID2 * RDSon
5A2 * 13.5mΩ = 0.34W Pretty good!!!
And low VDSon
5A * 13.5mΩ = 68mV How cool is that?
No heat sink, no power driver, easy peasy.
Duane J
Hi Duane J
It's so good to see all the involved maths, well explained. Easy peasy, as you said!
+1 to the hate of Darlingtons. They were pure mess, in almost every design I tryed to use them!
Yanomani
There was a time when Darlingtons and the related Sziklai configurations ruled.
But when MOSFETs came along they pretty much took over.
Also, the IGBTs, which look similar to the Sziklai, work nicely in high voltage high current applications where the fairly high VCEsat is acceptable. Their real advantage is the relatively high stiff characteristics when turned on.
For almost any power switching application, from a few mA to many amps MOSFETs are kings.
Duane J
We tend to just want to envision a switch... plain and simple.
But the BJT works through a sandwitch of diodes and can be looked at as either a switch (saturation mode) or a linear amp (less than saturation)
While it isn't quite correct to describe at behaving as if two diodes were working together, "The Art of Electronics" uses that explaination and it at least is a more sensible point of view than just thinking of it as a switch.
There is a lot of maths, if you want to be sure you are in the linear mode and using a nice sweet spot to get good amplification; but in switching mode, it is pretty much about giving it a blast of current and voltage that is adequate to put it into a full on condition.
What you want to study is the ON Characteristics in any pdf.
The hfe number is interesting, but the pdf also usually has additional listing details for saturation near the end. Since hfe is more useful as selection of the linear mode, you will find a lot of people just dismissing it.
So for switching ... take a look at the Base-Emitter saturation voltage in the pfd. For a 2n3904, there are two situations (not all situations are provided). It this case the Ic of 50ma means it can drive a 50ma relay coil, and requires 5ma input at at least 0.95v (worst case) needed to get the transistor to go to saturation. (The Absolute Voltage Ratings in the same pdf tells us that anything above 6.0 V on the Base with destroy the transitor.
You don't need to provide 25ma to get the full ON for a 50ma load (your load was 37ma, so the 5ma from the Propeller should be fine) and any voltage between 0.95 and 3.3 VDC from the Propeller will turn it on.
So just provide a current limiting resistor between the Propeller and the 2N3904 that provides 5ma. E=IxR or R=E/I 3.3/.005 = 660 ohms
That takes care of your connection to the Propeller.
Connecting the coil is simple.
Since the transistor is rated at 200ma in the Absolute Maximum Ratings, and the coil just wants 37ma.. wire it up and it won't damage the transistor.
Since the Absolute Maximum Ratings for the Collector to the Emitter is 40 VDC, it will work with a 12 volt coil
Since there is a diode sandwitch involved. under the ON Characteristics you will see that 0.3 volts will be dropped in the diode from Collector to Emitter, so if you use 12Volts, the coil will only get 11.7 volts (which is okay).
P.S. It really helps to read and comprehend the Headings in the pdfs as well as the individual line items. It takes time and effort to understand each line, but it is well worth doing so. So things apply to switching, others apply to linear, some apply to frequency limits.
Now that I finally learned to engineer a BJT switch, I still have problems with the MOSfet pdfs. Learning never stops. The MOSfets work on a different basis from either BJTs or Tubes.
http://www.fairchildsemi.com/ds/2N/2N3904.pdf
Include also VBEsat < 1V of npn switch i.e. ( 3.3V - VBEsat ) / IB = 2.3/0.005 = 470
I now see the thread has wandered away from the 2n3904 and off into other alternatives.
The PN3055 not only needs a heat sink... it generally needs a big heat sink, and maybe a fan if you are switching power with it.
MOSfets are a whole different subject.
All that heat under a dashboard is not a good thing... so I suggested a 30amp automotive relay driven by a BJT and suggested you up-grade your 2n3904 to a transistor that can ignore 100VDC spikes that occur in automotive.. you can get a 500ma 100V transistor for about $2.00 USD.. an NTE46.. which is a tiny Darlington.
But at some point, there is the need to learn to read the pdf properly and apply the math.
I'm scrapping the TIP3055 then too. Duane suggested using a mosfet but I am just barely staring to get the hang of BJT's lol I guess I'll have to study up on them extensively if I want this project to work though.
I have decided not to touch her ignition. Especially since my knowledge is very minimal when it comes to transistors.
What o you think I have been blindly asking questions and pulling formulas out f my *** lol I have been reading links members have provided. I have been studying Duanes formulas and words carefully. I have read this tutorial http://www.electronics-tutorials.ws/transistor/tran_4.html from top to bottom numerous times! The problem is that there is conflicting info here and there. I read something then find something that says something different. This stuff is not easy ya know.
For instance, the example number one in the tutorial link i just posted says to use Vbe and not Vbesat when calculating Rb. Then Duane tells me otherwise. Then it tells me, "Again using the same values, find the minimum Base current required to turn the transistor "fully-ON" (saturated) for a load that requires
and the formula it provides is
Duane says that beta is not used to calculate Ib when using the BJT as a switch. Thats only used in amplification. I have not only heard it from just him but i have read it elsewhere too. Point being that I have come across a lot of contradicting information. Just when I think I have it down something contradicting comes along and makes me rethink everything and then I come here to ask another question lol
I now see that in many ways we have been to focused on your request and not enough of providing the educational framework to easily learn.
Switching higher voltages and over an amp of current is more demanding, often one transistor or even one Mosfet stage won't do it.
With the BJTs we end up with a lot of heat to manage, and more so with Darlingtons.
I see you want to now switch something else with a 9V supply. To get the job done in two stages, a transistor and a relay is often less demanding than two stages of BJTs or two stages of Mosfets, or a mixed low stage BjT with higher stage MOSfet.
I was just trying to make the point that some of the conflicting information you are getting is because the BJT is a multipurpose device. it can be used for either switching or linear amplication or even as an oscillator. The pdf provides numbers for all three in different sections, and an ever present Absolute Maximum Rating sections to tell you what will destroy the device.
The biggest problems with the numbers presented is that the transistors do vary a lot in production, and the On conditions are curves, not tidy linear lines that fit easily into formula.
=+++++++++++++++
So real question is how to I best help you with your desire to switch something?
Do you want to use an all BJT soluion inspite of heat or do you want to use a BJT to relay solution to get rid of the heat problems?
if you simply want to switch 9VDC at several amps, a 9 volt relay may be your best choice.
If you go to BJT, a TIP120 Darlington will do the switching, but the darned 2.0 internal voltage drop means that your 9 volt power supply is not enough.
You would have to rush out and buy an 11VDC power supply to get a 9VDC output. And since nobody sells 11VDC power supplies, you end up with another project added on to the first.
Mechanical or Solid-state relays begin to look very attractive for a quick and clean build..
I usually go to a BJT/ Relay combination as i can easily control 240VAC at 12 amps or about 24VDC at 12 amps with one transistor driving a 12VDC relay coil. And I use the 2N2222 instead of the 2N3904, because it can handle somewhere from 700ma to 1amp, instead of 200ma, while being driven directly from +3.3 or +5..0 logic.
THe 2N3904 is possibly the smallest transistor you might bother with for switching circuits. Sooner or later you might realize that you can narrow your transistors to just a few that are most handy. And the 2n2222 is ordinarily a good replacemetn for the 2n3904.
Duane is a reliable source and a good mentor.. better than myself. Part of the problem is that these days everyone writes something on the internet ... even if they don't know anything. So find reliable sources and work closely with them.
But also realize that using linear algebra to fit a transistor is inexact and the goals are to merely get something that won't burn up and will work in your context.... every transistor in reality generates a series of curves.
Yes my PS2. I have been programming for 5 or so years. Give or take. I really took a liking in C programming. Which has been my programming language of choice ever since I have started. I wrote a server and a client that I altered to work with this project. The server(ethernet shield on my MCU) takes strings like "on" or "off" that are sent from the client that is running on my phone and acts accordingly. So if i send the word on to the ethernet shield, it will send a signal to the output pin on my MCU. I want to be able to switch on my PS2 this way.
So I figured, i would use a BJT with a 9VDC 5mA SPDT relay to achieve this goal. Even though Duane suggested the use of a mosfet. Duane has been very very informative and very helpful but I am just barely getting the hang of NPN BJT's. I think learning about Mosfets would be counter productive until I get the BJT's down.
By helping me do the above.
BJT with relay.
Couldn't i just use the TIP3055 with a 9VDC 5mA SPDT relay??
The PS2 runs on 8.5-9v 11VDC would be too much for my system no??
I do not follow could you please explain?
I usually go to a BJT/ Relay combination as i can easily control 240VAC at 12 amps or about 24VDC at 12 amps with one transistor driving a 12VDC relay coil. And I use the 2N2222 instead of the 2N3904, because it can handle somewhere from 700ma to 1amp, instead of 200ma, while being driven directly from +3.3 or +5..0 logic.
Good to know. I will use the 2n2222's from now on.
Agreed!
So, here are some questions to try and move forward. I had no idea that i was supposed to look at the data sheet to calculate all of this stuff. I was reading that link that one of the members posted here.
Question 1: Do I need a relay that matches the power of the object I am trying to control? Like I want to switch on my ps2. The ps2 uses a 9v 5a current. SO I would assume that i would need a 9v 5A SPDT relay. The transistor just needs to be able to handle this load and i do that math to get it into saturation. Is that right?
Where do I start? With the Ic? In order to find the Ic, Ic = Vcc / RL(relay coil resistor). Sound about right?
Then I calculate the base current according to the TIP3055 datasheet? If the Vbesat for the BJT is 1.8, Then -> Rb = (Vin - Vbesat) / Ic -> 3.2 / (lets say 5A) = .64 Ohms @ 16W.
So am I missing anything? Will using the TIP3055 with a relay work?? Not sure where to find a SPDT 5VDC relay with a nominal coil rating of 5A though. Should I go about this different? Should I scrap the BJT and start studying mosfet's?