ESP32: switch a high-current load with a 2N2222 transistor
Use a 2N2222 NPN transistor to switch a load that draws too much current for a GPIO pin. Pumps, fans, LED strips, solenoids.
The ESP32’s GPIO pins can deliver about 40 mA each, 200 mA total across all pins. That is enough for LEDs and small signals, but not enough for pumps, fans, solenoids, or LED strips that draw amps. The 2N2222 NPN transistor lets the small GPIO current switch a much larger load current.
This tutorial covers the wiring, the math for choosing the base resistor, and the pattern for switching 12V loads from a 3.3V GPIO.
What you need
- ESP32 dev board
- 2N2222 NPN transistor (TO-92 package; about $0.10)
- 1k ohm resistor (for the base)
- Load: a 12V LED strip, 12V fan, or 12V pump (anything drawing under 800 mA)
- 12V power supply for the load (the ESP32 cannot supply this)
- Jumper wires
Wiring
ESP32 GPIO 4 --[ 1k resistor ]-- 2N2222 base (pin 1)
ESP32 GND -- 2N2222 emitter (pin 3)
ESP32 GND -- 12V supply GND (common ground)
12V supply + -- Load +
Load - -- 2N2222 collector (pin 2)
When the GPIO goes HIGH, current flows through the 1k resistor into the base, which turns on the transistor. Current flows from collector to emitter, which completes the circuit through the load. The load turns on.
When the GPIO goes LOW, the transistor turns off, and the load turns off.
The 2N2222’s pinout (looking at the flat side): E B C (emitter, base, collector). The pin closest to the tab is the emitter. Pinout varies by manufacturer; check the datasheet for your specific part.
Why a transistor and not a relay
For DC loads (LEDs, fans, pumps), a transistor is better than a relay:
- No moving parts. No clicks, no wear, no bounce.
- Fast switching. PWM-friendly. You can dim an LED strip at 1 kHz.
- Smaller. A 2N2222 is smaller than a relay module.
- Cheaper. 10 cents vs a few dollars for the relay.
- Quieter. No mechanical click.
The downsides:
- Transistor gets hot. With a 12V load at 500 mA, the 2N2222 dissipates 6W as heat. That needs a heatsink.
- No isolation. A short in the load can damage the ESP32.
- DC only. Relays work for AC; transistors are DC-only.
- Current limit. The 2N2222 is rated for 800 mA. For higher current, use a MOSFET.
For low-current DC loads (LEDs, small fans), the 2N2222 is the right pick. For high current or AC, use a relay.
The base resistor math
The transistor needs about 1/10th of the load current at the base to fully turn on. For a 500 mA load, the base needs 50 mA. The ESP32’s GPIO delivers about 3.3V at 40 mA max, so we cannot supply 50 mA to the base. In practice, a smaller base current (10-20 mA) works for most loads because the transistor is well into saturation.
Calculate the base resistor:
R_base = (V_gpio - V_be) / I_base
R_base = (3.3V - 0.7V) / 0.01A = 260 ohms
Round up to 1k ohm (we want less base current, not more). 1k gives about 2.6 mA to the base, which is enough to switch 100-300 mA of load current with some saturation loss.
For higher load current (up to 800 mA), use a smaller base resistor (470 ohm or 220 ohm) to ensure full saturation:
R_base = (3.3V - 0.7V) / 0.02A = 130 ohms
Use 220 ohm for safety margin.
The code
ESP32 (Arduino)
const int LOAD_PIN = 4;
void setup() {
pinMode(LOAD_PIN, OUTPUT);
}
void loop() {
digitalWrite(LOAD_PIN, HIGH); // load on
delay(3000);
digitalWrite(LOAD_PIN, LOW); // load off
delay(3000);
}
Arduino (Uno, Nano, Mega)
const int LOAD_PIN = 4;
void setup() {
pinMode(LOAD_PIN, OUTPUT);
}
void loop() {
digitalWrite(LOAD_PIN, HIGH);
delay(3000);
digitalWrite(LOAD_PIN, LOW);
delay(3000);
}
Same code. The Uno’s 5V GPIO delivers more base current than the ESP32’s 3.3V, so the same base resistor value works for both.
PWM dimming an LED strip
The transistor is fast enough for PWM. Use analogWrite() (or LEDC on
the ESP32) to dim the LED strip:
const int LOAD_PIN = 4;
void setup() {
pinMode(LOAD_PIN, OUTPUT);
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness += 5) {
analogWrite(LOAD_PIN, brightness);
delay(50);
}
for (int brightness = 255; brightness >= 0; brightness -= 5) {
analogWrite(LOAD_PIN, brightness);
delay(50);
}
}
This dims the LED strip from 0 to 100% and back. The 2N2222 handles PWM at 1 kHz fine.
For PWM at higher frequencies (above 10 kHz), use a MOSFET instead of a 2N2222. The 2N2222’s switching speed is limited.
The flyback diode (for inductive loads)
If your load is inductive (a motor, a solenoid, a relay coil), the transistor needs a flyback diode across the load. The diode absorbs the back-EMF when the transistor switches off; without it, the back-EMF will damage the transistor.
Load + -- diode cathode (stripe)
Load - -- diode anode (no stripe)
Use a 1N4007 (for low-speed switching under 1 kHz) or a fast-recovery diode (for higher speeds). The 1N4007 is the standard pick.
When to use a MOSFET
The 2N2222 is rated for 800 mA and 40V. For higher current or voltage, use a MOSFET. The IRF520 is the standard pick for hobby projects: it handles 100V and 10A, and it has logic-level drive (works from 3.3V GPIO).
Wiring a MOSFET is the same as the 2N2222, but no base resistor is needed:
ESP32 GPIO 4 -- MOSFET gate
ESP32 GND -- MOSFET source
12V supply + -- Load +
Load - -- MOSFET drain
The IRF520’s gate has a high capacitance that can cause voltage spikes. Add a 100 ohm resistor between the GPIO and the gate, and a 10k pull-down from gate to source to keep the MOSFET off when the GPIO is not initialized.
What you learned
- The 2N2222 is the standard NPN transistor for switching small DC loads.
- A 1k base resistor is enough for 100-300 mA loads.
- Use a flyback diode for inductive loads (motors, solenoids).
- Use a MOSFET for higher current or PWM at higher frequencies.
When something breaks
- Load never turns on. Base resistor is wrong, or transistor is in backwards (swap emitter and collector).
- Load is always on. Transistor is shorted, or GPIO is stuck HIGH.
- Transistor gets very hot. Load is drawing too much current. Add a heatsink or use a MOSFET.
- ESP32 resets when load turns off. Back-EMF from an inductive load. Add a flyback diode.
What to build next
- The relay tutorial covers the alternative for AC or high-current loads.
- The servo tutorial covers the alternative for precise position control.
- The book ESP32 Robotics Projects covers H-bridge motor drivers for bidirectional motor control.