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.

A MOSFET in a TO-220 package with three legs: gate, drain and source
A MOSFET in a TO-220 package. Legs from left: gate, drain, source. The logic-level IRLZ44N looks the same.
Photo: Suyash Dwivedi, CC BY-SA 4.0

Low-side switch simulator

The simulator shows PWM slowly (1 cycle per second)
+12 V supply Motor inductive flyback diode D S N-MOSFET logic-level 100 Ω G 10 kΩ ESP32 GPIO 4 GND LOW 12 V GND common ground (ESP32 GND ↔ 12 V supply GND) ⚡ spike

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

PartPackageMax V / AOperates from 3.3 V
IRLZ44NTO-22055 V / ~47 AYes The datasheet gives RDS(on) at 4–5 V. Satisfactory for a few amps.
IRLB8721TO-22030 V / ~60 AYes Very low resistance. Very good for 12 V strips and motors.
IRL540NTO-220100 V / ~36 AYes For loads with a higher voltage.
AO3400ASOT-23 (SMD)30 V / ~5 AYes The datasheet gives values at 2.5 V. It is very small, so heat limits it to approximately two amps.
2N7000TO-9260 V / 0.2 AMarginal Only for small signals (relays, buzzers).
IRF520 / IRFZ44NTO-220100 V / 55 VNo Needs ~10 V on the gate. At 3.3 V, it turns on only a very small quantity.
About the blue “IRF520 MOSFET driver module”: Do not use this module with an ESP32. Sellers sell it for Arduino, but the IRF520 needs approximately 10 V on its gate to turn fully on. With the 3.3 V of an ESP32, it frequently stays partly on. It then decreases the load voltage by several volts and becomes too hot at more than a few hundred mA. Choose a module with a logic-level MOSFET (for example, IRLZ44N, AOD4184), or a module with a gate driver.
IRLZ44N text facing you G D S TO-220: G · D · S (tab = D) A09T 1 G 2 S 3 D SOT-23 (AO3400A)
Common pinouts. Always compare with the datasheet. Some parts in the same package use a different pin order.

Build a low-side switch

  1. Gate resistor (~100 Ω)GPIO → 100 Ω → gate. The resistor limits the inrush current into the gate capacitance. It also decreases ringing.
  2. Pull-down resistor (10 kΩ)Gate → 10 kΩ → GND. When the ESP32 boots, its pins float. The pull-down resistor keeps the load OFF.
  3. Source to groundMOSFET source → load supply GND. Also connect that GND to the ESP32 GND.
  4. Load between + and drainSupply + → load → MOSFET drain.
  5. 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.
  6. 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

FromToNotes
ESP32 GPIO 4100 Ω → MOSFET gateYou can use all output pins. Do not use strapping pins.
Gate10 kΩ → GNDKeeps the load off during the boot.
+12 V supplyLoad +Also the cathode (stripe) of the diode.
Load −MOSFET drainAlso the anode of the diode.
MOSFET sourceSupply GNDShort, thick wire.
Supply GNDESP32 GNDCommon ground
The function of a flyback diode: The current in a coil cannot stop immediately. When the MOSFET turns off, the coil increases its voltage until the current finds a path. This voltage is frequently tens of volts or more. If there is no diode, that spike goes to the drain of the MOSFET. The spike can destroy the MOSFET or reset the ESP32. The diode gives the current a safe loop in which it decreases. Try this in the simulator above.

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)

+12 V 10 kΩ S D P-MOSFET Load GND NPN 2N3904 1 kΩ GPIO
GPIO HIGH → NPN on → gate pulled to 0 V → P-MOSFET on → load energized. GPIO LOW → 10 kΩ pulls the gate up to +12 V → off. The logic is the same as the GPIO (HIGH = on). The ESP32 never gets 12 V.

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

OptionGood forBe careful about
Logic-level MOSFETDC loads from 100 mA to tens of amps, fast PWM, LED strips, motors in one directionNeeds 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 relaysNeeds 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 moduleTo switch AC or isolated circuits with slow on/off switchingSlow, 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, steppersEach channel has current and voltage limits. Refer to DC motors and steppers.
Solid-state relayTo 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