Simple DC motor speed control with PWM

By: Jeremy Cook

One of the most fundamental operations in electronics is DC motor speed control. Here, we’ll look at what that means from the basics: sending a PWM signal to a MOSFET or a dedicated driver. This signal controls the DC motor speed directly, using higher currents (and often voltages) than the controller can produce by itself.

Simple LED PWM control

0323-External-LED-with-pulses-indicated-by-oscilloscope-image-2

Jeremy Cook | External LED with pulses indicated by oscilloscope

PWM (i.e., pulse-width modulation) simulates an analog voltage output by repeating pulses for varying lengths of time. In this setup, the period is defined as the time it takes one on/off cycle to complete. The duty cycle, often expressed as a percentage, is the ratio of on time to off time.

A five-volt signal with a 50% duty cycle would result in a simulated 2.5V analog signal. A period of 1ms would produce this on/off state change 1000 times per second (or 1000 Hz), defined as the PWM signal’s frequency. PWM control works well with DC motors and other slowly changing loads. A true analog output is preferable in different situations, like audio reproduction.

We’ll use the Raspberry Pi Pico running CircuitPython to explore this principle to generate PWM signals. For a very brief intro, load the code found here. This code will alternate the onboard LED from full strength, or 65,535 (2^16 – 1), to 4095 (2^12 -1), less than 1/10th of the original value. One can observe the light changing, though it may be difficult to correlate the observed light output with the numerical value.

MOSFET motor control circuit

0323-visual-of-MOSFET-DC-motor-control-circuit-image-3

Jeremy Cook | MOSFET DC motor control circuit

The control methodology of using a MOSFET (FQP30N06L) is essentially the same as for an LED. The code for our next experiment is found here. Hook up the controlling output to the MOSFET’s gain pin through a small resistor (~100Ω). Add a 10k resistor to ground to pull it low when no input is present. The motor’s positive lead runs to the MOSFET drain, while the MOSFET source is connected to ground.

The VBUS pin initially supplied the positive voltage; however, using an external voltage source tied to a common ground with the Pico would generally be better. Implementing a flyback diode would also be advisable to take care of stray inductive loads.

L293D motor driver: An easier way to build a DC motor control circuit

0323-L293D-Motor-Driver-image-4

Jeremy Cook | Caption: L293D Motor Driver

MOSFETs are great general-purpose devices, but if you want to control speed and direction—or if you have more than one motor to take care of—things get complicated very quickly. Fortunately, ready-built motor controller ICs are available to handle this challenge, including the venerable L293D driver used here. Wiring requirements can be determined from the datasheet. The method I used to hook it up to my Raspberry Pi Pico is diagrammed below:

0323-U4-GND-pins-connected-internally-image-5

Jeremy Cook | U4 GND pins connected internally

Digital (on/off) rotation code for this setup is found here, which makes the motor go forwards and backward at full strength, with pauses in-between. This PWM code initializes the motor at a specific duty cycle, ramps it up incrementally, and then stops. It then ramps up in the other direction and stops again before repeating the sequence.

A word of caution on the L293D driver

While using that last bit of code (with a 12V VCC2 input and no capacitor), I ended up disabling a Raspberry Pi Pico board. Good practice would have been to use capacitors to even out the power and logic inputs. The L293D does provide internal clamping capabilities to take care of inductive loads, so no external flyback diodes should have been needed. There are several potential culprits for how this happened, but it should serve as a reminder to ensure the hardware is specified and implemented to meet real-world requirements once it leaves the bench.

Plug-and-play – or full customization?

Your DC motor control circuit designs likely won’t reside on a breadboard forever. From here, there are two diverging paths:

For more advanced prototyping or one-off designs, motor shields and development boards are available that add drivers like the L293D as a plug-and-play solution for DC motors, steppers, servos, and more. The Arduino Motor Shield is perhaps the best known and is compatible with the Arduino Uno form factor. If you’d like to stick with the RP2040 platform, the Maker Pi RP2040 could be an excellent plug-and-play solution for controlling motors and other devices.

On the other end of the spectrum, it’s possible to integrate motor controller designs directly into your own printed circuit board design. This integration gives you near-infinite design freedom, allowing you to, for instance, implement some of the features of the Maker Pi RP2040 while leaving off what you don’t need. You can also lay out your board in the perfect physical form factor for your application.

PWM motor and load control

0323-MOSFET-for-electrical-motor-and-other-load-control-image-6

Jeremy Cook | MOSFET for electrical motor and other load control. As the solenoid acts as an inductor, a flyback diode would still have been a good idea.

The general PWM control principles presented here are sound and can be used for a variety of loads; however, the hardware presented was quickly put together as a demo for this article. For final designs, you will want to carefully consider each particular situation, along with specifications and datasheet info available on Arrow. Your device can have years of problem-free operation with the proper PWM DC motor controller setup.

However your design develops, Arrow is here to supply the microcontrollers, MOSFETs, motor drivers, and other devices needed to make your PWM DC motor speed controller a reality!

See related product

Arduino Motor Shield Rev3 | A000079

Arduino Corporation Power Management Development Boards and Kits View

See related product

L293D

STMicroelectronics Motor Controller and Driver ICs View

ArrowPerks-Loyalty-Program-Signup-banner-EN


Latest News

Sorry, your filter selection returned no results.

We've updated our privacy policy. Please take a moment to review these changes. By clicking I Agree to Arrow Electronics Terms Of Use  and have read and understand the Privacy Policy and Cookie Policy.

Our website places cookies on your device to improve your experience and to improve our site. Read more about the cookies we use and how to disable them here. Cookies and tracking technologies may be used for marketing purposes.
By clicking “Accept”, you are consenting to placement of cookies on your device and to our use of tracking technologies. Click “Read More” below for more information and instructions on how to disable cookies and tracking technologies. While acceptance of cookies and tracking technologies is voluntary, disabling them may result in the website not working properly, and certain advertisements may be less relevant to you.
We respect your privacy. Read our privacy policy here