How-to · Intermediate · 4 min read
Transistors and MOSFETs as Switches
BC547, 2N2222, IRLZ44N
By Geekobot Learning Team · Updated 24 Oct 2026
On this page
A transistor lets a small microcontroller signal switch a much larger current. For loads up to a few hundred milliamps, an NPN transistor such as the 2N2222 or BC547 with a base resistor works well. For amps, use a logic-level N-channel MOSFET such as the IRLZ44N, which turns fully on from a 5 V pin. Add a flyback diode across motors and relay coils.
Key specs at a glance
- BC547 (NPN)
- 45 V, 100 mA
- 2N2222 / PN2222 (NPN)
- 40 V, 600 mA
- TIP120 (Darlington)
- 60 V, 5 A, about 1 to 2 V drop
- IRLZ44N (logic-level MOSFET)
- 55 V, Rds(on) about 0.025 Ω at 5 V gate
- IRF540N
- 100 V, needs about 10 V on the gate to turn fully on
- Flyback diode
- 1N4007 (slow loads), 1N5819 or 1N4148 (fast PWM)
Why you need a transistor
A microcontroller pin supplies about 8 to 20 mA at 3.3 or 5 V. A motor, a relay coil, a solenoid or an LED strip needs tens of milliamps to several amps, often at 12 V. A transistor placed on the low side (between the load and ground) acts as a switch: the pin controls it, and the separate supply powers the load.
Switching with an NPN transistor (and calculating the base resistor)
Wire the load from +V to the collector, the emitter to ground, and the base to the microcontroller pin through a resistor. To make sure the transistor is fully on (saturated), use a forced gain of about 10:
Ib = load current / 10
Rb = (pin voltage - 0.7 V) / Ib
a relay coil drawing 80 mA from a 5 V Arduino pin: Ib = 8 mA, Rb = (5 - 0.7) / 0.008 = 537 Ω, so use 470 or 560 Ω. From a 3.3 V ESP32 pin: (3.3 - 0.7) / 0.008 = 325 Ω, so use 330 Ω.
Switching with a MOSFET
A MOSFET is controlled by gate voltage, not current, so it draws almost nothing from the pin once switched. Wire the load from +V to the drain, the source to ground, the gate to the pin through a 100 to 220 Ω resistor, and a 10 kΩ resistor from gate to ground so the load stays off while the microcontroller boots.
The key spec is Rds(on) at your gate voltage. A "logic-level" MOSFET specifies low Rds(on) at 4.5 V or less. The IRLZ44N is excellent from 5 V. From 3.3 V boards, choose parts specified at 2.5 V such as the AO3400 (SOT-23), or drive the gate of a larger MOSFET with a small transistor or gate driver.
| Part | Type | Good from 5 V pin? | Good from 3.3 V pin? | Use |
|---|---|---|---|---|
| BC547 | NPN BJT | Yes (to 100 mA) | Yes (to 100 mA) | LEDs, small buzzers |
| 2N2222 | NPN BJT | Yes (to about 500 mA) | Yes | Relays, small motors |
| IRLZ44N | Logic-level MOSFET | Yes, to many amps | Partly; limit to about 1 to 2 A | LED strips, 12 V motors, solenoids |
| IRF540N / IRFZ44N | Standard MOSFET | No, needs 10 V gate | No | Use with a gate driver |
| AO3400 | Logic-level MOSFET (SMD) | Yes | Yes, to about 2 A | Compact 3.3 V designs |
| ULN2003 | 7-channel Darlington array | Yes, 500 mA per channel | Yes | Relays, unipolar steppers |
- BC547
- Type:
- NPN BJT
- Good from 5 V pin?:
- Yes (to 100 mA)
- Good from 3.3 V pin?:
- Yes (to 100 mA)
- Use:
- LEDs, small buzzers
- 2N2222
- Type:
- NPN BJT
- Good from 5 V pin?:
- Yes (to about 500 mA)
- Good from 3.3 V pin?:
- Yes
- Use:
- Relays, small motors
- IRLZ44N
- Type:
- Logic-level MOSFET
- Good from 5 V pin?:
- Yes, to many amps
- Good from 3.3 V pin?:
- Partly; limit to about 1 to 2 A
- Use:
- LED strips, 12 V motors, solenoids
- IRF540N / IRFZ44N
- Type:
- Standard MOSFET
- Good from 5 V pin?:
- No, needs 10 V gate
- Good from 3.3 V pin?:
- No
- Use:
- Use with a gate driver
- AO3400
- Type:
- Logic-level MOSFET (SMD)
- Good from 5 V pin?:
- Yes
- Good from 3.3 V pin?:
- Yes, to about 2 A
- Use:
- Compact 3.3 V designs
- ULN2003
- Type:
- 7-channel Darlington array
- Good from 5 V pin?:
- Yes, 500 mA per channel
- Good from 3.3 V pin?:
- Yes
- Use:
- Relays, unipolar steppers
Always add a flyback diode for coils and motors
When current through a coil (relay, solenoid, motor) is switched off, the coil generates a voltage spike that can destroy the transistor. Place a diode across the load, cathode (striped end) to +V, anode to the transistor side. Use a 1N4007 for relays and solenoids, and a Schottky such as the 1N5819 for PWM-driven motors. Relay modules and ULN2003 chips include these diodes.
PWM a 12 V LED strip or motor with a MOSFET
const int GATE = 9; // PWM pin to MOSFET gate via 220 ohm
void setup() { pinMode(GATE, OUTPUT); }
void loop() {
for (int d = 0; d <= 255; d += 5) { analogWrite(GATE, d); delay(30); }
for (int d = 255; d >= 0; d -= 5) { analogWrite(GATE, d); delay(30); }
}Try it: Transistor base resistor calculator
Change the values and the diagram and results update as you type.
Common mistakes to avoid
- Leaving out the base or gate resistor.
- Using an IRF540N or IRFZ44N straight from a 5 V or 3.3 V pin; it only half turns on and overheats.
- Forgetting the flyback diode on relays, solenoids and motors.
- Not connecting the load supply's ground to the microcontroller ground.
- Putting an NPN or N-channel switch on the high side, where it cannot turn fully on.
Frequently asked questions
Should I use a transistor or a MOSFET?
Use an NPN transistor for loads up to a few hundred milliamps. Use a logic-level MOSFET for higher currents and PWM, since it wastes far less power as heat.
What is a logic-level MOSFET?
A MOSFET whose datasheet specifies low on-resistance at a gate voltage of 4.5 V or less, so a microcontroller pin can switch it fully on. Look for Rds(on) quoted at Vgs = 4.5 V or 2.5 V.
Can an ESP32 drive an IRLZ44N?
It will switch on, but at 3.3 V the resistance is higher than at 5 V, so keep currents modest, or use a MOSFET specified at 2.5 V, or a small gate-driver stage.
What base resistor should I use for a 2N2222?
Rb = (pin voltage - 0.7) / (load current / 10). For 100 mA from a 5 V pin, about 430 Ω; 1 kΩ is a common safe value for loads under 40 mA.
Why does my transistor get hot?
It is not fully saturated (base resistor too large, or gate voltage too low for the MOSFET), or the load exceeds its rating. Recalculate the base resistor or choose a logic-level MOSFET.
Parts for this guide
Calculators
Terms in this guide
Projects that use it
Put this into practice: 8 step-by-step builds in our project library use parts from this guide.
Keep learning
Sources
- P2N2222A datasheet (onsemi)
- BC547 datasheet (onsemi)
- IRLZ44N datasheet (Infineon)
- IRF540N datasheet (Infineon)
- AO3400A datasheet (Alpha & Omega Semiconductor)
- ULN2003A datasheet (Texas Instruments)
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.


