DC Motor Speed Control: PWM, L298N, TB6612 and MOSFET with Arduino and ESP32
In short: you control a DC motor’s speed by changing the average voltage it sees. The most efficient way is a PWM (pulse width modulation) signal. The Arduino or ESP32 generates the PWM; the motor is driven not from the pin but through a motor driver such as an L298N or TB6612FNG, or a MOSFET. Use an H-bridge if you need to reverse; a single MOSFET is enough for one direction.
This guide explains how PWM works, the common motor drivers and their differences, wiring, and example code for Arduino and ESP32.
In this article 11
- What is PWM and how does it change motor speed?
- Why can’t you connect a motor directly to an Arduino pin?
- Motor driver options
- Speed and direction with an L298N
- One-direction control with a MOSFET
- PWM on the ESP32
- Choosing the PWM frequency
- Current, heat and safety
- Brushless motors are different
- Frequently asked questions
- Related guides
What is PWM and how does it change motor speed?
PWM switches a signal on and off very quickly. The fraction of each period the signal stays on is the duty cycle.
- 100% duty: the motor sees full voltage and runs at full speed.
- 50% duty: the motor sees half the voltage on average and runs slower.
- 0% duty: the motor stops.
The motor’s mechanical inertia and winding inductance smooth out the switching, so it responds to the average voltage. PWM is far more efficient than dropping voltage with a resistor, because the driver is either fully on or fully off and wastes little heat.
On an Arduino Uno, analogWrite(pin, value) produces PWM with a value from 0 to 255; 128 is about 50% duty. The Uno’s PWM frequency is about 490 Hz on pins 3, 9, 10 and 11, and about 980 Hz on pins 5 and 6.
Why can’t you connect a motor directly to an Arduino pin?
An Arduino pin can supply only a few tens of milliamps. Even the smallest DC motor draws hundreds of milliamps running, and several times that at start-up or when stalled. The motor coil also produces a high reverse voltage spike when current is cut. So always drive the motor through a driver with a separate power supply, and let the Arduino produce only the control signal.
Motor driver options
| Driver | Type | Continuous current (per channel) | Motor voltage | Direction | Notes |
|---|---|---|---|---|---|
| Single MOSFET (e.g. IRLZ44N) | Switch | High, depending on cooling | Wide | No (one direction) | Simplest and most efficient; pumps, fans, one-way motors |
| L298N module | Bipolar H-bridge | ~2 A | ~5–35 V | Yes, 2 motors | Cheap and common; 2–3 V voltage drop and heat |
| TB6612FNG | MOSFET H-bridge | ~1.2 A (peak ~3 A) | 4.5–13.5 V | Yes, 2 motors | Small and efficient; ideal for small robots |
| DRV8833 | MOSFET H-bridge | ~1.5 A | 2.7–10.8 V | Yes, 2 motors | Small low-voltage motors |
| BTS7960 module | High-current H-bridge | Tens of amps | ~6–27 V | Yes, 1 motor | Large motors such as RS550 and RS775 |
Size the driver for the motor’s start-up and stall current, not its running current. Large brushed motors draw several times their running current at start. For RS-series current and voltage figures, see RS-series DC motors, RS775 and RS380.
Speed and direction with an L298N
The L298N module drives two motors independently. Each motor has three control pins:
- ENA (ENB): speed. Connect to an Arduino PWM pin. The ENA jumper on the module keeps it permanently on; remove the jumper for speed control.
- IN1 and IN2 (IN3, IN4): direction. IN1 = HIGH, IN2 = LOW turns one way; the reverse turns the other way. Both LOW lets the motor coast.
Wiring
| L298N | Connect to |
|---|---|
| 12V (VS) | Motor supply positive |
| GND | Supply negative and Arduino GND (common ground) |
| ENA | Arduino D9 (PWM) |
| IN1 / IN2 | Arduino D8 / D7 |
| OUT1 / OUT2 | Motor terminals |
If the motor supply is above 12 V, remove the module’s 5V regulator jumper and feed the module’s 5V pin from an external 5 V supply; the regulator can overheat at higher voltage.
Arduino code: speed from a potentiometer, direction from a button
const int ENA = 9; // PWM
const int IN1 = 8;
const int IN2 = 7;
const int POT = A0; // speed potentiometer
const int BTN = 2; // direction button (to GND)
bool forward = true;
bool lastState = HIGH;
void setup() {
pinMode(ENA, OUTPUT);
pinMode(IN1, OUTPUT);
pinMode(IN2, OUTPUT);
pinMode(BTN, INPUT_PULLUP);
}
void loop() {
bool state = digitalRead(BTN);
if (lastState == HIGH && state == LOW) { // button pressed
analogWrite(ENA, 0); // stop before reversing
delay(200);
forward = !forward;
}
lastState = state;
digitalWrite(IN1, forward ? HIGH : LOW);
digitalWrite(IN2, forward ? LOW : HIGH);
int speed = map(analogRead(POT), 0, 1023, 0, 255);
analogWrite(ENA, speed);
delay(10);
}Stopping the motor briefly before reversing reduces the shock load on the driver and gears.
One-direction control with a MOSFET
If the motor only needs to turn one way (fan, pump, feeder), the simplest and most efficient solution is a low-side N-channel MOSFET.
- Choose a logic-level MOSFET that fully turns on at logic voltage, such as the IRLZ44N. Standard MOSFETs may not fully turn on with 5 V on the gate.
- Connect the Arduino pin to the gate through a 100–220 Ω resistor.
- Put a 10 kΩ pull-down resistor between gate and GND so the motor does not start by itself while the Arduino boots.
- Fit a flyback diode across the motor, reverse-biased (for example a 1N5819 Schottky). It absorbs the voltage spike when the motor switches off.
- Motor negative goes to the MOSFET drain, source to GND; motor positive goes straight to the supply.
Because the ESP32 runs at 3.3 V, choose a MOSFET that fully turns on at 3.3 V or add a transistor gate driver.
PWM on the ESP32
On the ESP32, PWM comes from the LEDC peripheral, with adjustable frequency and resolution. A high frequency (for example 20 kHz) removes the audible motor whine.
// Arduino-ESP32 core 3.x
const int MOTOR_PWM = 25;
void setup() {
ledcAttach(MOTOR_PWM, 20000, 8); // pin, 20 kHz, 8-bit (0-255)
}
void loop() {
for (int d = 0; d <= 255; d += 5) { ledcWrite(MOTOR_PWM, d); delay(30); }
for (int d = 255; d >= 0; d -= 5) { ledcWrite(MOTOR_PWM, d); delay(30); }
}In the older 2.x Arduino-ESP32 core, use ledcSetup and ledcAttachPin instead. For ESP32 basics and pins, see what is ESP32.
Choosing the PWM frequency
- Low frequency (hundreds of Hz): the Arduino Uno default. Works, but the motor may whine audibly; low-speed torque can be better.
- High frequency (15–25 kHz): above human hearing, so the motor runs quietly. Suits MOSFET drivers (TB6612, BTS7960).
- L298N: its bipolar design loses efficiency at very high frequencies; it is comfortable up to a few kHz.
Current, heat and safety
- Separate supply: don’t power the motor from the Arduino’s 5V pin. Start-up current will pull the board’s voltage down and reset it.
- Common ground: the driver, power supply and Arduino grounds must be connected, or the control signal has no reference.
- Capacitors: a small ceramic capacitor (e.g. 100 nF) across the motor terminals and an electrolytic capacitor at the driver’s supply input reduce electrical noise.
- Cooling: bipolar drivers such as the L298N get hot. Add a heatsink as current rises, or switch to a MOSFET driver.
- Fuse: with large motors, a suitable fuse in the supply line protects wiring and battery from shorts and stalls.
To check that your battery can supply the current, use the battery pack calculator.
Brushless motors are different
The methods in this guide are for brushed DC motors. Brushless (BLDC) motors used in drones and fast RC models are controlled not by an H-bridge but by an ESC that drives three phases in sequence. See brushed vs brushless motors and what is an ESC.
Frequently asked questions
L298N or TB6612?
For small motors up to 12 V and around 1 A, the TB6612 is more efficient, smaller and runs cooler. The L298N works if you need higher voltage or already have one, but allow for its 2–3 V voltage drop.
Why doesn’t the motor turn at low PWM values?
A motor needs a minimum voltage to overcome friction before it starts turning, so below a certain duty cycle it only hums. Setting a minimum start value in code (for example 60/255) solves this.
Why does the Arduino reset when the motor starts?
Usually because the motor and Arduino share a supply and the motor pulls the voltage down at start-up. Power the motor separately, connect the grounds and add a capacitor on the supply line.
Can I measure motor speed?
Yes, with an encoder on the motor shaft or an infrared/Hall-sensor tachometer. Combined with PID control, the measured speed can keep the motor at a constant speed even under load. See the PID control guide.







