MOTOR SPEED CONTROL USING MOSFET/BJT & POT

Photo of author

By Jackson Taylor

Home » Electronics Topics » Arduino » MOTOR SPEED CONTROL USING MOSFET/BJT & POT

As an Amazon Associate we earn from qualifying purchases.

A small DC motor runs faster when it gets more average voltage and slower when it gets less. The efficient way to change that average is pulse width modulation (PWM), where a transistor switches the motor on and off very quickly. In this project, a potentiometer sets the speed, an Arduino turns the knob position into a PWM signal, and a MOSFET (or a BJT) switches the motor current.

This guide covers how the circuit works, what parts you need, the Arduino code, and the protection details that keep the transistor from failing.

How PWM speed control works

The Arduino cannot supply much current from a pin, so it never drives the motor directly. Instead, the pin sends a square wave to the gate of a MOSFET. The MOSFET connects and disconnects the motor from its supply many times per second.

Hardware Hack

Stop Buying New Batteries for Your Tech

Electronics are expensive, but their power sources don't have to be. Discover the simple engineering trick to recondition dead batteries and bring your gadgets back to 100% capacity.

Watch the Free Video

The duty cycle is the percentage of each cycle that the signal stays high. At 0% the motor gets nothing, at 50% it gets about half of the supply voltage on average, and at 100% it runs at full speed. Because the transistor is either fully on or fully off, it wastes far less power as heat than a resistor or a linear regulator would.

Parts you need

Pick the MOSFET carefully. Many power MOSFETs need around 10 V at the gate to turn on fully. A logic-level type is designed to switch properly from a 5 V Arduino pin, so check the datasheet for the on-resistance at a gate voltage of 5 V.

See also
HOW TO PROTECT MOSFET OR IGBT FROM VOLTAGE SPIKE

Circuit connections

The circuit diagram below shows the arrangement used with the sketch in this article.

Circuit diagram of an Arduino, potentiometer and MOSFET driving a DC motor with PWM

Potentiometer

Connect the outer pins of the potentiometer to 5 V and GND. Connect the middle pin (the wiper) to analog input A0. Turning the knob changes the voltage at A0 between 0 V and 5 V.

MOSFET and motor

  1. Connect Arduino pin 11 to the MOSFET gate through the gate resistor.
  2. Add the pull-down resistor from the gate to ground, so the motor stays off while the Arduino is starting or resetting.
  3. Connect the MOSFET source to ground.
  4. Connect one motor terminal to the positive motor supply and the other to the MOSFET drain.
  5. Place the flyback diode across the motor, with the cathode (striped end) on the positive supply side.
  6. Join the ground of the motor supply to the Arduino ground. Without a common ground, the gate signal has no reference and the circuit will not behave properly.

Using a BJT instead

An NPN transistor can do the same job. Connect the emitter to ground, the motor between the supply and the collector, and pin 11 to the base through a resistor. The resistor limits the base current, and its value depends on the motor current and the transistor gain, so work it out from the datasheet. A BJT needs continuous base current and drops more voltage than a MOSFET, so it wastes more power and runs hotter at higher loads. For anything beyond a small motor, a MOSFET is the better choice.

Arduino code

The sketch reads the potentiometer, scales the value, and writes it to the PWM pin.

void setup()
{
    pinMode(11, OUTPUT);
}
void loop()
{
    int a=analogRead(A0)/4;
    analogWrite(11, a);
}

What the code does

  • pinMode(11, OUTPUT) sets pin 11 as an output. On an Arduino Uno, pin 11 supports PWM.
  • analogRead(A0) returns a number from 0 to 1023, based on the voltage at the potentiometer wiper.
  • Dividing by 4 scales that range down to 0 to 255, which is the range analogWrite() accepts.
  • analogWrite(11, a) outputs a PWM signal whose duty cycle follows the value, from 0 (always off) to 255 (always on).
See also
WONDERWARE INTOUCH

The loop repeats constantly, so the motor speed follows the knob in real time.

Protecting the circuit

A motor is an inductive load. When the MOSFET switches off, the motor winding pushes current back as a voltage spike that can damage the transistor. The flyback diode gives that current a safe path, so do not leave it out.

  • Heat: a MOSFET that is only partly turned on gets hot. If it does, check that the gate is really reaching the voltage the datasheet requires.
  • Current rating: choose a transistor that handles well above the motor’s running current, and remember that the starting or stall current is higher.
  • Supply noise: motors can cause electrical noise. A capacitor across the motor terminals, and another across the supply, often helps keep the Arduino stable.
  • Separate supplies: do not power a larger motor from the Arduino 5 V pin. Use a separate supply and share only the ground.

Troubleshooting

  • Motor does not run: check the common ground, the gate connection and the MOSFET pinout (gate, drain and source differ between packages).
  • Motor runs only at full speed: the MOSFET may be shorted, or the wiper may not be connected to A0.
  • Motor whines at low speed: this is normal with PWM at about 490 Hz on pin 11 of an Uno. Many motors will not start turning until the duty cycle is above a minimum level.
  • Arduino resets when the motor starts: the supply is sagging or noisy. Use a separate motor supply and add capacitors.

Where to go next

Once this works, you can scale it up. For a bigger load, see the project on adjustable DC motor speed control. If you also need to reverse the motor, look at direction control with an H-bridge, which uses four switches instead of one.

PWM with a single transistor suits brushed DC motors only. A brushless DC motor needs an electronic controller, and mains motors need a different approach, such as the triac method used for speed control of an AC fan.

Sustainable Tech

Keep Toxic E-Waste Out of Landfills

Don't throw away your failing laptop, phone, or car batteries. Learn how to recondition them at home in just a few hours. It's better for the planet—and your wallet.

Learn How It Works
See also
MOSFET & IGBT

FAQ

Can I control motor speed with just a potentiometer?

You can place a potentiometer in series with a very small motor, but it wastes power as heat and gives poor control under load. Using the potentiometer as a control input to a PWM circuit, as here, is far more efficient.

Why divide analogRead by 4?

The Arduino’s analog input has a 10-bit range (0 to 1023), while analogWrite uses an 8-bit range (0 to 255). Dividing by 4 maps one range onto the other.

Is a MOSFET better than a BJT for motor control?

For most motor projects, yes. A suitable MOSFET is controlled by voltage, needs almost no gate current and has low on-resistance, so it runs cooler. A BJT is fine for very small motors, but it needs base current and loses more energy.

Do I really need a flyback diode?

Yes. Without it, the voltage spike from the motor winding when the transistor turns off can destroy the MOSFET or BJT over time.

Revive Your Dead Electronics

Got a drawer full of dead gadgets or a failing car battery? Instead of paying a premium for replacements, use this simple trick to easily recondition them right from home.

See How It Works