A GPIO pin on an Arduino, ESP32 or Raspberry Pi Pico can only source a few milliamps. That's enough to light a single LED, but not to run a motor, a relay, or a strip of LEDs. The fix is almost always the same part: a transistor, used as an electronic switch.
What a transistor does
A transistor has three pins. A small signal on one pin controls a much larger current flowing between the other two. Your microcontroller provides the small signal; the transistor does the heavy lifting.
The two types you'll use most
BJT (bipolar junction transistor)
Pins: base, collector, emitter. A small current into the base lets a larger current flow from collector to emitter. NPN types such as the 2N2222, 2N3904 and BC547 are cheap, common and perfect for loads up to a few hundred milliamps: small relays, buzzers, short LED strips.
MOSFET
Pins: gate, drain, source. A voltage on the gate (almost no current) lets current flow from drain to source. MOSFETs waste less power as heat, which makes them the better choice for motors, heaters and anything drawing an amp or more.
Wiring a transistor as a low-side switch
The simplest and most common setup puts the transistor between the load and ground:
- Load positive goes to your supply (for example 5 V or 12 V).
- Load negative goes to the collector (BJT) or drain (MOSFET).
- Emitter or source goes to ground, shared with the microcontroller's ground.
- The GPIO pin drives the base or gate through a resistor.
Three details that save you trouble
1. Always use a base resistor on a BJT
Without one, the base draws too much current and can damage the transistor or your microcontroller pin. A quick rule of thumb for switching: aim for a base current of about one tenth of the load current. For a 100 mA load driven from a 3.3 V pin, that's about 10 mA: (3.3 V − 0.7 V) ÷ 0.01 A ≈ 260 Ω, so a standard 220 Ω resistor works well.
2. Choose a logic-level MOSFET
Many MOSFETs only turn fully on with 10 V on the gate, which a 3.3 V or 5 V pin can't provide. Look for "logic-level" parts and check the datasheet's RDS(on) rating at your gate voltage. The IRLZ44N, for example, is designed to be driven from 5 V logic. Add a 10 kΩ pull-down resistor from gate to ground so the MOSFET stays off while your board boots, and a small series resistor (around 100 Ω) on the gate.
3. Add a flyback diode for anything with a coil
Relays, solenoids and motors produce a voltage spike when switched off. Place a diode such as a 1N4007 across the load, with the stripe (cathode) toward the positive supply. It safely absorbs the spike and protects your transistor.
Which one should you pick?
- Under about 200 mA, like a small relay or buzzer: an NPN BJT such as the 2N2222 is simple and inexpensive.
- Motors, LED strips or higher currents: use a logic-level N-channel MOSFET.
- Not sure? Check the load's current draw, then compare it with the transistor's maximum current and power ratings in its datasheet, leaving plenty of margin.
Transistors are one of the most useful parts in any maker's kit. Once you've wired one, you can control almost anything from a single pin.