Sub topic 9
Switching a DC Motor with a Logic-Level MOSFET
This chapter assumes you have read the earlier chapters in this tutorial. You should know why a pin cannot power a motor, how to share a ground between an external supply and the Arduino, how analogWrite produces PWM, and why a flyback diode protects against inductive kickback. Here you put those pieces together into a working one-direction motor driver with speed control.
What a MOSFET does in this circuit
A MOSFET (metal-oxide-semiconductor field-effect transistor) is a voltage-controlled switch. It has three terminals:
- Gate: the control input. It draws almost no steady current.
- Drain: where the load current enters.
- Source: where the load current leaves, usually tied to ground.
When the gate voltage (measured relative to the source) is high enough, the drain-source path conducts. When it is near zero, the path is open. The Arduino pin only has to charge and discharge the gate, so the pin's current limit stops mattering. The motor current flows from the external supply, through the motor, through the MOSFET, and back to the supply's ground.
This arrangement is called a low-side switch, because the MOSFET sits between the load and ground. The motor's positive side stays connected to the supply at all times. Only the ground path is switched.
Why "logic-level" matters
MOSFETs are not all equal at low gate voltages. Every datasheet lists a gate threshold voltage (V_GS(th)), and beginners often misread it. The threshold is the voltage at which the MOSFET just barely starts to conduct, typically at a tiny test current such as 250 µA. It does not mean the MOSFET is fully on.
What you need is a low on-resistance, R_DS(on), at the gate voltage you can supply. A standard power MOSFET such as the IRF540 specifies R_DS(on) at a gate voltage of 10 V. At 5 V (or 3.3 V on many boards) it may conduct poorly, behave like a resistor, and get hot.
A logic-level MOSFET guarantees a low R_DS(on) at 4.5 V or even 2.5 V. Check these items in the datasheet:
R_DS(on)is listed atV_GS= 4.5 V (for a 5 V board) or 2.5 V (for a 3.3 V board).- The maximum drain-source voltage
V_DSis comfortably above your motor supply voltage. - The continuous drain current rating is well above your motor's stall current.
Common hobby choices include the IRLZ44N and the FQP30N06L. For very small motors, a 2N7000-class part is too weak, so check the current rating before you substitute.
| Part type | Gate voltage for full turn-on | Works from a 5 V pin? |
|---|---|---|
| Standard power MOSFET (e.g. IRF540) | About 10 V | Poorly. It may run hot or fail to switch fully. |
| Logic-level MOSFET (e.g. IRLZ44N) | About 4.5 V or less | Yes |
| Sub-logic-level MOSFET | About 2.5 V | Yes, including on 3.3 V boards |
The complete circuit
Wire the circuit as follows. Each component has a job.
- Arduino pin 5 to a 100–220 Ω resistor to the gate. The gate behaves like a small capacitor. Without a resistor, the pin sees a brief current spike every time the gate charges. The resistor limits that spike and protects the pin.
- 10 kΩ resistor from gate to ground. This pull-down keeps the gate low while the Arduino is booting or the pin is floating. Without it, the gate can drift high and the motor may twitch or run at power-up.
- Motor between the external supply (+) and the MOSFET drain.
- MOSFET source to ground.
- Flyback diode across the motor terminals. The cathode (the striped end) goes toward the supply positive. When the MOSFET turns off, the motor's inductance forces current to keep flowing. The diode gives that current a loop to circulate in, so the drain voltage does not spike past the MOSFET's rating.
- External supply ground connected to Arduino GND. This common ground gives the gate signal a reference. Without it, the gate voltage has no meaning to the MOSFET.
Pin 5 is a PWM-capable pin on an Uno. Check your board's pinout if you use a different model.
PWM speed control
Switching the motor fully on or off gives you two speeds. analogWrite produces PWM, a fast on-off pattern whose duty cycle sets the average power. At a 50% duty cycle, the motor sees the full supply voltage half of the time. Because the motor has inertia and inductance, it responds to the average and runs at roughly half speed.
The Uno's pins 5 and 6 run PWM at about 980 Hz, and most other PWM pins run at about 490 Hz. The motor's inductance smooths the current at those rates, though you may hear a faint whine.
This sketch ramps the motor up and then down:
// Logic-level N-channel MOSFET as a low-side switch.
// Arduino pin 5 -> 100-220 ohm resistor -> MOSFET gate (10k pull-down from gate to GND).
// Motor between the external supply (+) and MOSFET drain; source to GND.
// Flyback diode across the motor, cathode toward the supply (+).
// External supply ground is connected to Arduino GND.
const int MOTOR_PIN = 5;
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
}
void loop() {
for (int speed = 0; speed <= 255; speed += 5) {
analogWrite(MOTOR_PIN, speed);
delay(40);
}
for (int speed = 255; speed >= 0; speed -= 5) {
analogWrite(MOTOR_PIN, speed);
delay(40);
}
}The ramp takes about two seconds in each direction: 52 steps of 40 ms each.
Minimum duty cycle and the dead zone
A motor does not turn at very low duty cycles. Friction must be overcome first, and the average voltage at a duty of 20 or 30 out of 255 may be too low to start the shaft. You will see the motor hum without turning, then start suddenly at some threshold value. That threshold varies with the motor, its load, and the supply voltage.
Measure it for your setup. Then map your control range to start just above it:
const int MOTOR_PIN = 5;
const int MIN_RUN = 60; // measured: lowest duty that reliably starts the motor
// percent: 0 to 100
void setSpeedPercent(int percent) {
percent = constrain(percent, 0, 100);
if (percent == 0) {
analogWrite(MOTOR_PIN, 0);
} else {
analogWrite(MOTOR_PIN, map(percent, 1, 100, MIN_RUN, 255));
}
}
void setup() {
pinMode(MOTOR_PIN, OUTPUT);
}
void loop() {
setSpeedPercent(30);
delay(2000);
setSpeedPercent(0);
delay(1000);
}A motor that is already spinning can keep running at a lower duty than it needs to start. Some projects apply a short burst at full power to start the shaft and then drop to the target value.
Edge cases and common mistakes
The MOSFET gets hot
Heat comes from P = I² × R_DS(on). A MOSFET that is not fully on has a higher resistance than the datasheet number, so the usual cause is a gate voltage that is too low. Check that the part is truly logic-level. Also check the duty cycle. Switching losses add to conduction losses at higher PWM frequencies, but at 490–980 Hz they are small.
At a stall current of 2 A and an R_DS(on) of 0.05 Ω, the power loss is 2² × 0.05 = 0.2 W. That is warm but fine. At an effective 1 Ω because of a weak gate drive, it is 4 W, enough to burn a finger and destroy a TO-220 part without a heat sink.
The motor runs at full speed and ignores PWM
This usually means the drain and source are swapped or the wrong pin is used, or the MOSFET has failed short. A MOSFET that has seen an unclamped voltage spike often fails with drain and source shorted together. Test with the power off: a healthy MOSFET reads no continuity between drain and source when the gate is at 0 V.
Be careful with the pinout. Many TO-220 MOSFETs list the pins as gate, drain, source from left to right when you face the printed side, but verify with your datasheet.
The motor never stops
A missing gate pull-down, or a gate that is left floating, can leave the MOSFET partly on. Add the 10 kΩ resistor. A hand near the unconnected gate can turn the motor on.
Arduino resets when the motor starts
A motor that starts draws a surge of current, and a shared supply sags. The Arduino browns out and restarts. Keep the motor on a separate supply from the board, or add decoupling capacitance. A later chapter covers this in detail.
Wrong flyback diode orientation
A reversed diode is a short circuit across the supply while the motor is on. It can overheat within seconds. The cathode (striped end) points toward the positive supply. Use a diode rated for at least the motor's stall current, such as a 1N5819 Schottky for small motors. A 1N4001 also works for low currents.
What this circuit cannot do
A single low-side MOSFET drives the motor in one direction only. To reverse it, you need to swap the polarity across the motor, which takes four switches arranged as an H-bridge. The next chapter covers that.
Braking is also limited. With one MOSFET, switching off lets the motor coast down, with the flyback diode absorbing the energy. You cannot actively brake it.
Exercise
A student uses an IRF540 MOSFET (RDS(on) is specified at VGS = 10 V) to switch a 12 V motor from an Arduino Uno pin through a 150 Ω gate resistor. The motor runs weakly and the MOSFET gets hot. What is the likely cause, and what are two changes that would fix it?
Show answer
A 5 V pin does not drive the IRF540 gate high enough to turn it fully on, so its on-resistance is far above the datasheet value and it dissipates heat. Fix it by replacing the part with a logic-level MOSFET such as an IRLZ44N, or by adding a gate driver stage that supplies about 10 V to the gate. Check that the pull-down and common ground are present as well.
Exercise
Your motor hums at analogWrite(MOTOR_PIN, 40) but only starts turning at about 75. How would you change the sketch so a 1% to 100% speed setting always produces a turning motor?
Show answer
Treat 75 as the minimum running duty and map the 1-100 percent range onto 75-255, for example analogWrite(MOTORPIN, map(percent, 1, 100, 75, 255)), while keeping 0 percent as analogWrite(MOTORPIN, 0).