Introduction
There are many ways to drive a relay, such as using a transistor or a 2003 chip. Using a transistor to drive a relay is a relatively cost-effective circuit drive method and is widely used. The core working principle of this circuit is to operate the transistor in its saturation/cutoff region, allowing it to act like a switch to control the relay's operating state. Today, we will introduce the selection and calculation of all the components involved.

Part 02
Calculation and Selection
We will use the following schematic as an example to illustrate the selection:

The Vin voltage typically comes from an MCU IO port, such as a 5V microcontroller. When the IO port outputs 0V, the transistor is cut off, and the relay stops operating. When the IO port outputs 5V, the transistor is turned on, and the relay operates.
1. RB Base Resistor Selection The RB resistor is used to limit current. What is current limiting? First, because the output current capability of the MCU IO port is limited, we use a series RB resistor to prevent the output current from exceeding its maximum output current limit. Second, for a transistor to operate in its saturation region, β*IB > IC. Therefore, the series RB resistor is used to set the transistor's IB current, ensuring operation within the saturation region. Knowing the function of the RB resistor, the calculation becomes straightforward:
IC_max / β_min: β_min is the minimum gain of the transistor, and I_IO_MAX is the maximum output current limit of the MCU IO port. These two values can be found in the transistor and MCU datasheets.
Knowing β_min and I_IO_MAX isn't enough; we also need to know the maximum collector current, IC_max:
IC_max = (Vcc - Vces) / R_relay (Vcc is the supply voltage, Vces is the transistor saturation CE voltage drop, which can be found in the transistor datasheet, and R_relay is the equivalent resistance of the relay coil, which can be found in the relay datasheet).
This allows us to calculate the RB resistor.
2. Selecting the Base-Emitter Resistor R. We've already covered the function of resistor R in a previous article, so I won't elaborate here. If you're interested, please check it out. A larger value for resistor R won't have much impact, but a smaller value can have an impact. How does this affect the transistor? We know that for a transistor to conduct, the applied voltage Vbe must be greater than the base-emitter turn-on voltage (typical for silicon transistors: 0.7V). Therefore, 5V*R/(R+RB)>Vbe_on (Vbe_on is the minimum base-emitter turn-on voltage of the transistor, which can be found in the transistor datasheet).
3. Transistor Selection Transistor selection primarily considers two factors: current and voltage. Current refers to the transistor's maximum collector-emitter current capability, which should be greater than the actual applied collector-emitter current. Voltage refers to the transistor's maximum collector-emitter withstand voltage, which should be greater than the supply voltage Vcc. These parameters can be found in the transistor datasheet.
4. Diode Selection
The current flowing through the relay coil generates a magnetic field. When the current is turned off, this magnetic field suddenly disappears. This sudden disappearance of the magnetic field induces a brief high voltage across the coil, which can potentially damage the transistor and IC. Therefore, a protection diode clamps the induced voltage, preventing it from becoming high enough to damage the transistor and IC.
When selecting a diode, therefore, focus on ensuring that its reverse voltage withstand is greater than the supply voltage Vcc and that its forward current is greater than the maximum collector current IC_max.