Sub topic 3
Switching a DC Motor with a Logic-Level MOSFET
Prerequisites
This chapter assumes you can upload sketches, use pinMode and analogWrite, and read a basic circuit diagram. You know that an Arduino I/O pin supplies only about 40 mA, which is far too little for a motor.
Switching with a MOSFET
A logic-level N-channel MOSFET is an electronic switch. Its gate turns the switch fully on at the 3.3 V or 5 V logic level, so the Arduino can drive it directly. Connect the motor between the external supply and the drain, and the source to ground. Place a 150 ohm resistor between the Arduino pin and the gate, and a 10k pull-down resistor from the gate to ground.
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);
}
}Place a flyback diode across the motor, with its cathode toward the supply positive. When the MOSFET turns off, the motor coil produces a voltage spike. The diode absorbs that spike and protects the MOSFET.
Reversing Direction with an H-Bridge
An H-bridge is a circuit of four switches that reverses the current through a motor. A driver chip such as the TB6612FNG holds two H-bridges. Two direction pins select forward, reverse, or stop, and PWM on the speed pin sets the speed. Check your chip's datasheet for the exact pin names.
Noise, Decoupling Capacitors, and Brownouts
A motor draws a large current spike when it starts or stalls. That spike can pull the supply voltage down. A brownout is a dip low enough to reset the Arduino. A 100 µF electrolytic capacitor across the motor supply, plus a 0.1 µF ceramic capacitor close to each chip, absorbs most of these spikes.
A First Look at Stepper Motors
A stepper motor turns in fixed steps. It needs a driver board that switches its coils in sequence. Stepper coils draw more current than a small DC motor, so use a dedicated driver board instead of a single MOSFET.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Arduino resets when the motor starts | Brownout from the current spike | Add a bulk capacitor and a separate motor supply |
| Servo jitters | Noisy or weak shared supply | Power the servo separately and add capacitors |
| Motor stays silent, MOSFET is warm | Gate not fully on, or a non-logic-level MOSFET | Check the gate voltage and the part number |
| Driver or MOSFET runs hot | Current above the part rating | Lower the load current and check the datasheet |
Exercise
The motor keeps running when the Arduino pin is disconnected and the sketch is not running. Which part is most likely missing, and what does it do?
Show answer
The 10k pull-down resistor from the gate to ground is missing or open. It holds the gate at 0 V, so the MOSFET stays off whenever the Arduino pin is floating, during reset, or before setup runs.