小编
Published2025-10-15
Introduction to DC Motor Speed Control with Arduino
If you’re interested in robotics, automation, or just exploring electronics, understanding how to control the speed of a DC motor is a vital skill. DC motors are commonly used in many devices, from simple toys to complex robotics systems, and their speed is often a critical factor in performance. By harnessing the power of an Arduino, you can precisely control the speed of a DC motor with ease.

The Arduino is an open-source microcontroller platform that's beginner-friendly and perfect for learning electronics and programming. With the right components, such as a DC motor, a motor driver, and the Arduino board, you can create a project where you control the motor’s speed seamlessly. In this guide, we’ll cover how to control the speed of a DC motor using Pulse Width Modulation (PWM), a technique that allows for efficient and precise control of motor speed.
Before diving into the details, let’s first list out the components required to control the DC motor with Arduino:
Arduino Board (Arduino Uno, Nano, or any other compatible board)
DC Motor (a small 6V or 12V DC motor is ideal for beginners)
Motor Driver Module (L298N, L293D, or similar motor driver)
External Power Source (for powering the motor, often a 9V battery or a regulated power supply)
Breadboard and Jumper Wires
Potentiometer (optional, for manual control of speed)
Resistors, Capacitors (for circuit stability)
Once you have all the components ready, you can start setting up your circuit and then move to the coding phase.
What is PWM (Pulse Width Modulation)?
PWM stands for Pulse Width Modulation, and it’s the key to controlling motor speed. PWM involves switching a signal on and off rapidly to create an average voltage that controls the speed of the motor. The "duty cycle" of the PWM signal determines the speed.
Duty cycle refers to the percentage of time the signal stays "on" within one cycle.
A higher duty cycle means more time the motor is powered, leading to faster speed.
A lower duty cycle means less time the motor is powered, resulting in a slower speed.
For example, a PWM signal with a 50% duty cycle will make the motor spin at half its maximum speed, while a 100% duty cycle will make it run at full speed.
Setting Up the Motor Driver Circuit
In order to control the motor, you need to use a motor driver, as the Arduino alone cannot provide the current needed to drive the motor. The motor driver acts as an interface between the low-power Arduino and the high-power motor.
Let’s take the L298N motor driver as an example, which is commonly used in Arduino projects. Here’s how to wire it:
Motor Connections: Connect the two terminals of the DC motor to the output pins of the L298N driver (OUT1 and OUT2).
Arduino Connections: Connect the input pins of the L298N (IN1 and IN2) to the digital PWM pins on the Arduino (such as pins 9 and 10).
Power Connections: Connect the motor power supply (e.g., a 9V battery) to the motor power input (VCC) on the L298N. Make sure to connect the Arduino ground to the motor driver ground as well.
Enable Pin: Connect the enable pin (EN) to a 5V source to ensure the motor driver is activated.
With the hardware ready, you can proceed to the coding section to control the motor’s speed using PWM.
Arduino Code for DC Motor Speed Control
Now, let’s dive into the programming part. The Arduino code will use PWM to control the motor speed. Here’s a simple example:
// Define pins for motor driver
int motorPin1 = 9; // IN1
int motorPin2 = 10; // IN2
int speedPin = 3; // PWM pin to control motor speed
// Set motor pins as output
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(speedPin, OUTPUT);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
// Control motor speed with PWM
analogWrite(speedPin, 128); // 128 is a value between 0 and 255 (50% duty cycle)
delay(2000); // Let the motor run for 2 seconds
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
analogWrite(speedPin, 0); // Motor is off
Pin Definitions: The pins for controlling the motor direction (motorPin1, motorPin2) and the PWM pin (speedPin) are defined at the beginning of the code.
Pin Setup: In the setup() function, we set the motor pins as outputs.
Motor Control: The motor direction is controlled by setting motorPin1 HIGH and motorPin2 LOW. This turns the motor in one direction.
Speed Control: The analogWrite() function is used to send a PWM signal to speedPin, which controls the motor speed. The value 128 represents a 50% duty cycle (out of 255).
Motor Off: After a 2-second delay, we stop the motor by setting both motor control pins to LOW and sending a PWM signal of 0 to stop the motor.
This code will make the motor run at half speed for 2 seconds, then stop.
Advanced Techniques and Customizing Your Motor Control
Adding Speed Control with a Potentiometer
While controlling the motor speed with a fixed PWM value is useful, you may want to adjust the speed in real time. A great way to do this is by using a potentiometer, which is essentially a variable resistor that allows you to adjust the voltage input to an analog pin on the Arduino. Here’s how you can implement it:
Potentiometer Connections: Connect one end of the potentiometer to 5V and the other end to ground. The middle pin (wiper) should be connected to an analog input pin on the Arduino (e.g., pin A0).
Arduino Code with Potentiometer
int motorPin1 = 9; // IN1
int motorPin2 = 10; // IN2
int speedPin = 3; // PWM pin
int potPin = A0; // Potentiometer connected to analog pin A0
int potValue = 0; // Variable to store potentiometer value
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(speedPin, OUTPUT);
// Read the potentiometer value (0 to 1023)
potValue = analogRead(potPin);
// Map potentiometer value to PWM range (0 to 255)
int motorSpeed = map(potValue, 0, 1023, 0, 255);
// Control motor speed with PWM
analogWrite(speedPin, motorSpeed);
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
Potentiometer Reading: The analogRead(potPin) function reads the value from the potentiometer (ranging from 0 to 1023).
Mapping the Value: The map() function is used to convert the potentiometer’s range (0-1023) to a suitable PWM range (0-255).
Motor Speed Control: The analogWrite() function adjusts the motor speed based on the potentiometer’s position.
Creating a Smooth Ramp-Up and Ramp-Down Effect
Sometimes, you may want the motor to gradually accelerate or decelerate, instead of jumping directly to the desired speed. This can be achieved by gradually changing the PWM value over time, which creates a smooth transition. Here’s an example of how to implement ramp-up and ramp-down:
int speed = 0; // Motor speed, ranging from 0 to 255
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(speedPin, OUTPUT);
for (speed = 0; speed <= 255; speed++) {
analogWrite(speedPin, speed);
Kpower has delivered professional drive system solutions to over 500 enterprise clients globally with products covering various fields such as Smart Home Systems, Automatic Electronics, Robotics, Precision Agriculture, Drones, and Industrial Automation.
Update:2025-10-15
Contact Kpower's product specialist to recommend suitable motor or gearbox for your product.