18
\$\begingroup\$

I'm a hobbyist, and never got past the datasheets/tutorials for FET transistors; I'm a BJT man. I never found discussions dealing with BJT vs. FET and specific applications best suited for each type. My projects are very simple switching and logic gate style circuits. So once I got BJTs to satisfy a project's requirements, I just stayed with what was working. I have spent the afternoon researching this on EE-SE and found lot's of good stuff. I found that FETs seemed to the more popular choice for level-shifters. I was hoping someone could provide a "for dummies" explanation regarding the strengths/weaknesses and trade-offs involved with FETs and BJTs in some common applications.

I chose this level-shifter for my project: I want to drive a 5V relay using an ESP8266 which has 3.3V GPIOs. I measured the relay's coil current to be right about 100mA. I want to use an S8050 and a minimum of parts, requirements are not high. I'm just using the ESP8266 to read the pin on a PIR sensor and also read some toggle switches to control a light using a relay. Is the above circuit a good choice? I designed my own circuit, but am not going to use it. Still, it would help my understanding if someone would kindly provide an analysis of my design, which was based on some hunches, guesses, and perhaps a bit of voodoo.

Just briefly, I reasoned that my base-current (GPIO output 3.3V - 0.7V base of Q1) / 1K ohm of R2 = 2.6mA would not be affected much by the current in the voltage divider R1/R3 which I think is 5 / (100K +100K) = 25uA. I don't know how the junction of R1, R2, R3 and U1's base will work; I guessed that the base of U1 will pull the 2.5V of the divider down to 0.7V, but wasn't sure how it would affect the 2.6mA that the GPIO sources. That's why I went with the circuit I linked.enter image description here

\$\endgroup\$
3
  • 1
    \$\begingroup\$ What is R1 doing? \$\endgroup\$ Aug 19, 2017 at 22:24
  • \$\begingroup\$ This is where the voodoo comes in: it's just something that looks familiar from various circuit web-sites. After consulting my magic 8-ball, I thought I'd try "biasing" the circuit. Mainly, I just wanted to keep the GPIO pin from exceeding 3.3V. Like I said, "voodoo" (or perhaps superstition... whatever). \$\endgroup\$
    – user160409
    Aug 19, 2017 at 22:34
  • 5
    \$\begingroup\$ I suspect part of the popularity of FETs is that, being voltage rather than current driven, and most people using switching applications, they're easier to understand in many respects. Having to think in currents with BJTs can be a bit head-scratchy. The ironic disadvantage sometimes with FETS being that you need that voltage to apply to the gate, and you haven't got extra volts above your positive rail, etc. \$\endgroup\$
    – Ian Bland
    Aug 20, 2017 at 0:41

5 Answers 5

20
\$\begingroup\$

Ray. Yes, there are hundreds if not thousands of good pages on using BJTs for pretty much any kind of switching arrangement you can imagine. They also work fine as level shifters, though despite your use of that phrase I actually don't think that's your situation here. If you want to look at an example of level shifting using BJTs, you can see my answer here.

Below, rather than give you a fish, I'll try and teach you to fish.


For situations involving current compliance that exceeds your I/O pin (like a relay) or also a different, higher driving voltage than your I/O pin can handle (again, like your relay), or also where you need some protection against inductive kickback (once again, like your relay) you will probably want to use an external BJT or FET as a switch.

You can arrange things so that the switch is:

  1. On the low side (near ground), or
  2. On the high side (near the driving voltage for your relay or other device), or
  3. On both sides (H-bridge, bridge-tied load, etc.)

But you really need to have a good reason for choosing (2) or (3), above. They involve more parts and often unnecessarily complicated if you don't have some good reason. So the low-side switch is the first choice to examine for something like this.


To design any switch, you start with the specifications of what you need to drive and the specifications of what you have for driving it.

Let's look at an ESP8266 datasheet:

enter image description here

Here, you can see that the current compliance for an I/O pin has a maximum value of \$I_{MAX}=12\:\textrm{mA}\$. This means you should plan to stay well under that value. I like to stay below half of the maximum, with still less being better if I can manage it. Less is better because if you are using several different I/O pins like this at the same time, the loading adds up and there are dissipation limits for the entire port and for the entire device, too. Even if they aren't stated, they exist. So keep things as low as possible.

Also take note of the voltage limits. Assuming you are operating on \$V_{CC}=3.3\:\textrm{V}\$, then they guarantee a high output voltage of 80% of that, or $$V_{OH}\ge 2.64\:\textrm{V}\label{voh}\tag{Voh Min}$$ (This means, when sourcing \$I_{MAX}\$.) They also guarantee a low output voltage of 80% of that, or $$V_{OL}\le 330\:\textrm{mV}\label{vol}\tag{Vol Max}$$ (This means, when sinking \$I_{MAX}\$.)

Let's now look at a typical relay datasheet:

enter image description here

From here you can see that the resistance is \$125\:\Omega\$ and that the required current is \$40\:\textrm{mA}\$.

(Another detail is that it requires at least 70% of the specified voltage to engage, which confirms that a BJT switch-mode, saturated \$V_{CE}\$ drop of perhaps a few tenths of a volt is "affordable." If you don't understand what I mean, or why I say it, when I write 'switch-mode, saturated \$V_{CE}\$ drop' then you need to stop and think about this. It's important. When operating a BJT as a switch, you cannot afford a large-magnitude \$V_{CE}\$. You want this to be as small as practical so that it really does work like a switch. But to achieve small magnitudes there, you need to operate it 'saturated,' which means the applicable \$\beta\$ will be low.)


The above bits of data say that you really do need an external switch for all the reasons mentioned earlier. You need it because it requires more current compliance then your I/O pin can provide, because you want to protect your I/O pin from back-emf from the relay's inductance, and because the relay requires a higher voltage than your I/O pin can provide. Don't even think of using the I/O directly!

You also can use almost any BJT, because of the low current needed by the relay.

(Your relay may require more current. But even if it is twice as much, most BJTs can handle that relatively easily. Regardless, you need to say so, if so. [EDIT: I think you've indicated in comments below my answer that the measured current is \$100\:\textrm{mA}\$. That should be okay. But it changes some of the values I write below.)

In this case, I'd use what I have a lot of: OnSemi PN2222A devices. Let's start by examining Figure 11:

enter image description here

Look at Figure 11 and you can get a lot of important information. The first is that they "recommend" operating it as a switch with \$\beta=\frac{I_C}{I_B}=10\$. (You can see this in two places: the lowest curve on the chart which is the value of \$V_{CE}\$ when saturated, where they specify \$\frac{I_C}{I_B}=10\$ and also the top-most curve which they identify in the same way.) So this means $$I_B=4\:\textrm{mA}\label{ib}\tag{Ib}$$ which is well within the limitations of your I/O pin. So that's nice. The second is that $$V_{BE}\approx 800\:\textrm{mV}\label{vbe}\tag{Vbe}$$ with that collector current. (Just look along the x-axis to find the collector current, then look up to where the curve intersects a y-axis value.) This last detail will be used in the design.

Time to prepare a schematic:

schematic

simulate this circuit – Schematic created using CircuitLab

The value of \$R_1\$ is pretty simple to compute. First, assume that the I/O pin is operating at its lowest output voltage when high. You already know this value from above, \$\ref{voh}\$. Also, you know the typical value for the base-emitter voltage of the BJT from above, \$\ref{vbe}\$. And finally, you also know the likely base current, \$\ref{ib}\$. So just compute: $$R_1=\frac{2.64\:\textrm{V}-800\:\textrm{mV}}{4\:\textrm{mA}}=460\:\Omega\label{r1}\tag{R1}$$

The nearest value would be \$470\:\Omega\$. So that is what you see in the schematic. The diode, of course, provides a path for the relay's magnetic field energy to collapse, when you try to turn it off. It otherwise doesn't conduct.

Say your I/O pin is more powerful than we assumed and holds a full \$3.3\:\textrm{V}\$ when driving high. Then the I/O pin and BJT base current will be \$\frac{3.3\:\textrm{V}-800\:\textrm{mV}}{470\:\Omega}\approx 4.4\:\textrm{mA}\$. This is also just fine and won't hurt anything at all. So this design should work well.

There are reasons for adding a resistor to ground, from the BJT base. One is that it helps keep the base near ground if for some reason the other end of \$R_1\$ were floating and not connected to your ESP8266. And there are other reasons. But it's not vital here, so I'll leave out the discussion of it for now.

EDIT: With you indicating (in comments below) value of \$100\:\textrm{mA}\$ for the relay, which is 2.5 times as much as I'd used above, you could consider the idea of using 2.5 times the base current. But also most of these small signal BJTs can work well as a switch with higher values of \$\beta\$ than I earlier suggested from reading Figure 11. Let's look at Figure 4, now:

enter image description here

Here, you can see a curve labeled \$150\:\textrm{mA}\$, which is more than you need. The x-axis is the base current \$I_B\$, and the y-axis is \$V_{CE}\$. You want a low value for \$V_{CE}\$ and you can see that it plateaus out at around \$100\:\textrm{mV}\$. Keeping in mind that these are typical curves and not guaranteed curves, you can see that using \$I_B\approx 8\:\textrm{mA}\$ looks pretty solid (far away from the curve knee) and that \$10\:\textrm{mA}\$ is even better. Well, this suggests that \$\beta\$ from about 15 to 20 is probably going to work pretty well.

Taking all this together for your relay at \$100\:\textrm{mA}\$, you need about 2.5 times as much base current because of the increased relay load but you can afford to drop it by a factor of from 1.5 to 2.0 because of the Figure 4 curve. So perhaps going from the earlier computed \$I_B=4\:\textrm{mA}\$ to perhaps \$I_B=5\:\textrm{mA}\$ to \$I_B=6.7\:\textrm{mA}\$ is just fine.

Let's re-compute the earlier equation for \$\ref{r1}\$: $$R_1=\frac{2.64\:\textrm{V}-800\:\textrm{mV}}{5\:\textrm{mA}}=368\:\Omega\label{r1x}\tag{R1 redo 1}$$

$$R_1=\frac{2.64\:\textrm{V}-800\:\textrm{mV}}{6.7\:\textrm{mA}}=275\:\Omega\label{r1y}\tag{R1 redo 2}$$

Between these two? I'd just go with \$R_1=330\:\Omega\$. I think that would be sound. Worst case I/O pin current should be approximately \$7.5\:\textrm{mA}\$. This is well below the maximum of \$12\:\textrm{mA}\$ for the ESP8266 datasheet table I show above, but enough under it that I wouldn't be too concerned. (At least, not unless I knew I was repeating this driver across a large number of I/O pins. In that case, I'd probably go look to see if there was a specified limit for the port or device as a whole.)

\$\endgroup\$
8
  • \$\begingroup\$ Excellent answer! This will be the circuit I use, I already have some 2N2222A that I've scavenged. I thought I had this material down pat, but I'm glad you went into the detail that you did because I see that I'm a little shaky on some things: First, I'll be paying attention to the guaranteed values for high level output voltage and factor in the 80% (or whatever is the case), instead of just using the full 100% in my calculations. The thing that really surprised me was your use of the beta parameter of collector current / base current. I've been using hFE all this time. I glossed over \$\endgroup\$
    – user160409
    Aug 20, 2017 at 1:55
  • \$\begingroup\$ the calculations in my question, so: I measured 100mA through the relay using a 5V supply (I can't get the datasheet because I've glued over the printing). I multiplied this by a suggested 2X- 5X safety margin, so I settled on 260mA. Isn't that what I use for collector current? I divided this by the hFE of 100 to get a base current of 2.6mA. So here's where I'm all confused: thought hFE was the current gain of the base to the collector. Rearranging beta = Icollector / Ibase gives base current X beta = collector current. Where have I slipped? I'm also puzzled by the graph in Fig. 11, \$\endgroup\$
    – user160409
    Aug 20, 2017 at 2:58
  • \$\begingroup\$ @Ray71 You have to over-drive the BJT if you want it acting like a switch. Look at Figure 11. You can see curves with \$V_{CE}=10\:\textrm{V}\$ and this would be with a \$\beta\$ you are used to. But the other curve is for "saturation." Which is what you want with a switch. If your relay current is \$100\:\textrm{mA}\$ then I think you will be fine using a \$\beta=15\$ or a little higher, perhaps. Which makes it still work for your I/O pin. \$\endgroup\$
    – jonk
    Aug 20, 2017 at 3:08
  • \$\begingroup\$ At the top there are 3 plots, 2 of which are well labelled, but the third one simply says, "1.0 V". Even though I used the one labelled, "Vbe(sat) @ Ic/Ib = 10" I'm curious about the "1.0V'. For the protection diode, I been in the habit of using ones in the range 1N4001-1N4007. How much does this matter. Again, out of ignorance, I thought that being "beffier" than the delicate looking 1N4148 meant it was more "heavy duty". I'm on board with the 1N4148 from now on, just wondering about the difference in behavior. I've inferred from he schematic that the diode's cathode should connect to the \$\endgroup\$
    – user160409
    Aug 20, 2017 at 3:14
  • \$\begingroup\$ @Ray71 The BJT becomes increasingly saturated as \$V_{CE}\$ gets less than a volt. You want to drive \$V_{CE}\$ well below a volt. So you must approach this as a saturated situation and you don't get to use the curves where \$V_{CE}=1\:\textrm{V}\$ or \$V_{CE}=10\:\textrm{V}\$, obviously. Those would be very bad switches. Feel free to use any diode that works for you. Most will survive. I didn't want to get into the details of calculating the inductive kick back. (It can be done.) \$\endgroup\$
    – jonk
    Aug 20, 2017 at 3:22
10
\$\begingroup\$

You don't need this "voodoo". Both R1 and R3 are unnecessary here. A bipolar transistor is working on currents, not voltages. These resistors are only needed to bias the transistor into its linear region for linear amplifiers. You don't want linear amplification, you want high-efficiency switching.

schematic

simulate this circuit – Schematic created using CircuitLab

The emitter-base voltage \$U_{BE}\$ depends on the collector current but in general, it's about 1V. So, with 3.3V on its base and a 1k base resistor, you have about 2mA base current.

Use a switching transistor, these have a high beta value and go into saturation at very low input currents. You may also consider a darlington type for higher loads. Saturation leads to lower voltage drop and less heat production in the transistor.

\$\endgroup\$
4
\$\begingroup\$

FETs don't saturate. Thus a big speed win.

And a bipolar Vbe is pretty much set at 0.5--0.7volts, for useful currents.

Whereas a FET happily allows 1 or 2 or 5 or 10 volts between gate and channel. Thus a big win for flexibility of operation.

\$\endgroup\$
2
\$\begingroup\$

A general comparison of BJTs and FETs:

BJT: - Current-controlled device - Charge carriers are both electrons and holes (hence bipolar) - Physically larger - Very little input capacitance (can give higher speed/higher frequency amplification) - More linear amplification since gain doesn't depend on bias voltage - Can have lower output impedance, and therefore drive low-impedance loads easier - Generally higher power consumption due to current control

FET: - Voltage-controlled device (lower power consumption, only draw power when switching state generally) - Charge carriers are either electrons or holes (depending on type, hence unipolar) - Physically smaller - Can scale easier (half drain current by halving gate size) - Generally higher input capacitance and Miller Effect means that as gain goes up, so does input capacitance - Can't drive low-impedance low very well (usually needs buffer stage) - Generally lower power consumption

This is by no means a complete list of differences, but hopefully answers your question as to the differences between the two types of transistors. In my educational experience, it seems that 95% of the time for hobbyist projects, BJTs are the way to go, but for large-scale, high density projects, CMOS is the primary choice since most digital circuits are CMOS, and therefore it's cheaper to produce both analog and digital in the same process.

\$\endgroup\$
0
\$\begingroup\$

In some applications, energy efficiency is very important. Even though there are many applications where it doesn't really matter, many people don't like needlessly limiting designs to the latter applications.

If one needs to have a single-BJT-based circuit that is capable of switching 100mA, that circuit will probably need to draw somewhere between 2-10mA whenever it's supposed to be on, whether the load current is actually 100mA or zero. If the load will actually draw 100mA any time it's on, adding even 10mA to the power draw of the system at that time would only increase overall power consumption by 10%. If, however, the load might often be driving something that only takes 1mA, adding even 2mA to the power draw when it's on would triple the power consumption related to controlling that load. If the load will be switched on most of the time (but simply draw very little current) that could be very wasteful.

BJTs have been widely available longer than MOSFETs, and many circuits are designed around that availability. I don't know that any particular MOSFET is quite as ubiquitous as the 2N3904 and 2N3906. Those parts are nowhere near the best transistors on the planet, but they're everywhere. I don't know of any MOSFETs of which one can say the same.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.