Sub topic 9
Switching a DC Motor with a Logic-Level MOSFET
Assume you can upload an Arduino sketch, use pinMode() and analogWrite(), and understand voltage, current, and ground. This chapter shows how to control the speed of a small direct-current (DC) motor without powering it from an Arduino pin.
A MOSFET is an electronic switch. The Arduino controls its gate, while the motor current flows through the MOSFET from an external power supply. This keeps the motor’s current away from the Arduino I/O pin.
The low-side switch circuit
Use a logic-level, N-channel MOSFET. “Logic-level” means that the MOSFET turns on well when its gate is driven by the Arduino’s logic voltage, such as 5 V or 3.3 V. Check the datasheet because not every N-channel MOSFET is logic-level.
Connect the circuit as follows:
- Connect the motor’s positive wire to the positive terminal of an external motor supply.
- Connect the motor’s negative wire to the MOSFET drain.
- Connect the MOSFET source to the supply ground.
- Connect the Arduino GND to the same supply ground. This is the common ground.
- Connect an Arduino PWM pin to the MOSFET gate through a 100–220 ohm resistor.
- Add a 10-kilohm resistor from the gate to ground. This pull-down resistor keeps the MOSFET off while the Arduino is starting.
- Place a flyback diode across the motor. Its cathode, marked with a stripe, connects to the motor’s positive side. Its anode connects to the motor’s negative side.
The motor supply must provide the motor’s rated voltage. Do not assume that the Arduino 5 V pin can supply enough current.
Speed control with PWM
Pulse-width modulation (PWM) switches the MOSFET on and off quickly. analogWrite() sets the duty cycle, which is the percentage of each cycle that the signal is on. On many Arduino boards, a value of 0 means always off and 255 means always on.
// Logic-level N-channel MOSFET as a low-side switch.
// Arduino pin 5 -> 100-220 ohm resistor -> MOSFET gate.
// Add a 10k resistor from gate to GND.
// Motor: external supply positive -> motor -> MOSFET drain.
// MOSFET source -> supply GND.
// Connect supply GND to Arduino GND.
// Flyback diode: cathode to supply positive, anode to MOSFET drain.
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 motor may not move at very low duty cycles. It needs enough torque to overcome friction and its starting load. Once moving, it may continue running at a lower setting.
Choosing parts and avoiding failures
| Part | What to check |
|---|---|
| MOSFET | Logic-level operation, voltage rating, and stall-current capability |
| Flyback diode | Reverse-voltage and forward-current ratings |
| Motor supply | Correct voltage and enough current during startup |
| Gate resistor | About 100–220 ohms |
| Gate pull-down | About 10 kilohms |
If the MOSFET becomes hot, it may not be fully on. Use a logic-level part with a low on-resistance at the Arduino’s gate voltage. A hot MOSFET wastes power and can fail.
The diode absorbs the voltage spike produced when current through the motor stops. Without it, that spike can damage the MOSFET or create resets and communication errors.
Exercise
The motor turns off when the Arduino resets, but it sometimes turns on briefly during startup. Which component helps keep the motor off?
Show answer
A gate pull-down resistor, typically 10 kilohms, holds the MOSFET gate near ground while the Arduino pin is not driving it.