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.
Możesz tak ustawić rzeczy, aby przełącznik był:
- Na niskiej stronie (blisko ziemi) lub
- Po stronie wysokiej (w pobliżu napięcia sterującego przekaźnika lub innego urządzenia) lub
- Po obu stronach (mostek H, obciążenie związane z mostem itp.)
Ale naprawdę musisz mieć dobry powód, aby wybrać (2) lub (3) powyżej. Obejmują więcej części i często są niepotrzebnie skomplikowane, jeśli nie masz dobrego powodu. Tak więc przełącznik niskich częstotliwości jest pierwszym wyborem do sprawdzenia czegoś takiego.
Aby zaprojektować dowolny przełącznik, zacznij od specyfikacji tego, co musisz prowadzić, oraz specyfikacji tego, co masz do prowadzenia.
Spójrzmy na arkusz danych ESP8266 :
Tutaj widać, że bieżąca zgodność dla pinów we / wy ma maksymalną wartość IMAX=12mA
VCC=3.3V
VOH≥2.64V(Voh Min)
IMAX.) They also guarantee a low output voltage of 80% of that, or
VOL≤330mV(Vol Max)
(This means, when sinking
IMAX.)
Let's now look at a typical relay datasheet:
From here you can see that the resistance is 125Ω and that the required current is 40mA.
(Another detail is that it requires at least 70% of the specified voltage to engage, which confirms that a BJT switch-mode, saturated VCE 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 VCE 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 VCE. 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 β 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 100mA. 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:
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 β=ICIB=10. (You can see this in two places: the lowest curve on the chart which is the value of VCE when saturated, where they specify ICIB=10 and also the top-most curve which they identify in the same way.) So this means
IB=4mA(Ib)
which is well within the limitations of your I/O pin. So that's nice. The second is that
VBE≈800mV(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:
simulate this circuit – Schematic created using CircuitLab
The value of R1 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, Voh Min. Also, you know the typical value for the base-emitter voltage of the BJT from above, Vbe. And finally, you also know the likely base current, Ib. So just compute:
R1=2.64V−800mV4mA=460Ω(R1)
The nearest value would be 470Ω. 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.3V when driving high. Then the I/O pin and BJT base current will be 3.3V−800mV470Ω≈4.4mA. 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 R1 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 100mA 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 β than I earlier suggested from reading Figure 11. Let's look at Figure 4, now:
Here, you can see a curve labeled 150mA, which is more than you need. The x-axis is the base current IB, and the y-axis is VCE. You want a low value for VCE and you can see that it plateaus out at around 100mV. Keeping in mind that these are typical curves and not guaranteed curves, you can see that using IB≈8textrmmA looks pretty solid (far away from the curve knee) and that 10mA is even better. Well, this suggests that β from about 15 to 20 is probably going to work pretty well.
Taking all this together for your relay at 100mA, 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 IB=4mA to perhaps IB=5mA to IB=6.7mA is just fine.
Let's re-compute the earlier equation for R1:
R1=2.64V−800mV5mA=368Ω(R1 redo 1)
R1=2.64V−800mV6.7mA=275Ω(R1 redo 2)
Between these two? I'd just go with R1=330Ω. I think that would be sound. Worst case I/O pin current should be approximately 7.5mA. This is well below the maximum of 12mA 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.)