Selecting a BJT transistor to act as a switch for a 3.3V STM32 microcontroller involves matching the transistor's electrical characteristics (current, voltage, gain) to the load, while ensuring the 3.3V GPIO pin can provide enough base current to drive the transistor into saturation. A common, reliable choice for low-power, low-side switching (loads up to ~500mA) is the
2N2222A or 2N3904 (NPN).
Key Selection Criteria
-
Collector Current IC: The transistor's maximum collector current (IC,max) must be greater than your load's operating current.
-
Collector-Emitter Voltage (VCEO): Must be higher than the load's supply voltage.
-
DC Current Gain (β or hFE): The transistor needs high gain at the required collector current, as STM32 GPIO pins are limited to 3.3V and usually around 20-25mA maximum current (often lower for sustained usage).
-
Saturation Voltage (VCE(sat)): (Collector-Emitter Saturation Voltage) absolutely matters when selecting a BJT transistor to be driven by an STM32 or any microcontroller. It directly dictates the efficiency, heat generation, and voltage drop of your switching circuit.
-
It represents the lowest voltage drop across the transistor's main terminals when it is acting as a closed switch.
The voltage delivered to your load is Vsupply - VCE(sat). So if you are switching a 3.3V load with an NPN BJT (emitter to ground) and the VCE(sat) is high (e.g., 0.5V), your load only receives 2.8V, which might not be enough for some components.
-
For silicon NPN transistors, this value is usually around 0.2V–0.3V, while Germanium transistors are lower, typically around 0.1V.
To get the exact value check the datasheet, look at the graphs for VCE(sat) vs. IC at the specific current you need.
-
Low VCE(sat): A lower VCE(sat) is desirable for high-efficiency switching, as it reduces power loss (Ploss=IC ✕ VCE(sat)).
A low VCE(sat) BJT (often < 0.2V) reduces this heat, improving battery life and preventing the component from overheating. Also low VCE(sat) transistors often have higher current gain (hFE) at low saturation voltages, meaning they can switch higher currents (IC) with lower base drive currents (IB), making them better suited for direct GPIO driving.
-
High VCE(sat): If the voltage drop between collector and emitter is high (e.g., 0.7V - 1.0V), a lot of power (P=IC ✕ VCE(sat)) is wasted as heat in the transistor.
-
VCE(sat) Impact:
|
VCE(sat) Level |
Typical Value |
Efficiency |
Heat |
Best Used For |
|
High |
0.5V - 1.0V |
Low |
High |
Low current, non-battery, low-cost
(e.g., 2N2222) |
|
Low |
< 0.2V |
High |
Low |
Battery-powered, high current, efficient switching
(e.g., NSS40201L, 2STR1215) |
2. Steps to Select and Design the Circuit
-
Determine Load Current (IC)
Find the maximum current your load (relay, LED, motor) will draw. For example, a 12V relay drawing 100mA means
IC=100mA.
-
Choose an NPN Transistor
For low-side switching (load connected to
VCC), transistor between load and ground), choose an NPN. Examples include:
-
2N2222A (NPN): Excellent general-purpose, up to 600mA–800mA.
-
BC547 (NPN): Suitable for small signal/LEDs (<100mA).
-
TIP120 (Darlington): Suitable for high currents (>1A),but has higher VCE(sat).
C.
Calculate the Base Resistor (RB)
To use the BJT as a switch, it must be in
saturation, not in the linear (active) region. A reliable rule of thumb is to assume a forced beta (β
forced) of 10 to 20, or set the base current (
IB) to 1/10th of the collector current (
IC).
1. Desired IB: (e.g., 100mA/10=10mA).
2. Calculate RB: Use VGPIO = 3.3V.
R
B= (V
in- V
BE)/I
B → R
B= (V
GPIO- V
BE)/I
B
Assume V
BE is 0.7V for a saturated BJT.
=260Ω
Use a 220Ω or 270Ω resistor to ensure strong saturation.
What will be happen if we use a too small value of the RB resistor
If the value of the base resistor (R
B) is too small, it can cause
immediate or long-term damage to both the STM32 and the transistor.
1. Damage to the STM32 GPIO Pin
The most critical risk is exceeding the STM32's maximum output current per pin (typically 20mA to 25mA).
-
Overheating the Silicon: Since the BJT's base-emitter junction acts like a diode (dropping only ~0.7V), a tiny resistance will allow too much current to flow from the GPIO. The Excessive current can burn out the internal GPIO driver circuitry, permanently "killing" that specific pin or even damaging the entire microcontroller due to internal heat.
2. Damage to the Transistor
-
Excessive Base Current (IB): Every transistor has a maximum IB limit in its datasheet. Exceeding this can destroy the base-emitter junction.
-
Thermal Runaway: High base current contributes to internal heating. As a BJT gets hotter, its internal resistance decreases and gain (hFE) may increase, which can pull even more current until the device destroys itself.
How to avoid this
Always use the formula xxxxx and ensure your chosen
IB is well below the STM32's 20mA limit. For most small-signal BJTs (like the 2N2222 or 2N3904), a resistor between 470Ω and 2.2kΩ is a safe starting point for a 3.3V system.
3. Essential Circuit Components
-
Base Resistor (RB): Limits current from the STM32 pin, preventing damage to the MCU.
-
Flyback Diode (Required for Inductive Loads): If switching a relay, motor, or solenoid, connect a diode (e.g., 1N4007) in reverse parallel across the load. This protects the BJT from high voltage spikes when switching off.
-
Pulldown Resistor (Optional): A 10kΩ resistor from Base to Ground ensures the transistor stays OFF during STM32 startup or reset.
Summary Table for 3.3V STM32
|
Parameter |
Suggestion |
Reason |
|
Transistor Type |
|
|
|
|
|
|
Note: If driving more than ~ 500mA, a Logic Level MOSFET (e.g., IRLML2502) is a better, more efficient choice than a BJT, as it requires no gate current.