MOSFETs: switch amps with a 3.3 V pin
An ESP32 pin can give only approximately 20 mA at 3.3 V. A MOSFET is an electronic switch that a voltage controls. If you apply a voltage to its gate (G), current can flow from the drain (D) to the source (S). The gate uses almost no continuous current, so one GPIO pin can switch LED strips, solenoids, pumps and motors that use several amps at 12 V or more. The pin can also do this with PWM to dim LEDs and control the speed of motors.

Photo: Suyash Dwivedi, CC BY-SA 4.0
How a MOSFET works
Gate, drain, source
An N-channel MOSFET connects the drain to the source when the gate is several volts more than the source. The gate is insulated. It operates like a small capacitor. When you charge the gate, the switch closes. When you discharge the gate, the switch opens. After the gate is charged, it uses no current, so a small GPIO pin can control a large load.
N-channel and P-channel
An N-channel MOSFET is between the load and ground (a low-side switch). It turns on when the gate goes HIGH. It is the usual choice: it costs less, has a lower resistance, and is easy to control from 3.3 V. A P-channel MOSFET is between the supply and the load (a high-side switch). It turns on when its gate is pulled below the supply voltage.
Threshold ≠ fully on
VGS(th) is the gate voltage at which the MOSFET starts to conduct. The datasheet measures it at a very small test current (frequently 250 µA). Full conduction needs a higher gate voltage. Find the datasheet line RDS(on) at VGS = 4.5 V or 2.5 V. If the datasheet gives RDS(on) only at 10 V, a 3.3 V pin turns the MOSFET only half on. A half-on MOSFET becomes hot.
Which MOSFET to buy
| Part | Package | Max V / A | Operates from 3.3 V |
|---|---|---|---|
| IRLZ44N | TO-220 | 55 V / ~47 A | Yes The datasheet gives RDS(on) at 4–5 V. Satisfactory for a few amps. |
| IRLB8721 | TO-220 | 30 V / ~60 A | Yes Very low resistance. Very good for 12 V strips and motors. |
| IRL540N | TO-220 | 100 V / ~36 A | Yes For loads with a higher voltage. |
| AO3400A | SOT-23 (SMD) | 30 V / ~5 A | Yes The datasheet gives values at 2.5 V. It is very small, so heat limits it to approximately two amps. |
| 2N7000 | TO-92 | 60 V / 0.2 A | Marginal Only for small signals (relays, buzzers). |
| IRF520 / IRFZ44N | TO-220 | 100 V / 55 V | No Needs ~10 V on the gate. At 3.3 V, it turns on only a very small quantity. |
Build a low-side switch
- Gate resistor (~100 Ω)GPIO → 100 Ω → gate. The resistor limits the inrush current into the gate capacitance. It also decreases ringing.
- Pull-down resistor (10 kΩ)Gate → 10 kΩ → GND. When the ESP32 boots, its pins float. The pull-down resistor keeps the load OFF.
- Source to groundMOSFET source → load supply GND. Also connect that GND to the ESP32 GND.
- Load between + and drainSupply + → load → MOSFET drain.
- Flyback diode for coilsFor motors, solenoids, relays and pumps, connect a diode across the load. Connect the stripe (cathode) to + and the other end to the drain. A Schottky diode (1N5819, SS34) or a fast diode (UF4007) is best. A 1N4007 is satisfactory for slow on/off switching.
- Test with a current-limited supplyBefore you use PWM, make sure that the load turns fully on. Make sure that the MOSFET stays cool.
Wiring
| From | To | Notes |
|---|---|---|
| ESP32 GPIO 4 | 100 Ω → MOSFET gate | You can use all output pins. Do not use strapping pins. |
| Gate | 10 kΩ → GND | Keeps the load off during the boot. |
| +12 V supply | Load + | Also the cathode (stripe) of the diode. |
| Load − | MOSFET drain | Also the anode of the diode. |
| MOSFET source | Supply GND | Short, thick wire. |
| Supply GND | ESP32 GND | Common ground |
MOSFET heat calculator
A MOSFET that is fully on operates like a small resistor, RDS(on), so it changes P = I² × RDS(on) into heat. The temperature increase is P × θ (thermal resistance in °C/W). Without a heatsink, a TO-220 has θJA ≈ 62 °C/W, so approximately 1 W is a good limit.
High-side switch (P-MOSFET)
When you need a high-side switch
- The load must stay connected to ground (for example, some sensors or modules with their own GND connection).
- You must fully disconnect the power from a sub-circuit.
- You need reverse-polarity “ideal diode” protection on a battery input.
Why the ESP32 cannot control the P-MOSFET directly
To turn the P-MOSFET off, the gate voltage must be equal to the supply voltage (12 V). An ESP32 pin cannot do this. The NPN (or a small N-MOSFET) changes the 3.3 V signal into “pull the gate to 0 V”. Find the maximum VGS of the MOSFET (frequently ±20 V). If the supply voltage is more than that, add a Zener or a resistor divider.
Compare a MOSFET, a transistor, a relay and a driver chip
| Option | Good for | Be careful about |
|---|---|---|
| Logic-level MOSFET | DC loads from 100 mA to tens of amps, fast PWM, LED strips, motors in one direction | Needs a flyback diode for coils. Choose the part by its RDS(on) at your gate voltage. |
| BJT (2N2222, 2N3904) | Small loads (< ~200 mA): buzzers, small relays | Needs a base resistor. Decreases the voltage by ~0.2–0.7 V. Darlington transistors, for example the TIP120, decrease the voltage by 1–2 V and become hot. |
| Relay / relay module | To switch AC or isolated circuits with slow on/off switching | Slow, makes a click, wears, no PWM. The coil needs a transistor and a diode (modules have them). Mains voltage is dangerous. Work only with supervision. |
| Driver ICs (ULN2003, TB6612, DRV8833) | Several channels, H-bridges for motors in two directions, steppers | Each channel has current and voltage limits. Refer to DC motors and steppers. |
| Solid-state relay | To switch AC loads silently (heaters, lamps) | Most AC SSRs cannot switch DC. Some need more than 3.3 V on the input. Read the input specification. |
Code
On / off
mosfet_switch.ino
const int GATE_PIN = 4;
void setup() {
pinMode(GATE_PIN, OUTPUT);
digitalWrite(GATE_PIN, LOW); // the load is OFF at the start
}
void loop() {
digitalWrite(GATE_PIN, HIGH); // load ON
delay(2000);
digitalWrite(GATE_PIN, LOW); // load OFF
delay(2000);
}
PWM to control brightness / speed
mosfet_pwm.ino
const int GATE_PIN = 4;
const int FREQ = 5000; // 1–5 kHz for LEDs; ~20 kHz for quiet motors
const int BITS = 10; // 0..1023
void setup() {
ledcAttach(GATE_PIN, FREQ, BITS);
}
void loop() {
for (int d = 0; d <= 1023; d += 8) { ledcWrite(GATE_PIN, d); delay(10); }
for (int d = 1023; d >= 0; d -= 8) { ledcWrite(GATE_PIN, d); delay(10); }
}
At each switching edge, the GPIO must charge the gate capacitance through 100 Ω. The GPIO can supply only a limited current. For small TO-220 parts, frequencies up to ~20 kHz are satisfactory. For higher frequencies or large MOSFETs, use a gate-driver chip (for example, TC4427). This chip pushes amps into the gate and switches cleanly.
Troubleshooting
The load does not turn on
Make sure that there is a common ground. Make sure that the G/D/S pins are in the correct positions. Make sure that the MOSFET is a logic-level MOSFET. Measure the gate voltage with a meter. When the pin is HIGH, the meter must show 3.3 V.
The load is always on
Possible causes: the drain and source are in the wrong positions (the internal body diode conducts), the gate floats without a pull-down resistor, or the MOSFET is defective (a defective MOSFET is usually shorted). The usual cause of a defective MOSFET is a missing flyback diode.
The MOSFET becomes hot
Possible causes: the MOSFET is not fully on (the gate voltage is too low for that part), the current is too high for its RDS(on), or the PWM frequency is too high for the gate drive. Use the calculator above. Choose a part with a lower RDS(on), or add a heatsink.
Resources
- Electronics Tutorials: MOSFET as a switchOperating regions, threshold voltage and switching loads.
- SparkFun: TransistorsA basic explanation of BJTs and MOSFETs as switches.
- Falstad: NMOSFET simulationChange the gate voltage and look at the drain current.
- Adafruit: RGB LED strips with MOSFETsA standard LED strip driver with three MOSFETs.
- PWM & LEDs · DC motors · Power suppliesOther pages that use MOSFETs.