Why you cannot connect a motor to a GPIO pin
A GPIO pin on the ESP32 is rated for roughly 20 mA in practice, while even a small hobby motor draws 200 mA to over 1 A, and much more at start-up. Connecting it directly can damage the pin or reset the board. The pin should only carry the tiny control signal. The motor current has to flow through a separate power path that the pin switches on and off.
Parts you need
- An N-channel logic-level MOSFET, meaning one whose on-resistance is specified at a gate voltage of 3.3 V or lower. Small parts such as the AO3400 or IRLML6344 suit loads of a few amps. Always confirm Rds(on) at 3.3 V in the datasheet.
- A gate resistor of 100 to 220 Ω.
- A pull-down resistor of 10 kΩ from gate to ground.
- A flyback diode across the motor, such as a 1N5819 Schottky or a 1N4007.
- A motor supply that matches the motor voltage (for example 5 V or 12 V), separate from the ESP32 USB supply when the motor is larger.
Wiring
This is a low-side switch: the MOSFET sits between the motor and ground.
- Motor supply positive goes to one motor terminal.
- The other motor terminal goes to the MOSFET drain.
- The MOSFET source goes to ground.
- The ESP32 pin goes through the gate resistor to the gate. The 10 kΩ pull-down connects gate to ground.
- The flyback diode connects across the motor terminals with its cathode (the striped end) on the positive side.
- The ESP32 ground and the motor supply ground must be connected together. Without a common ground the gate signal has no reference and the circuit will not work.
Why the pull-down and the diode matter
During boot, an ESP32 pin can float for a moment. Without the pull-down, the gate may pick up noise and the motor could twitch or run at start-up. The diode protects the MOSFET: when the switch turns off, the motor coil tries to keep current flowing and produces a voltage spike far above the supply. The flyback diode gives that current a safe path to circulate and die out.
Example code with PWM
The sketch below uses the ESP32 Arduino core 3.x API and ramps the motor speed up and down. A frequency of 20 kHz keeps the switching sound out of the audible range.
const int MOTOR_PIN = 25; // avoid strapping pins 0, 2, 12, 15
const int PWM_FREQ = 20000; // 20 kHz
const int PWM_RES = 8; // 8 bit: duty 0..255
void setup() {
ledcAttach(MOTOR_PIN, PWM_FREQ, PWM_RES);
ledcWrite(MOTOR_PIN, 0); // motor off at start
}
void loop() {
for (int duty = 0; duty <= 255; duty += 5) {
ledcWrite(MOTOR_PIN, duty);
delay(40);
}
for (int duty = 255; duty >= 0; duty -= 5) {
ledcWrite(MOTOR_PIN, duty);
delay(40);
}
}
On the older core 2.x you use ledcSetup() and ledcAttachPin() instead of ledcAttach().
How do you test it safely?
First run the circuit with the motor disconnected and measure the drain voltage with a multimeter: it should read the supply voltage when the pin is low and drop close to zero when the pin is high. Then connect the motor at a low duty value and increase it gradually. After a minute under load, touch the MOSFET lightly. If it is too hot to hold, the gate voltage is probably too low for that part or the current is above its limit.
Why does the motor run weakly or the MOSFET get hot?
The most common cause is a MOSFET that is not truly logic-level, so 3.3 V does not turn it fully on and its resistance stays high. Choose a part with a low Rds(on) specified at 2.5 V or 3.3 V. The second cause is a missing common ground. The third is a gate resistor that is too large for a high PWM frequency, which slows the switching.
Summary
A logic-level MOSFET, a small gate resistor, a pull-down and a flyback diode are all you need to control a motor from an ESP32 safely. Share the grounds, check Rds(on) at 3.3 V, and start testing at low duty.
Comments