Transistor base resistor calculator
The usual formula divides by the transistor's typical gain and leaves it on the edge of saturation. This one picks the base resistor that keeps an NPN switch fully saturated at every worst-case corner of drive voltage, VBE and tolerance, checks it against what the pin can supply, and counts the pull-down resistor and the heat.
Load (low side, collector to supply)
Drive (the pin)
From the datasheet
| Base resistor | Base, lowest | Forced gain | Pin, highest | Verdict |
|---|
How this is calculated
A transistor used as a switch should be saturated: driven with more base current than the load needs, so the collector sits a fraction of a volt above ground whatever the gain of the part in your hand. The ratio of collector to base current you design for is the forced gain. The base current needed is the load current divided by it, and the resistor sets that current from the voltage left across it once the base-emitter junction has taken its share:
IB = (Vdrive − VBE) / RB − VBE / RBE
The last term is the current the pull-down resistor takes from the base, if you fit one. The page evaluates that at every corner: the drive at its lowest, VBE at its highest and both resistors at the wrong end of their tolerance give the least base current, and it must still reach IC divided by the forced gain. That fixes the largest base resistor that works. The opposite corner, the drive at its highest, VBE at its lowest and RB at its low end, gives the most current the pin has to supply, and that fixes the smallest. The page buys the largest catalogue value inside the window, because it costs the pin the least.
With the defaults (a 400 Ω, 12 V relay coil from a 3.3 V pin, a 2N2222A and a 10 kΩ pull-down) the coil draws 30 mA, the base needs 3 mA, and the window runs from 341 Ω to 539 Ω. The page buys 510 Ω, which gives at least 3.18 mA and asks the pin for at most 5.35 mA.
Why dividing by hFE is the wrong formula
Most base resistor calculators work out IC/hFE and stop. That is the current that just brings the transistor to the edge of saturation, on a part with exactly that gain. Gain varies about three to one between parts of the same type, falls at low temperature and falls again at high current. A part at the bottom of its range is then not saturated at all: it sits in its linear region with several volts across it, the load gets less than its supply, and the transistor dissipates the difference. A relay that chatters or a transistor that runs hot with a light load is usually this.
The datasheet answers the question directly. It specifies VCE(sat), the voltage across a saturated transistor, at a forced gain of 10: for the P2N2222A, 0.3 V maximum at 150 mA with 15 mA of base current. Design to that ratio and the saturation voltage is guaranteed. A forced gain up to about half the minimum hFE still saturates in practice, and the page accepts it, but it says when VCE(sat) is no longer the datasheet's figure. Above the minimum hFE it says the switch is not guaranteed to turn fully on.
The gain the forced-gain check compares against is the minimum hFE at your current, and at a low collector-emitter voltage where the datasheet gives one. For the two built-in parts the page reads it from the onsemi tables at the test currents either side of your load and takes the lower. VCE(sat) and VBE(sat) come from the first test current at or above your load, since both rise with current. Beyond the last test current the page says so: the datasheet no longer covers the design.
The two numbers that set the worst case
VBE(sat) is specified as a range, 0.6–1.2 V for the P2N2222A at 150 mA. The top of that range matters most when the drive is low: from a 3.3 V pin, the 0.6 V difference is a third of the voltage across the base resistor. The other is the pin's own high level. A microcontroller output sags under load, and its datasheet guarantees only a minimum, for example VDD − 0.4 V at a stated current. Enter that minimum as the worst high level, and the supply's own tolerance with it.
The pin current limit is the one in the microcontroller's datasheet for a single pin, not its absolute maximum. If the window is empty, the pin cannot supply the base current the forced gain asks for: the page then names the forced gain the pin can afford and whether it still saturates.
The pull-down resistor
While a microcontroller resets, its pins float. A base left floating can pick up enough current through leakage and stray coupling to half turn on a relay or a heater. A resistor from base to emitter holds it off. It costs base current: VBE/RBE, 0.12 mA at 1.2 V with 10 kΩ, which the page includes. It also speeds turn-off by pulling the stored charge out of the base. Set it to 0 to leave it out.
When the pin can't supply enough
At 30 mA, a 3.3 V pin drives a small transistor with room to spare. At 500 mA it would need 50 mA of base current at a forced gain of 10, and no microcontroller pin supplies that. Three ways out: raise the forced gain toward half the minimum hFE and accept a VCE(sat) the datasheet doesn't promise; add a second transistor or use a Darlington, whose gain is the product of two but whose saturation voltage is close to a volt; or use a logic-level MOSFET, whose gate draws no steady current at all. Above a few hundred milliamps the MOSFET is usually the answer.
The heat is small when the transistor is saturated: the load current times VCE(sat), plus the base current times VBE. At 30 mA it is a few milliwatts. A TO-92 package dissipates about 200 °C/W to the air, so 300 mW is already a 60 °C rise. A part that is not saturated dissipates far more, which is why this page checks the forced gain before anything else. For a larger part on a heatsink, the heatsink calculator takes it from there.
Relays, motors and the flyback diode
A coil resists any change in its current. When the transistor turns off, the coil drives its current on regardless, and the collector voltage rises until something conducts: without a diode, that is the transistor's collector-emitter junction breaking down, tens of volts above the supply. Fit a diode across the coil, cathode to the supply, rated for the coil current. It makes the relay release a little more slowly, and a zener in series with it restores the speed if that matters. Tick inductive load and the page reminds you.
For an LED, the same switch works, with the LED's own series resistor in the collector: the LED resistor calculator sizes it, with VCE(sat) as one more drop in series.
PNP transistors and the high side
A PNP switch between the supply and the load works the same way with the polarities reversed: its base must be pulled below the emitter by VBE to turn it on, and brought back up to the emitter to turn it off. That last part is the trap. A 3.3 V pin can't pull the base of a PNP on a 12 V rail up to 12 V, so the transistor never turns off. The fix is an NPN like the one on this page, driving the PNP's base resistor to ground, with a pull-up from the PNP's base to its emitter. Then this page sizes the NPN, and the PNP's base resistor follows from the same formula with 12 V in place of the drive.
FAQ
What base resistor do I need for a relay on a 3.3 V pin?
For a 12 V relay with a 400 Ω coil and a 2N2222A: 510 Ω, with a 10 kΩ pull-down from base to emitter and a diode across the coil. A 5 V relay draws more, often 70–90 mA. With a 70 Ω coil the page buys 220 Ω, which asks a 3.3 V pin for up to 12.4 mA, more than the 8 mA default limit: check your microcontroller's figure, or raise the forced gain.
Is 1 kΩ a good base resistor?
From a 5 V pin it gives about 3–4 mA, which saturates a small transistor at a forced gain of 10 for loads up to about 30 mA. From a 3.3 V pin it gives at least 1.6 mA, and at the worst corner it saturates a 2N2222A at a forced gain of 10 only up to 15 mA. It is not a universal value; enter yours in check the one I have to see where it stands.
Why does my transistor get hot when it switches a load?
Almost always because it is not saturated: the base current is too small for the load, so several volts sit across the transistor at full current. Check the forced gain at the worst corner. The other cause is a load past the transistor's rating, or a missing flyback diode on a coil.
Should I use a 2N2222A or a 2N3904?
The 2N3904 is rated for 200 mA and its gain falls off by 100 mA; the P2N2222A is rated for 600 mA. Below about 50 mA either works. Pick the part the page reports inside its datasheet's table at your current.