Here's an example solution that is based upon your schematic diagram. Perform these steps for each LED color—i.e., perform these steps for the red LED, then perform these steps again for the green LED, and again for the blue LED.
Design Choices
- KY-009 RGB Full color LED SMD Module
- \$I_{LED}=20\,\mathrm{mA}\$, ref1 ("KY-009 Specifications" table)
Datasheet Values
- Light Emitting Diode, D1
- \$V_{LED}(@I_{LED}=20\,\mathrm{mA}) = 2.0\,\mathrm{V}\$, ref2 (Fig. 1)
- 2N3904 NPN transistor, Q1
- \$V_{BE(sat)}(@I_{C(sat)}=20\,\mathrm{mA}) \approx 0.8\,\mathrm{V}\$, ref3 (Fig. 17)
- \$V_{CE(sat)}(@I_{C(sat)}=20\,\mathrm{mA}) \approx 0.11\,\mathrm{V}\$, ref3 (Fig. 17)
- Raspberry Pi GPIO pin
- Minimum voltage for a logic HIGH output signal, \$V_{OH}=3.0\,\mathrm{V}\$, ref4 ("Voltage specifications")
- Recommended maximum output current for a logic HIGH output signal, \$I_{OH(max)} = 16\,\mathrm{mA}\$, ref5
Solution
- Use Ohm's Law and Kirchhoff's Voltage Law (KVL) to calculate the resistance value for the LED's current limiting resistor Rx.
$$
\begin{align*}
Rx &= \frac{V_{Rx}}{I_{Rx}}\;\;\;\;\leftarrow\text{Ohm's Law}\\
&= \frac{V_{CC} - V_{LED} - V_{CE(sat)}}{I_{LED}}\;\;\;\;\leftarrow\text{KVL}\\
&= \frac{5\,\mathrm{V} - 2.0\,\mathrm{V} - 0.11\,\mathrm{V}}{20\,\mathrm{mA}}\\
&= 144.5\,\mathrm{\Omega}
\end{align*}
$$
Using a table of standard resistor values (example table), choose an available and appropriate resistance value for resistor Rx:
Calculate the power dissipation in the chosen resistance value Rx.
$$
P_{Rx} = I^2\cdot Rx = (20\,\mathrm{mA})^2 (143\,\Omega) = 57\,\mathrm{mW}
$$
Select an available and appropriate power rating for resistor Rx. For beginners I suggest the following rule: if a component's operational power dissipation is \$x\,\mathrm{Watts}\$, then choose (purchase) a component that can dissipate \$\ge 2x\,\mathrm{Watts}\$.
- \$2 \times P_{Rx} = 2 \times 57\,\mathrm{mW} = 114.4\,\mathrm{mW}\$
- When purchasing Rx, select a part whose power rating is 1/8 W (125 mW) or greater.
Transistor Q1 should operate in saturation mode, not forward-active mode. Select \$\beta _{sat} = 10\$, ref3, Fig. 17
Calculate transistor Q1's base saturation current \$I_{B(sat)}\$ when the transistor is operating in saturation mode and \$I_{C(sat)} = 20\,\mathrm{mA}\$.
$$
I_{B(sat)} = \frac{I_{C(sat)}}{\beta _{sat}}
= \frac{20\,\mathrm{mA}}{10}
= 2\,\mathrm{mA}
$$
- :: CHECK :: Ensure \$I_{B(sat)}\$ does not exceed the GPIO pin's maximum output current spec.
$$
2\,\mathrm{mA} \le 16\,\mathrm{mA} = I_{OH(max)}
$$
Repeat steps 1 – 4 to choose available and appropriate values for resistor R1's resistance value and power rating.
$$
\begin{align*}
R1 &= \frac{V_{R1}}{I_{R1}}\\
&= \frac{V_{OH} - V_{BE(sat)}}{I_{B(sat)}}\\
&= \frac{3.0\,\mathrm{V} - 0.8\,\mathrm{V}}{2\,\mathrm{mA}}\\
&= 1100\,\mathrm{\Omega}
\end{align*}
$$
Chosen value for R1: 1.1 kΩ, 1 % tolerance
Calculated power dissipation in R1: 4.4 mW
Chosen power rating for R1 (>= 2x calculated power dissipation): 1/8 W