Speed control of DC motor with PWM using Arduino

In this guide, you'll learn how to control the speed of a DC motor with Arduino using the powerful PWM (Pulse Width Modulation) technique. PWM is a highly efficient method for adjusting motor speed, allowing you to precisely control the rotation speed of your DC motor, from a complete stop to full speed. This technique is widely used in robotics, automation, and IoT projects. Whether you're a beginner or an experienced maker, this tutorial will show you how to set up the hardware, write the code, and fine-tune your DC motor control system. Follow along to explore the basics of DC motor speed control using Arduino and Arduino PWM motor control, and take your projects to the next level.

DC motor speed control with Arduino PWM

 

What is PWM and How Does It Control DC Motor Speed?

PWM (Pulse Width Modulation) is a widely-used technique for controlling the speed of DC motors by adjusting the width of the pulse signals. By varying the duration of the high and low pulses, PWM regulates the average voltage supplied to the motor, which in turn controls the motor speed. When applied to a DC motor, PWM allows for smooth acceleration, deceleration, and precise speed adjustments.

This method isn’t limited to just DC motor speed control; it can also be used for controlling stepper motors and various other applications. Microcontrollers such as the ATmega328P (found in the Arduino Uno) come with built-in PWM capabilities, simplifying the process and reducing the workload on the processor. With Arduino PWM motor control, there’s no need to manually toggle pins using digitalWrite() or delay() functions, freeing up the processor for other tasks and improving the overall efficiency of your project.

Key Advantages of Using Arduino PWM for DC Motor Control

  • Efficient Processor Use: The built-in PWM feature on Arduino microcontrollers reduces the need for manual pin manipulation, allowing your microcontroller to handle multiple tasks.
  • Precise Speed Control: Achieve fine-grained motor speed adjustments, ranging from 0 to 255, thanks to Arduino’s 8-bit PWM resolution.
  • Versatile Applications: Whether it’s for DIY Arduino DC motor speed controllers, robotics, or automation systems, Arduino PWM is perfect for any project requiring reliable motor control.
  • Reduced Electromagnetic Interference (EMI): By using components such as diodes and capacitors, you can minimize noise and interference in your circuit.

Components and Hardware Required for Arduino PWM DC Motor Control

To get started with Arduino PWM motor control, you’ll need the following components:

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

For more powerful setups, consider using motor driver ICs like L293D or L298 to control higher-rated motors. If you’re interested in learning more, check out our guide on Arduino DC Motor Control using ATmega32 and L293D.

Wiring Diagram for Arduino PWM DC Motor Control

The schematic diagram below shows how to wire the components for Arduino PWM DC motor control:

arduino dc motor circuit diagram

 

In this setup, Arduino PWM pin 10 is connected to the base of a 2N2222 NPN transistor through a 1KΩ resistor. This allows the Arduino to control the transistor, which in turn regulates the power supplied to the DC motor. To prevent accidental current spikes, a 10KΩ resistor is placed between the transistor’s base and ground. The DC motor is connected to the transistor’s collector as the load, while a protection diode (e.g., 1N4001) is connected across the motor terminals to suppress back EMF, protecting the transistor from voltage spikes caused by the motor’s inductance. Additionally, a 0.1µF decoupling capacitor is added to reduce noise generated by the motor, ensuring smooth operation.

Important Note: Always connect the ground of the external 5V power supply to the Arduino’s ground to ensure proper functionality and protect your microcontroller from potential damage.

How Transistors Act as DC Motor Drivers in Arduino PWM Control

In this circuit, transistors like the 2N2222 act as DC motor drivers, amplifying the base current to allow higher currents to flow through the motor. For instance:

  • With a current gain (hfe) of 100, a 20mA base current can drive up to 2000mA (2A) at the collector, making it suitable for 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 and halting its operation.
  • Conversely, when the Arduino sends a HIGH signal, the transistor enters saturation mode, allowing current to flow through the motor and powering it.

For high-current applications, consider using a Darlington transistor like the TIP120, or specialized motor driver ICs such as the L293D or L298 for more efficient control of larger motors. For detailed instructions on motor control with these driver ICs, check out our guide on DC Motor Control with L293D Motor Shield.

Watch Our Video on DC Motor Speed Control with Arduino PWM

For a hands-on demonstration of DC motor speed control using Arduino PWM, watch our video below.

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);
}

In the provided Arduino code, we define motorPin as a user-friendly name for PWM pin 10 to enhance readability. In the setup() function, there's no need to explicitly declare pin 10 as an output pin. This is because, later in the loop() function, we use the analogWrite() function, which automatically configures the pin for output when generating the PWM signal.

Within the loop() function, we call analogWrite() with two arguments:

  1. The pin number (motorPin) that will output the PWM signal.
  2. The PWM value, ranging from 0 to 255, as Arduino uses an 8-bit resolution for PWM. A PWM value of 0 represents no power (motor off), while 255 corresponds to full motor speed.

The motor starts with 0 PWM, which means it's off, waits for 1 second, then ramps up to half power (127 PWM), waits for another second, and finally reaches full power (255 PWM). This cycle repeats continuously.

To summarize:

  • The analogWrite() function sends a PWM signal to the motor pin, controlling its speed between 0 (off) and 255 (full power).
  • The motor starts at zero speed, increases to half speed (127 PWM), reaches full speed (255 PWM), and then slowly ramps back down.

Alternative Control Method: Using a Potentiometer for Manual Speed Adjustment

Alternatively, you can control the motor speed manually using an external potentiometer, as detailed in our DC motor speed control using potentiometer guide. This method gives you real-time control over the motor's speed.

Learning DC motor speed control with PWM using Arduino opens up endless possibilities for applications in electronics, robotics, and automation. By understanding how PWM works, choosing the right components, and writing efficient code, you can build reliable and adaptable motor control systems for your projects. Explore more tutorials on Arduino PWM techniques and harness the power of PWM to enhance your electronics projects.

1 Comments

Previous Post Next Post