Speed control of DC motor with PWM using Arduino

Introduction to DC Motor Speed Control Using Arduino PWM

Learn how to control a DC motor using the Arduino PWM built-in capability , a highly efficient technique for speed regulation . By leveraging the power of Pulse Width Modulation (PWM) , you can precisely adjust the speed of your DC motor, from zero to maximum and back down. This method is widely used in robotics, automation, and IoT projects. We’ll walk you through the process step-by-step, including hardware setup, code implementation, and optimization tips. Whether you’re building a DIY project or exploring Arduino-based motor control systems , this guide will help you master DC motor speed control using Arduino PWM .

What is PWM and How Does It Work for Motor Speed Control?

PWM (Pulse Width Modulation) is a technique used to generate signals with varying widths of high and low pulses. The modulation of these pulse widths determines the average voltage supplied to the motor, effectively controlling motor speed with PWM. When applied to a DC motor, PWM adjusts the amount of power delivered, enabling smooth acceleration, deceleration, and precise speed control.

This PWM technique for speed control is not only limited to DC motor control but also extends to stepper motor control and other applications. Microcontrollers like the ATmega328p (used in Arduino Uno) come with built-in PWM circuitry , making it easy to implement without overloading the processor. For instance, instead of manually toggling pins with digitalWrite() and delays—which can tie up processor time—you can use the Arduino PWM feature to free up resources for multitasking.

Key Advantages of Using Arduino PWM for DC Motor Control

  1. Efficient Processor Usage : Built-in PWM functionality ensures the microcontroller remains available for other tasks.
  2. Precise Speed Regulation : Adjust motor speed from 0 to 255 levels using an 8-bit resolution.
  3. Versatile Applications : Ideal for DIY Arduino DC motor speed controllers , robotics, and automation systems.
  4. Reduced Noise : Incorporating components like diodes and capacitors minimizes electromagnetic interference (EMI).

Hardware & Components Required for Arduino PWM DC Motor Control

To build your Arduino PWM DC motor controller , gather the following components:

  1. Arduino Uno (with USB cable)
  2. DC Motor (rated 3V to 5V for this tutorial)
  3. General-purpose transistor (e.g., 2N2222 or TIP120 for higher current motors)
  4. Resistors : 1KΩ and 10KΩ
  5. Protection Diode : 1N4001, 1N4007, or 1N4148
  6. Decoupling Capacitor : 0.1µF
  7. External 5V DC Power Supply
  8. Connecting Wires

For advanced setups, consider using motor driver ICs like L293D or L298 for higher-rated motors. Learn more about motor drivers in our tutorial on Arduino DC Motor Control using ATmega32 and L293D .


Wiring Diagram for Arduino PWM DC Motor Control

The schematic diagram below demonstrates how to connect the components.

 arduino dc motor circuit diagram

  • The Arduino PWM pin 10 is connected to the base of a 2N2222 NPN transistor via a 1KΩ resistor.
  • A 10KΩ resistor is added between the transistor’s base and ground to prevent accidental current spikes.
  • The DC motor is connected as the load at the transistor’s collector.
  • A protection diode (1N4001) is placed across the motor terminals to suppress back EMF, protecting the transistor.
  • A decoupling capacitor (0.1µF) reduces noise generated by the motor.

Note: Always connect the external 5V power supply’s ground to the Arduino’s ground to ensure proper operation and avoid damaging the microcontroller.


How Transistors Act as DC Motor Drivers

Transistors like the 2N2222 amplify the base current to drive higher currents through the motor. For example:

  • With a current gain (hfe) of 100, a 20mA base current can theoretically produce 2000mA (2A) at the collector—enough to drive most small to medium-sized DC motors.
  • When the Arduino outputs a LOW signal, the transistor enters the cutoff region, stopping current flow to the motor.
  • Conversely, a HIGH signal drives the transistor into saturation, allowing current to flow and powering the motor.

For high-current applications, consider using a Darlington transistor (TIP120) or dedicated motor driver ICs like L293D or L298. For this see the guide on DC motor control with L293D Motor Shield.

Video Demonstration of DC Motor Speed Control with PWM

Check out our video demonstration showcasing DC motor speed control using Arduino PWM :

Arduino Code for controlling DC motor using PWM

The DC motor control code using PWM is below.


/* Processor: Arduino Uno
 * Compiler:  Arduino AVR
 * http://ee-diary.com
 */


// Name pin 10 as motorPin
int motorPin = 10;


void setup () {
// For PWM the PWM pins don't require the pinMode() function
}


void loop() {

// Turn motor on from zero
analogWrite(motorPin, 0);
// Wait 500 ms
delay(500);

// Turn motor to 1/2 the power
analogWrite(motorPin, 127);
// Wait 500 ms
delay(500);

// Turn motor to maximum speed
analogWrite(motorPin, 255);
// Wait 500 ms
delay(500);
// Turn motor to 1/2 the power
analogWrite(motorPin, 127);
// Wait 500 ms
delay(1000);
}

Code Explanation: 

In the code, we have declared motorPin as our name for the PWM pin 10 for code readability. In the setup() function, we don't need to declare pin 10 as output because later on in the loop() function we will call the analogWrite() function which will call the pinMode() function and declare the pin 10 as output. Next in the loop() function we call analogWrite() function with two arguments - the pin or pin name(motorPin) which we will be using to output PWM signal and the value of the PWM which ranges from 0 to 255 because Arduino is 8-bit microcontroller. A PWM value of 0 means no PWM and highest PWM pulse signal with highest power is 255. We start by sending lowest PWM value which is 0, wait for 1 second, then the half power PWM for which we send 127 and wait for 1 second. And finally we send PWM signal with highest power which is given by value 255 and again wait for 1 second. This process is then repeated. In brief,

  • The analogWrite() function outputs PWM signals ranging from 0 (off) to 255 (full power).
  • The motor starts at zero speed, ramps up to half-speed (127), reaches full speed (255), and then gradually slows down.

Another way is to control the PWM width with an external potentiometer and control the DC motor manually which is provided in the guide DC motor speed control using potentiometer. Learning DC motor speed control with PWM using Arduino opens doors to countless applications in electronics, robotics, and automation. By understanding the principles of PWM , selecting the right components, and implementing efficient code, you can create reliable and versatile motor control systems. Explore more tutorials on Arduino PWM techniques and take utilize the PWM feature to your advantage in your electronics projects.

1 Comments

Previous Post Next Post